Modulo not working


#1



https://www.codecademy.com/en/courses/getting-started-v2/4/5?curriculum_id=506324b3a7dffd00020bf661


So, I thought I could use modulas to cleaverly make my statement come out true and print out the string "I finished my first course!" Despite having done my syntax correctly,it still doesnt work. I realise that 5%2 returns 1 as its value. What I dont understand is that 5 devided by 2 gives you 2.5 with 5 as its remainder. Therefore my understanding was that 5%2 evaluates to 5 not 1. I could be wrong. This is my code:

var cond="five5".length;

if(cond === 5%2){
    console.log("I finished my first course!")
}else{
    console.log("Not working buddy")  
}


#2

if(cond === 5%2)// wrong

cond=5
and
5%2=1

and your condition is
cond===5%2

think !!!

@designcoder78823


#3

You mean modulo?

if ( 5 % 2 ) {
    // will be true
} ...

#4

Uhm, no need to be rude buddy. I have been trying to think and did not come up with a solution, thus I created the topic. And your answer does not explain what you are trying to tell me. In future, reply if you genuinely want to help, not gloat because you know the answer.


#5

Yes I did mean modulo thanks. :slight_smile: Am i wrong in thinking 5%2 evaluates to 5? Seeing as 5 devided by 2 is 2.5.


#6

The modulo is the remainder, not the quotient. 2.5 is the quotient, the remainder is 1. 1 is truthy, so 5 % 2 => true. 4 % 2 => false, which is why we always equate to zero to find even numbers.

if ( x % 2 === 0) {
    // x is even
}

#7

Supplement

Consider n % 1. What will this evaluate to? Think carefully...

Answer:

Now consider, what is the largest a modulo can be?

Answer:


#8

Well it would evaluate to zero. Right?


#9

Perhaps, but what if the number has a decimal value? It will then evaluate to a decimal fraction.

10.5 % 1 => 0.5     // remember, its the remainder

The floating point value is what comes out.

Math.PI % 1 => 0.1415...

In a modulo calculation, the divisor is also known as the modulus. The second question above has a simple answer:

The modulo can never be equal to or greater than the modulus. In the case above, with % 1, it can approach 1 but never reach it. In the case of integer division, the modulo can never be more than 1 less than the modulus.

6 % 6 => 0
6 % 5 => 1
6 % 4 => 2
6 % 3 => 3
6 % 2 => 4
6 % 1 => 5
6 % 0 => 0