Lesson 9 in ruby for loops and iterators is confusing help


#1



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


(ruby):7: syntax error, unexpected $end, expecting keyword_end


i = 20

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


#2

You are supposed to only use loop do. Mixing two loops together (loop do and for i in) is bad.

In the next statement, your condition has to check if i is odd (how do you check if a number is odd? :wink:)

You also have to keep subtracting 1 from i each iteration so that the break conditition can be met and the loop hence exited (so that the loop does not become an infinite loop).


#3

thanks, I'm sorry \i didn't see this sooner :slight_smile:


#4

I'm not sure why this isn't working. Can you give me any hints?

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

Thanks in advanced.


#5

@joe3863,
First of all
you are using the modulo-operator
http://stackoverflow.com/questions/3517238/what-does-the-operator-do-in-ruby-in-n-2


#6

I understand now. I had the wrong remainder for odd numbers divided by 2. Thanks!