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

the ruby loop challenge 1

I'm confused as to what I am doing wrong. Please help! thanks

loop.rb
numbers = []

number = 0

# write your loop here
loop do 
  numbers << number
   number += 1
  if numbers > 3
    break
  end
end

1 Answer

1) If you want to stop your loop when your number >= 3, you should do it:

    numbers = []

    number = 0

    loop do

       numbers << number

       number += 1 

       break if number >= 3

    end

2) If you want to stop your loop when your numbers.length >= 3, you should do it:

    numbers = []

    number = 0

    loop do

       numbers << number

       number += 1 

       break if numbers.length >= 3

    end

Both codes work in this exercise, because when number.length >= 3 or number >= 3, the numbers array has more than 3 items.

thanks that was really helpful

Shaii Ong
Shaii Ong
3,078 Points

I had the same problem. This was helpful! Thank you, :)