Stuck on Exercise 9


#1

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.


#2

Supposed to print out numbers 18-0 , this is what I have so far

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


#3

Hey fstagi

In your next statement, your trying to check if the remainder of dividing by 1 is equal to 2 (i % 1 == 2), but it will always be false since every number divided by 1 will give 0 as a remaining value.

The modulo operator, %, will take the number in front and divide it by the number behind, and return what ever value that remains. So given (i % n), the result will always be between 0 and n-1.

In the example they show, they use "next if i % 2 == 0" to check if the number is even, since every even number (e.g. 2, 4, 68), will all divide by 2 and return 0.
To check for odd numbers, you only need to check if the remainder value of dividing by 2 is not 0, making the command "next if i % 2 != 0"


#4

Hi

I'm also stuck on this one and can't seem to get past it, I either get a syntax error or that is can't print number 1 to 18.

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

What have I done wrong? when I need to take out the for i in 1..20 that's when I get the 1 to 18 error, have I reversed something or missed a step? Thanks!

Chrystal


#5

@cvondran

Remove this line.


#6

I removed the line so this is the code now
i = 20
loop do
i -= 1
next if i % 2 == 0
print "#{i}"
break if i <= 0
end
and now I get this

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


#7

@cvondran

i should have a remainder of 1 not 0


#8

Thank you! it worked!
I see much reviewing and reading in my future :blush:


#9

@distrattos Thank you, I was stuck on the same problem. :slight_smile:


#10

## Thank you soooo much!!!


#11

Yeah works for me! =) thanks