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 trialJoshua Shroy
9,943 PointsRuby 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.
8 Answers
Dan Johnson
40,533 PointsThe 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.
John Steer-Fowler
Courses Plus Student 11,734 Points+1 for this answer.
Joshua Shroy
9,943 PointsThanks Dan. I'm glad it wasn't my misunderstanding but this challenge definitely through me in for a loop ;)
Hyunsoo Choi
1,255 PointsThis 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
Robert Mion
5,973 PointsHi 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
Joshua Shroy
9,943 PointsGreat code! It definitely appears more efficient. I especially like how you implemented the "while" loop.
Thank for sharing!
Alexandre Cordani
1,571 Pointshello. 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
Annie Scott
27,613 Pointsnumbers = []
number = 0
write your loop here
loop do numbers.push(number) number + 1 if numbers.length >= 3 break end end
Marco Fish
Courses Plus Student 1,903 Pointsloop 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
6,669 PointsThe solution:
numbers = []
number = 0
loop do
number += 1
numbers.push(number)
if numbers.length >= 3
break
end
end
Nasser Sanou
6,895 PointsHere is the Solution without using while loop.
numbers = []
number = 0
loop do number +=1 numbers.push(number) if numbers.length >= 3 break end end
Jason Anello
Courses Plus Student 94,610 PointsJason Anello
Courses Plus Student 94,610 PointsJason Seifer ,
The challenge instructions don't seem to match what the challenge is checking for.