This works fine, but I want to make it prettier - and accommodate all values that are divisible by 4:
if i==4 || i==8 || i==12 || i==16 || i==20 || i==24 || i==28 || i==32 # ... end
Any clever, short method to do this?
It gives the remainder when counter is divided by 2. For example: 3 % 2 == 1 2 % 2 == 0.
The modulus operator turns out to be surprisingly useful. For example, you can check whether one number is divisible by another—if x % y is zero, then x is divisible by y. Also, you can extract the right-most digit or digits from a number. For example, x % 10 yields the right-most digit of x (in base 10).
Try this:
if i % 4 == 0
This is called the "modulo operator".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With