Can somebody help me with these( ruby )


#1


https://www.codecademy.com/courses/ruby-beginner-en-XYcN1/2/2?curriculum_id=5059f8619189a5000201fbcb


so I am confused on how to print out the output and skip the odd numbers

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


for i in 1..5
  next if i % 2 == 0
  print i
end
This is the 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.

Use the example above for help, but remember that the example above skips even numbers.

This is my code:
 
i = 20

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


#2

Okay everything is perfect, except for the operator you use in your next if statement. What operator tells you the REMAINDER of dividing two numbers? It's used a lot in programming, so any language you write in uses it plenty. Instead of i divided by 2, you want to write i modulus 2. :slight_smile:


#3

let me try first and I hope some feedback or response from the community


#4

so kyleaw the error says:
It looks like your code doesn't print out the numbers from 18 - 0

even using this code:

i = 20

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


#5

The problem with this is that you are skipping over the even numbers. If the remainder of a number divided by two is zero, what does that tell you? How can you skip over the odd numbers instead?


#6

thats is why I need help so someone can explain to me how to do it. Explain logically


#7

I'm using prompt questions because I will not just give you an answer that you don't understand. You're really close though.

This is the line that is skipping over the even numbers. In english, this line basically says, "skip to the next step if the remainder of i divided by two is zero." Even simpler, numbers that are divided by 2 and equal zero are commonly referred to as even numbers. In a nutshell, you're skipping over the even numbers with this line. If a number that is ODD is divided by two, what is the remainder? That number, (the remainder of an odd number divided by two), should be in place of zero. :slight_smile: I hope this helps!


#8

ok gotchu

next if i % 2 == 1


#9

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