Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

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
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

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!