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 trialPhilip Vaarskov
2,252 PointsUsing the loop construct, add the current value of number to the numbers array. Inside of the loop, add 1 to the number
What am i doing wrong?
numbers = []
number = 0
# write your loop here
loop do
number =+ 1
number.push(numbers)
if numbers == 3
break
end
end
Bummer! NoMethodError: undefined method `push' for 0:Fixnum Did you mean? puts
2 Answers
Steve Hunter
57,712 PointsHi Philip,
You want to push
onto the numbers
array; pass in number
as the method argument. Your error is saying that number
doesn't have a push
method - that's because it isn't an array; numbers
is!
numbers.push(number)
Also, you want to do that first in the loop, before you increment number
otherwise your array will never have the value zero in it. The question wants add the current value of number to the numbers array - this includes the initial zero value. Next up; to increment, use +=
rather than =+
.
That all looks like:
loop do
numbers.push(number)
number += 1
break if number == 3
end
Let me know how you get on.
Steve.
Philip Vaarskov
2,252 PointsThank you so much, it worked :)