Loops & Iterators -- Next!


#1

Only prints the number 45
Why does it do that?

i = 45
    loop do
        break if i <= -21
                next if i % 2 == 0
            puts i
            i -= 1
        end

#2

The code results in an infinite loop due to the placement of the control. There is excessive indentation structure, as well, but it shouldn't matter to Ruby. It's the order that is messed up, here.

I wrestled with your code and came up with this...

i = 45
loop do
    i -= 1
    next if i % 2 == 0
    puts i
    break if i <= -21
end

which ran to completion as expected. However, the lesson checker tossed up an error message:

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

That would suggest starting with i = 20 or i = 19 so that the first even number printed is 18. You will also need to use print, not puts so it all prints on one line with no white space. Set your break condition to if i < 1