I don't understand how to do 9. Next!


#1

This is what it gives me:
i = 20
loop do
i -= 1
print "#{i}"
break if i <= 0
end

Instructions:
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.

Please HELP!!!


#2

The condtional will resemble the break condition:

i -= 1
next if i % 2
print "#{i}"

#3

You know about the modulo operator correct? " % ".

Just use that :smiley:


next if i % 2 == 1

If you have any questions, it mentions this in the instructions


#4

No matter what i try it comes up with

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


#5

My bad, using JS style conditional, if ( i % 2 ) will be true in JavaScript.

This is tested and runs as expected:

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

The output will be on one line:

181614121086420nil

#6

OMG, thank you so much. I got stuck on this and got really frustrated.