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 trialDenis Chernik
Courses Plus Student 5,351 PointsLoops. Exercise one.
Assignment says: "If the numbers array has more than 3 items, use the break keyword to exit the loop." But it gives an error if there are more than 3 items when loop breaks. So it should say: "If the numbers array has 3 items, use the break keyword to exit the loop." (Exactly 3).
4 Answers
kabir k
Courses Plus Student 18,036 PointsThe challenge is correct the way it is worded. What it is saying is:
check to see if the
numbers.length > 2
OR
numbers.length == 3
If it is greater than 2 or it is equal to 3, it means the numbers array has more than 3 items. Since array index starts from 0.
So you have: [ 0, 1, 2 ] (representing the 3 items to meet the condition for the loop to exit)
David Clausen
11,403 PointsThis is incorrect. Length equals total items in it. If an array has more than 3 items and array.length == 3 than the array has 3 items [0,1,2], if the length is >2 when it exits that means it has 3 or more items in it.
The question says "If the numbers array has more than 3 items, use the break keyword to exit the loop."
When the array exceeds 3 items than break. The condition that its test though makes it where it breaks at 3 items not more than three.
Again length/count return total items, not index number. Index does not equate to length. Index is its position, length/count is its total items.
"Test" string has a length of 4 characters, but its index is 0-3. 0-"T" 1-"e" 2-"s" 3-"t"
[0,1,2,3,4,5] has a length of 6. Test it out.
Benjamin Ranard
2,302 Pointsagreed
gilroncharles
8,978 Pointsnumbers = []
number = 0
# write your loop here
loop do
if numbers.length < 3
numbers.push(number)
else
break
end
end
The question is asking you to push the value contained within the number variable to the numbers array if the length of the array is < 3. My syntax basically says: if the length of the array(numbers.length) is less than 3 insert number = 0 into the numbers array. The loop continues to add the number = 0 into the numbers array until the array is filled with number = 0, number = 0, number = 0, number = 0. Once numbers.length is greater than 3 then the else portion executes break/stop the loop. Hopefully i helped :)
David Clausen
11,403 PointsIf you walk through your code it break when you have THREE items in the array. The question ask you to break "If the numbers array has more than 3 items" Your arrray after its breaks has three items. The question is asking you to break if it has MORE than three. 3 is not more than 3, it is exactly three and no more than 3. The question is in direct conflict with evaluation.
Your code adds the third item. You then check to see if it is LESS than three. When your array is exactly 3, then it is not less than 3, so it returns false. Therefore it moves to your else and breaks when the total length is 3.
Try it out in IRB "=> 3 < 3" will return false. Its not your fault the thing the checks the code is simply written wrong.
gilroncharles
8,978 PointsGreat ^ , i didn't even notice. Sometimes when 1 + 1 always = 2, you don't double check and realize that you entered 1+1 = 10 instead.
Bill Talkington
11,840 PointsBill Talkington
11,840 PointsAgreed!