Hacker News new | past | comments | ask | show | jobs | submit login

  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.



Oops, 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/Operat...




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: