3 ms·
rand10=Math.floor(Math.random())*10 // before rand10=0|Math.random()*10 // after The first is not random, it will always return zero (this error is
by leif 15y ago
rand10=Math.floor(Math.random())*10 // before
rand10=0|Math.random()*10 // after
The first is not random, it will always return zero (this error is very common). The second happens to be ok because * binds tighter than |. However, this is order-dependent.
- jedschmidt 15y agoOops, that was an error (the multiplication should be within the .floor call). It's fixed now. This style of code, while not for everyone, exploits the "happens to be okay" operator precedence. For this, Mozilla's MDC is a great resource: https://developer.mozilla.org/en/JavaScript/Reference/Operators/Operator_Precedence https://developer.mozilla.org/en/JavaScript/Reference/Operat...