Ruby Ruby Loops Ruby Loops The Ruby Loop

Lindsay Barrett
Lindsay Barrett
Python Web Development Techdegree Student 7,357 Points

Ruby Loops

How can I solve this problem?

loop.rb
numbers = []

number = 0

loop do
  number + 1
  if numbers === numbers[2]
    break
  end
end

numbers.push(number)

2 Answers

You are doing a great job! However, you can't just say "number + 1", or Ruby will think you want the result of the number plus one. Instead, you have to do "number += 1". Second, there is no "===" operator in Ruby. That's only for JavaScript. You'll have to use the "==" operator. Lastly, I don't know why you did "numbers == numbers[2]", but you should get the length, like this: "numbers.count" or you also could do: "numbers.length", and see if it is equal to three. The code:

numbers = []

number = 0

# write your loop here
loop do
  number += 1
  numbers.push(number)
  if numbers.count == 3
    break
  end
end

Hope that helps! :dizzy:

Close. Try this:

loop do
  numbers.push(number)
  number += 1 
  if numbers.length >= 3
    break
  end
end