Help with exercise NEXT; 5.9


<In what way does your code behave incorrectly? Include ALL error messages.

My code error states "Oops, try again. It looks like your loop doesn't print out the numbers 18 to 0." Yet my result is: 246810121416181..18. I am highly confused at this point.

i = 18
for i in 1..18
  next if i % 2 == 1
  print "#{i}"
  i -=1
  break if i <= 0


There's an issue with this line:

It should be printing the next iteration if the number i is odd, starting with 18:



okay but what's the issue though with the line?


Okay, let's take it right from the top.

This was the question

i = 20
___ ___
  i -= 1
  print "#{i}"
  ___ if i <= 0

In your code, you even distorted the original question.

The dashes gives us an idea of what we should really be doing. So, we will comfortably be adding just 4 keywords and your condition to check if even, that will do the whole magic.

i = 20 
loop do
  i -=1 #ensure it counts as 20,19,18,17,16...
  next if i % 2 == 1 #this checks if even
  print "#{i}"
  break if i <= 0

Apologies for my initial mix.

I hope this now resolves. :slight_smile:


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