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 trialCody Bouscaren
3,070 PointsRuby Loops Confusion
So, I realize that my code is pretty far off, but I'm confused here.
1) I can't seem to add the value of 'number' to the numbers array without erasing the array and setting the value to the '0'.
2) I'm pretty confused about what the question is asking me to do--and I think my code reflects this!
Any help would be greatly appreciated!
numbers = []
number = 0
numbers = number
loop do
number + 1
if numbers > 3
break
end
end
1 Answer
Tim Knight
28,888 PointsHi Cody,
I haven't gone through this particular course so I'm not sure what concepts they've taught that led up to this, but I think I can give you enough information to help you solve your issue.
First you're wanting to append the value to the array with each iteration of the loop so instead of numbers = number
on the outside of the loop you'll want to put numbers << number
inside of the loop. The << method is a shorthand way of appending an item to a list in Ruby (shorthand of the push method). Next you want to take the current value of number (which is 0) and add one to it. You would do this with +=
so you're saying "add this value to the current value and make the variable equal that". Since it wants you to end the loop at 3 items you'll want to do >=
instead of just >
so it'll end right at 3 instead of just items greater than.
With all of that you should have something that looks like this:
numbers = []
number = 0
loop do
numbers << number
number += 1
break if numbers.size >= 3
end