I'm having trouble understanding Lesson 9

#1

I'm not sure I understand how this loop works

So, the exercise wants me print numbers from 20 - 0 (skipping 20 in this case) that are even.

in line 4, I originally did "next if i % 2 == 0" to get even number, as even numbers wouldn't have a remainder, but for some reason the program printed all odd numbers.

Only when i changed the line to the current code, did it work the way it's supposed to, but looking at the code, one would assume that anything with a remainder of 1 in this instance would be an odd number, not an even number.

So, could someone help me understand this?

Thanks

``````i = 20
loop{
i -= 1
next if  i % 2 == 1
print "#{i}"
break if i <= 0
}``````

#2

Because you're skipping to the next if i % 2 == 0 (ie: and even number). You should try to see if you can skip it to next if it's NOT an even number. !(i % 2 == 0). Everything else seems right.

#3

Oh! I get it. The "next" goes to the next item in the loop if the parameter is true.

My assumption was it only continued the loop if the statement were true.

Thanks!

#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.