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

Ruby Loops Challenge 1

I am stuck with the first Ruby Loops challenge. Unsure what I am doing wrong. Seems quite simple.

The challenge question:

"Using the loop construct, add the current value of number to the numbers array. Inside of the loop, add 1 to the number variable. If the numbers array has more than 3 items, use the break keyword to exit the loop."

My code:

numbers = []

number = 0

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

I get the error message

Bummer! The length of the numbers array was not 3.

Jason Seifer ,

The challenge instructions don't seem to match what the challenge is checking for.

8 Answers

Dan Johnson
Dan Johnson
40,532 Points

The wording is a bit misleading on this question. It actually wants you to break on three items rather than when it exceeds it.

Change to:

if numbers.length >= 3

and it should pass.

Thanks Dan. I'm glad it wasn't my misunderstanding but this challenge definitely through me in for a loop ;)

Hyunsoo Choi
Hyunsoo Choi
1,255 Points

This code passed as well. I'm not sure if I did it right though...I'm wondering why it passed...

numbers = []

number = 0

write your loop here

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

Hi Joshua,

I'm a rubie (my term for Ruby newbie) and wanted to offer my own concocted solution to the challenge:

numbers = Array.new

number = 0

while numbers.length < 3
    numbers.push(number)
    number += 1
end

puts numbers

I started the same as you, creating a new Array object and storing it to a new variable numbers, then initialized a number variable and set it to the integer 0.

Next, I used a while-loop with the condition to check: that the total values in the numbers array be less than 3.

As long as this evaluates to 'true', the while-loop will tack on the current value of number to the numbers array, then increment number by 1, and repeat.

Lastly, the code will print the contents of the numbers array to the console:

0 1 2

Great code! It definitely appears more efficient. I especially like how you implemented the "while" loop.

Thank for sharing!

hello. I try this:

numbers = []

number = 0

write your loop here

loop do
  puts " tapez un nombre de 0 à 9"
  number= gets.chomp
numbers.push(number)
  break if numbers.length == 3
end

numbers = []

number = 0

write your loop here

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

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

This through me for a loop it took me thirty minutes to figure this out

Pedro Henrique Knoll Giacometo
Pedro Henrique Knoll Giacometo
6,669 Points

The solution:

numbers = []

number = 0

loop do
  number += 1
  numbers.push(number)
  if numbers.length >= 3
  break 
    end
end
Nasser Sanou
Nasser Sanou
6,895 Points

Here is the Solution without using while loop.

numbers = []

number = 0

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