Introduction to Ruby, Next iterator trouble


#1



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
end


#2

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?


#3

yeah sorry, i tried to post the link but obviously it didn't work out
here it is:
https://www.codecademy.com/fr/courses/ruby-beginner-fr-FR-7rcog/2/2?curriculum_id=535e93005d81962043000001

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!!


#4

Try either,

next if i % 2

or

next if i % 2 != 0

to filter even numbers.


#5

great!!
the != worked!!
thank you so much!


#7

HI!
Thank you for your response!
a


#8

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 %


#9

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

#10

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