• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

JavaScript - Modulo

Beo

Three Magic
Joined
Aug 25, 2009
Messages
9,074
Solutions
1
Reaction score
857
Can someone explain it to me further? I really get stuck with these... xd

Doing Codecadamy exercises. Stuck on this;

So why learn modulo? For one thing, it's good at testing divisibility. Consider 30 % 10. What does it return? There is nothing left over, so 0.

How about 9 % 3? Also 0.

200 % 100? You guessed it....2! Just kidding, it's also 0.

This is fun when we combine it with conditionals.

Edit line 3 so the correct prompts are displayed.

Code:
//An example of an if/else statement with modulo in the condition



if(  ) {
    console.log("The first number is even!");
} else {
    console.log("The first number is odd!");
}


So I have to fill the code out with the answer, which I have no idea what it is. Hint isn't very helpful either lol.

I don't want the exact answer, I just want to understand it. Thanks.

Probably coz I'm so tired. Gonna sleep and try again tomorrow :)
 
Okay, so a Modulo or a Modulus Operator is simply this "%". It returns only the remainder. If either value is a string, an attempt is made to convert the string to a number. Say you had..
HTML:
var resultOfMod = 26 % 3;

It would result in the remainder of 2 being stored in the variable "resultOfMod".

So..
HTML:
    <script language="JavaScript">
    <!--
    answer = 7 % 2;
    document.write("answer = ",answer);
    -->
    </script>

The modulus operator behaves differently for special values...

If the operands are numbers, regular arithmetic division is performed, and the remainder of that division is returned.
If the dividend is Infinity or the divisor is 0, the result is NaN.
If Infinity is divided by Infinity, the result is NaN.
If the divisor is an infinite number, the result is the dividend.
If the dividend is 0, the result is 0.
 
I haven't learnt anything about using "var" so I'm guessing that isn't in my answer.. Unless they think I'm psychic.

I know what modulos are, I'm asking for help on the question I am on.

If you've done that, then I need more explanation. I'm bad with math. ;d
 
I still have no idea, perhaps I should just delay working on my JavaScript skills for now, doing HTML / CSS exercises first, seems pretty easy xd
 
Here's how you do modulus:

20%10 = 0
How many times does 10 go into 20?
2, how much remaining? 0
10 + 10 = 20, with 0 remaining

16%5 = 1
How many times does 5 go into 16?
3, how much remaining? 1
5 + 5 + 5 = 15, with 1 remaining

1212%26 = 16
How many times does 26 go into 1212?
46, how much remaining? 16
26 * 46 = 1196, with 16 remaining

Even/Odds:
To determine whether the number is even or odd, you just use mod of 2.

72%2
How many times does 2 go into 72?
36, how much remaining? 0
When there is nothing remaining, it is even.

95%2
How many times does 2 go into 95?
47, how much remaining? 1
When there is 1 remaining, it is odd.
 
Here's how you do modulus:

20%10 = 0
How many times does 10 go into 20?
2, how much remaining? 0
10 + 10 = 20, with 0 remaining

16%5 = 1
How many times does 5 go into 16?
3, how much remaining? 1
5 + 5 + 5 = 15, with 1 remaining

1212%26 = 16
How many times does 26 go into 1212?
46, how much remaining? 16
26 * 46 = 1196, with 16 remaining

Even/Odds:
To determine whether the number is even or odd, you just use mod of 2.

72%2
How many times does 2 go into 72?
36, how much remaining? 0
When there is nothing remaining, it is even.

95%2
How many times does 2 go into 95?
47, how much remaining? 1
When there is 1 remaining, it is odd.

Indeed, but how would I make it so it would go to false if odd and true if even? I really don't know what to put on line 3. It's probably staring me straight in the face, but once again I've been working all day and I'm tired. lol
 
if X%2=0, it's even
if X%2=1, it's odd

So, to fill your program, it's if ANYTHING%2 = 0, then it's true (even).

I don't know how you would define ANYTHING in your example.
 
Indeed, but how would I make it so it would go to false if odd and true if even? I really don't know what to put on line 3. It's probably staring me straight in the face, but once again I've been working all day and I'm tired. lol

HTML:
function isEven(n) 
{
   return isNumber(n) && (n % 2 == 0);
}

function isOdd(n)
{
   return isNumber(n) && (n % 2 == 1);
}

Something like this? :o
 
Back
Top