2 equalities on an IF conditional like in python

59 views Asked by At

I'm starting to learn javascript for front-end programming, being python my first language to learn completely.

So I'm trying to solve a while loop excersise that console-logs every number from 50-300 that is divisble by 5 and 3.

So in python i would do this:

i = 50
while i < 301:
    if i % 5 == i % 3 == 0:
        print(i)
    i += 1

And works flawlessly. I know you could use and and operator but the whole point of this question is to avoid using it.

So I try the same thing in javascript

var i = 50;
while (i < 301){
    if (i % 5 === i % 3 === 0){
        console.log(i);
    }
    i ++;
}

And somehow that wont work. However, with an && operator it does. Are double equalities in javascript not allowed? If they are, what am I missing?

1

There are 1 answers

1
hobbs On BEST ANSWER

It's allowed, and it does exactly what you told it to do -- it's just that that's not the same as what you want it to do. i % 5 === i % 3 === 0 is the same as (i % 5 === i % 3) === 0 (because === is left-associative). (i % 5 === i % 3) evaluates to either true or false depending on the value of i, and true === 0 and false === 0 are both false, so the condition will always be false.