I’m working through eloquent javascript and I don’t understand this bit of code
for (var current = 20; current % 7 != 0; current++)
;
console.log(current);
It states that it checks for the first number greater than 20 that is divisible by 7 and console.log() would produce 21.
But I read that as start at 20, check if current divided by 7 has no remainder to see when we break the loop. But straight away 20 mod 7 == 6 meaning it does not equal 0 or (20 % 7 != 0).
So shouldn’t the for loop break straight away and console.log(current) produce 20? What am I missing here?
A loop continues as long as the condition is met.
No, it’s “check if
currentdivided by 7 does NOT have no remainder”.I think the negative condition is throwing you. Basically, you need to ask if the condition is a true statement.
So if I’m the condition, and you’re the loop, and I say “six does not equal zero”, you would say “that’s true”, so you would agree to let the loop continue.
But then if I say “zero does not equal zero”, you would say “that’s false”, and would halt the loop.
A clearer condition would be “check if
currentdivided by 7 does have a remainder”. No double negative, so it could be written as:or as: