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 can also:
Seriously, there's no jQuery plugin for odd/even checks?
Well, not anymore - releasing "Oven" a jQuery plugin under the MIT license to test if a given number is Odd/Even.
Source code is also available at http://jsfiddle.net/7HQNG/
Test-suites are available at http://jsfiddle.net/zeuRV/
Please write the following code in your console:
Please Note: It will return true, if the entered number is even otherwise false.
Use Modulus, but.. The above accepted answer is slightly inaccurate. I believe because x is a Number type in JavaScript that the operator should be a double assignment instead of a triple assignment, like so:
Remember to declare your variables too, so obviously that line couldn't be written standalone. :-) Usually used as an
if
statement. Hope this helps.array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
array.each { |x| puts x if x % 2 == 0 }
ruby :D
2 4 6 8 10