Introduction to Ruby, Next iterator trouble


Hi everybody!
I've been doing the Introduction to Ruby course and i'm stuck at the "next" iterator. i think i get the code straight but the console will show all impairs numbers when it should show the fairs...
the console shows this: 191715131197531-1nil

If you have any idea as to why my code won't work, it would be great!
Thank you in advance!

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


Without seeing the instructions it is impossible to know what is expected. Did you read the template when writing this topic? It specifically asks for a link to the exercise. Please furnish us with one. Thanks.

On a lesson note, is your list supposed to be comprised of odd, or even numbers?


yeah sorry, i tried to post the link but obviously it didn't work out
here it is:

my list is supposed to show even numbers, but i only get odd ones, which is strange because i followed the instructions... which is why i don't understand what when wrong cause my code looks exactly like the one in the instruction!

thanks for your help!!


Try either,

next if i % 2


next if i % 2 != 0

to filter even numbers.


the != worked!!
thank you so much!


Thank you for your response!


a fair number is obviously a even number according to me!! I don't know why
but i was absolutely convinced that was the way to say it! sorry, english
isn't my native language!!
thank you very much for the explanation, i'm getting started with Ruby and
i was mistaken about the meaning of %


In technical terms, oddness and evenness are known as odd parity and even parity. We test parity by checking for divisibility by 2, and modulo % lets us examine the remainder of the division.

The same applies to divisibility in general. When x % n is zero, x is evenly divisible by n.

15 % 5 == 15 % 3 == 0


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