Ruby Ruby Loops Ruby Loops The Ruby Loop

How do I complete this code?

Racking my brains here. Appreciate any help.

loop.rb
numbers = []

number = 0

# write your loop here
loop do
  print "Enter a number. "
  numbers = number[]
  number = gets.chomp.to_i

  if 
    number = (+ 1, 0)
  elsif 
    number = (+ 1, 1)
  else
    number = (+ 1, 2)
      break
   end
end

1 Answer

Jennifer Nordell
STAFF
Jennifer Nordell
Treehouse Staff

Hi there! I can see that you've worked on this quite a bit. I would advise going back and reviewing arrays and how we add and manipulate them. But here's my solution and I'll walk you through it.

numbers = []

number = 0

# write your loop here

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

The first part is set up for us by Treehouse. It contains an empty array named numbers and a variable named number with an initial value of 0. We start our loop. The first thing we do is add the number to the numbers array. So at this point 0 is put into the array. Then we increment the number variable to 1. Next we check to make sure we don't have 3 or more numbers in the array. If we do, we break the loop. The end result will be that your numbers array will contain the values 0, 1, and 2.

Hope this helps! :sparkles:

By the way, I think I might of seen this method of using operators before:

+ 1 2

Although that will cause an error in Ruby, in languages like Lisp it is valid.

Just a little note in case Marguerite Holden has learned something like Lisp before :)

Thanks for the detailed answer. I'll go over everything until I understand it. Thanks for the advice and quick response!