Unknown infinite loop


#1



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


My code keeps making an infinite loop.


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


#2

there should be an 'if' after your next


#3

I tried that, but then it creates an infinite loop.


#4

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

If you refactor your code to do this(move your increment before your next statement), it will output =>19171614131110875421

However this isn't what the lesson calls for. It wants even numbers printed

Try next if i % 2 == 0


#5

Thank you! I'll try that next time.


#6

Add a line to your loop before your print statement. Use the next keyword so that you skip to the next iteration if the number i is odd.

That means using 2 to test parity.

break if i < 1

will work nicely, as well.


#7

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