I may be an idiot, I'm very tired right now, but why the times 1000 or mod 2? Won't that always be 0? If this is any normal language rounding random will either give 0 or 1 which will go to 1000 or 0 with the multiplication. 1000 % 2 is zero, thus no matter what the output is zero
Am I missing something?
No shade BTW the joke was clear and funny even if I'm right
In Javascript, Math.random() produces a float value between 0 and 1, so I multiply by 1000 and round it to get a larger integer. The value %2 == 0 is a non-library way of performing isEven() on the random integer (value % 2 is 0 if even, 1 if odd, and the ==0 makes it return a boolean). When used in the if statement, it's essentially a coin flip.
does javascript not allow you to interpret integers as booleans in a conditions directly? seems it'd be simpler to just do math.round(math.random()), which should still get you true (1) or false (0) in equal likelihood. or am i missing something?
Yes sure. Code is logical stepwise. By including the "only if" it implies that other stuff is taken into account, which it isn't at that moment in code.
I mean, I don't need to extend the implications of an IF statement. It already does exactly what it says.
Anyway fuzzy logic does exist for people who want "sometimes if". It's useful in certain cases. I've only ever considered it in music production, where things very often get to the point of complexity where it makes a (sometimes) useful difference.
In normal parlance, "if and only if" rules out that something could also happen as a result of other circumstances. EG, if you fall out of a plane, you will lose your glasses. But there are other conditions that would lead to the same result.
In code, the alternative would be to have a different if statement that executes identical code. Or *cough* ~you could use a jump statement to execute literally the same code.~