Loops & Iterators - 9. Next


Link: https://www.codecademy.com/courses/ruby-beginner-en-XYcN1/2/2?curriculum_id=5059f8619189a5000201fbcb

For some reason this code isn't working and I really can't see what's wrong with it! I'm using modulus to see if a number is odd, then skip to the next iteration of the loop if it is. But I'm getting the following error message:

Oops, try again. It looks like your loop doesn't print out the numbers 18 to 0.

I know I need to add an exception for 1, because 1/3 won't equal 0. But it doesn't seem to be writing the correct numbers to the console. Here's what comes out:


Can anyone see what's wrong with this code? Thank you!

i = 20

loop do    
  i -= 1
  next if i % 3 == 0
  break if i <= 0
  print i


Whether a number is odd has to do with whether it's divisible by two, not three

Your error message also mentions 0, make sure you get the first and last number right


I think my code does show what it should but it is still wrong, why?
Thank you !


Following ionastan's advice, I managed to solve it. Try this:

i = 20

loop do    
  i -= 1
  next if i % 2 == 1
  break if i < 0
  print i


Silly me. Yes, fixed it now. Thank you!



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