How do I figure out if a variable is divisible by 2? Furthermore I need do a function if it is and do a different function if it is not.
相关问题
- Is there a limit to how many levels you can nest i
- How to toggle on Order in ReactJS
- void before promise syntax
- Keeping track of variable instances
- Can php detect if javascript is on or not?
You don't need jQuery. Just use JavaScript's Modulo operator.
Hope this helps.
Here is a complete function that will log to the console the parity of your input.
Use modulus:
You can do it in a better way (up to 50 % faster than modulo operator):
odd: x & 1 even: !(x & 1)
Reference: High Performance JavaScript, 8. ->Bitwise Operators
You can use the modulus operator like this, no need for jQuery. Just replace the
alerts
with your code.