Not understanding Modulus

Wassup World,

I’m practicing on the Go app and came across this problem.

Now, when I do hand math, I get 9.66667, which I chose first and got wrong.

I’m not understanding how the answer is rounded up to 10.

Like, how does 10 % 3 = 1 (p. 76, JavaScript & jQuery, Duckett)

Please advise

(upload://fEmTAoNcwDlTNhfgYIpQHt4ZhoR.jpeg) You must select a tag to post in this category. Please find the tag relating to the section of the course you are on E.g. loops, learn-compatibility

When you ask a question, don’t forget to include a link to the exercise or project you’re dealing with!

If you want to have the best chances of getting a useful answer quickly, make sure you follow our guidelines about how to ask a good question. That way you’ll be helping everyone – helping people to answer your question and helping others who are stuck to find the question and answer! :slight_smile:

num1 = 5, and num2 = 3.
The modulus, or remainder operator %, yields the remainder of the division.
5 divided by 3 = 1.66667, or 1 w/ remainder 2. So 5%3 = 2.

1 Like

You may find the MDN documentation for the modulo, or as they call it the “remainder”, operand helpful.

Put simply, as @codeysoup says, the modulo operator gives you the remainder after the division.

To provide an analogy, imagine that you had 5 pieces of cake to share between 3 people so that everyone got the same number of pieces. You could give each of them one piece each, and you’d be left with a remainder of 2 pieces. :slight_smile:

1 Like

So if I had 10 pieces of cake and had to share it with 3 people, I’d have 7 pieces left over, not 1.

So how does 10 % 3 = 1 and not 7?

No, if you had 10 pieces of cake to share equally between 3 people, you would have one left over because you could give each of them 3 pieces. They each get the same number of pieces, not simply one each. :slight_smile:

There are 3 “whole threes” in 10 - 3 x 3 = 9 - with a remainder of 1. So, 10 % 3 = 1.

2 Likes

You wanna give each of them maximum pieces, while still being fair. So your remainder will never be as big or bigger than the number of people (the divider).

2 Likes

Ok. I get that now.

I was thinking the answer to 5 % 3 was suppose to be the answer you’d get from regular division. But I see the output should be the remainder of the regular division, which in this case is 2. That’s why the answer is 10 (5+3+2=10)

1 Like

Ok. I get it now. I wasn’t considering “equally” giving them out.

Thanks for the insight.

1 Like