9. Next! bugged? (next if for odd numbers)


#1

Is #9 bugged? I follow the instructions and it keeps giving me an error. The bold text is my entry.

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


#2

Although the problem doesn't mention this. It works with .odd?

i = 20
loop do
i -= 1
next if i.odd?
print i
break if i <= 0
end


#3

try this
i = 20
loop do
i -= 1
next if i % 2 == 1
print "#{i}"
break if i <= 0
end
what are being asked to do is to print out odd numbers i.e 15,17 e.t.c the code will break whenever you get an odd number i.e 18,16 e.t.c. i -= 1 is also the same thing as i = i - 1 ( 20-1) because i is equals to 20. and even number divided by 2 will give you a remainder of zero while an odd number divided by an even number will give you a remainder. So basically you are telling the code to print out integers that when divided by an even number gives you a remainder of 1 .... I hope you understand


#4

Ah thankyou, that makes a lot of sense.


#5

Sure its no problem...


#6

That's simply because the instructions ask for odd numbers, not the non-multiples of 3. It would work perfectly with a % 2 operation.

EDIT: nevermind, didn't read the thread fully, my answer was unnecessary. Please use the "solution" button so that people know a thread is solved ^^, thanks.


#7

Thank you arjofocolovi, I didn't realize I did not click the solution button.


#8

I am having the same problem. exept when i do 18..0 it wont print out but when do 0..18 it will print but it says i need to print 18..0 PLEASE HELP


#9

Post your code. Also I don't believe the excercise says to use: .. to reverse the order.


#10

for i in 18..0
next if i.odd?
print i
end

here. I dont know what the problem is.


#11

I suggest going back and re-reading the problem. It never says to use: .. in this example. Also using .odd? is also not stated even though it can work. Go back and take your time.

The point of the excercise isnt just to complete it but to understand the logic behind what they are teaching and showing you. So, take it slow and read the details.


#12

For those of you who need assistance,
I did the following and it worked.

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

To skip the odd numbers, you can just put
next if !(i%2 == 0)


#13

Hi Guys, I think this code makes much more sense and we don't have to resort to workarounds such as starting at 20, here it is:

i = 18
loop do
break if i < 0
if i % 2 == 0
print i
i -= 1
next
else
i -= 1
end
end


#14

Hello all, I am trying to understand the code in exercise 9, but it makes no sense to me at all. Please can you all help:

>i-=1   (why is there a negative before the equals sign and it then says i is equal to 1?) 
> next if !(i%2 == 0) (why is there an exclamation mark outside a brackets)?
>  print "#{i}"  (is # always required)

Is there a stage that i am missing to understand these equations?

Thank you BC


#15

Great you code help me to understand better. Thank you!


#16

Thanks this is really helpful


#17

I realize that I am VERY late with this, but when you are in the forums, try not to post answers. The point of the forums is to help people learn, and from my experience, most people don't learn just by watching (Or in this case, copying and pasting). Again, I realize I am very late with this, and I am reviving an old topic, but still, try not to post answers. Thanks :smiley:


#18

this solution helped me since I had the prior
i = 20
loop do
i -= 1
next i % 2 == 1
print "#{i}"
break if i <= 0
end

and it still wouldn't work so thanks!


#19

or you could use the != for 0 instead of ==1

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


#20

Hey, excuse me. But when you use the "next if..." in a loop you dont have to use the "for in"? I dont understand very well that problem.