The Next if method is really tough for me can anyone help me?


#1

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.

Use the example above for help, but remember that the example above skips even numbers.

So heres the example it gave.....for i in 1..5
next if i % 2 == 0
print i
end

Here was my answer....i = 18
loop do
i -= 1
next if i % 3 == 0
print "#{i}"
break if i == 0
end....

Its not skipping the odd numbers can anyone help me?


#2

The module(%) tests for remainders. use i % 2 == 0 instead of i % 3 == 0. If you wanted to capture an odd number you would use i % 2 == 1.

I'm not that familiar with the ruby syntax but I hope that fixes your problem.


#3

Hi edwin thanks for the response i tried what you told me an it tells me this "It looks like your loop doesn't print out the numbers 18 to 0."


#4

i = 18
loop do
    if i % 2 == 0
        print "#{i}"
        i -= 1
    else
        i -= 1
    break if i == 0
    end
end

Here is what I came up with. It may have wanted you to print out 18 as well.. With your program the first number printed would be 16 since i is subtracted one immediately before the if operation is done. Hope it works :slightly_smiling: .

Oh, and if it wants the 0 to be printed too, just change break if i == 0 to break if i < 0