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 trialMatthew Mercaldi
1,316 Pointswhy my loop do statement is not working
I am at the part where I need to Add numbers to an array until the number of items in the array are greater than 3. I found the right answer but I am curious why my answer does not work:
numbers = []
number = 0
loop do
number = number + 1
numbers.push(number)
if numbers.count > 3
break
end
end
2 Answers
Michael Hess
24,512 PointsHi Matthew,
You may want to use numbers.size to check if the array is equal to 3. If the array size is 3 then the break statement inside the if-statement will be executed.
loop do
number = number + 1
numbers.push(number)
if numbers.size == 3
break
end
end
If you have any further questions feel free to ask! Hope this helps!
Matthew Mercaldi
1,316 Pointsnumbers.size worked
Thanks
Michael Hess
24,512 PointsMy pleasure -- glad I could help!