Loops & Iterators Next



I am trying to create a loop that skips odd numbers and prints the even.
This is my code:-
for i in 0..18
next if i % 2 > 0
print i

Which gives "0246810121416180..18" as the output but I get an error message:-"Oops, try again. It looks like your loop doesn't print out the numbers 18 to 0."

Can anyone help?


for i in 0..18
  next if i % 2 > 0
  print i


I'd avoid using a for loop right here.

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

Using a range in your for loop causes the output to 'count up'. The lesson wants you to count down.


Great, thanks flight_dragons,
I hadn't realised that it wanted to count down, should have guessed from the error message!!
That all makes sense, I just to to get a better understanding of %.


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