Loops and Iterators: Lesson #9 !Help What Am I Doing Wrong!


#1



I can't seem to get past #9 (Next!) Help what am I doing wrong!


Oops, try again. Make sure to replace the __s with the appropriate keywords!


Code:
i = 20
for i in 1..20
next if i % 1==0
loop do
i -= 1
print i
break if 1 <= 0
end


#2

I can't get past #9 too!!!!


#3

Did you ever figure this out? It took me a bit but I can help you if you want.


#4

No I haven't been able to figure it out. Please help!! Thanks bunches!


#5

Okay so I'm gonna try to lead you to the answer without just giving it to you.

So the problem here is that you know what the code does, but you need to single out the odd numbers. Think about what each line does and when the next if is going to skip the odd number. You are on the right track with division by 2, but you need a way to make sure the variable i is equal to the odd number so it will skip.

So if you are familiar with order of operations, you simply need to make sure that the conditions on the left match the conditions on the right when the i variable becomes an odd number.

Think about it and if you still can't get it let me know. You're kind of doing things backwards but once you get it you'll probably feel silly like I did.


#6

I don't get it!! I already fill silly!! Please lay it out for me!! TIA


#7

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

This will pass the test for you. Quickly explained, we want it to skip odds and only print evens. So, if we set up

next if i % 2 !=0

so let's say the program starts with 1. 1 / 2 = 0 since 2 cannot go evenly into 1. so since i !=0 it skips 1. For the next number, 2, we have 2 / 2 = 1. Since i !=0 (because i = 1) it prints 2.


#8

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