Modulus
Explore the modulus operation and how to implement it recursively within numerical problems. This lesson helps you understand dividing numbers, calculating remainders, and structuring base and recursive cases for modulus in JavaScript recursion, preparing you to handle related coding interview questions.
We'll cover the following...
What is the modulo operation?
The modulo operation (abbreviated as mod) returns the remainder when a number is divided by another. The symbol for mod is %.
The number being divided is called the dividend, and the number that divides is called the divisor.
The illustration below represents the concept of remainders using basic division:
Mathematical Notation
The above illustration can be mapped on to the following equation:
Generically,
Implementation
Let’s have a look at the code:
Explanation:
Let’s discuss how we reached this solution. Look at the illustration below. It shows that if a number is divided by , it can give remainders: , , ...