I literally have no idea how this works, how does 17 % 2 = 1. According to the lecture you multiply the second number with itself and then subtract it?

Tell us what’s happening:
Describe your issue in detail here.

``````
const remainder = 11 % 3;
``````

User Agent is: `Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36`

Challenge: Finding a Remainder in JavaScript

Actually, the `%` symbol is the remainder symbol which returns what’s left after the division would’ve been executed. For example, `5%3` will return 2.

1 Like

Right, it’s more technically called a modulo, but in school we always called it “remainder”.

Remember learning long division? If the divisor doesn’t evenly divide into the dividend, you have a “left over” number or a “remainder”. If I try to divide 24 by 10, 10 goes in twice but then I have 4 left over, the remainder, the modulo. Another way to think about it is if I got the decimal answer (2.4), rounded it down (to 2), multiplied it by the divisor (2 X 10 = 20) and then subtracted it from the original dividend (24 - 20 = 4), it yields the remainder.

2 Likes

Thank you, I think I get it now. But since we are rounding up. How is 11 % 3 = 2. When 11/3 = 3.67 , so shouldn’t be 11 % 3 = -1 if we round up? Or do we just drop the decimal number?

There is no rounding up. We care about the remainder.

11/3 can be written as (9+2)/3 or 3 + 2/3. That is where the 2 comes from. 2 is the remainder when we divide 11 by 3.

1 Like

Or (in the interest of helping) you could also say that this way:

13 / 4

``````13 = (4 * 3) + 1
``````

or more abstractly:

``````dividend = (divisor * quotient) + remainder
``````

If we insist that answer is an integer. This is sometimes referred to as “Euclidean division” or “remainder division”. It is usually the first division that you learn in school, that the answer is a quotient and a remainder. If a teacher asked you in 2nd grade, “What is 13 divided by 4?”, you’d answer “4 goes evenly into 13 three times, with a remainder of 1”. The modulo operator (`%`) is just shortcut to the remainder.

Another way to find it is:

``````const dividend = 13;
const divisor = 4;

const quotient = Math.floor(dividend / divisor) // 3
const remainder = dividend - (quotient * divisor) // 1
``````

But if all you need is the remainder and don’t need the quotient, you can skip all that and just do:

``````const dividend = 13;
const divisor = 4;

const remainder = dividend % divisor // 1
``````

It’s just a shortcut to the remainder - sometimes it’s all you need.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.