Help with exercise NEXT; 5.9


#1



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

<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
end


#2

There's an issue with this line:

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

181614121086420

#3

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


#4

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

This was the question

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

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
end

Apologies for my initial mix.

I hope this now resolves. :slight_smile:


#5

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