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 trialkevinthecoder
10,791 PointsUnable to complete Loop Challenge (very confusing)
I am unable to complete this loop challenge despite several tries. I am going to come right out and say it: The way the challenge is EXACTLY written is VERY CONFUSING. Notice that I said the word EXACTLY. The challenge statement itself can be interpreted about 4 different ways. I have tried various combinations.
Let's start with the very first statement 'using the loop construct'. I am interpreting that to mean that I am immediately adding the current value of number to the numbers array and that will immediately be inside of a loop (yes, I tried it outside of the loop just to make sure). What aggravates me is that it says "Inside of the loop"; to me, that tells me that I should be adding the current value of number to the numbers array OUTSIDE and before the loop (if I am reading the statement chronologically, in order etc). See where I am going with this? Very confusing.
Yes, I will admit it as I have admitted it before on the forum here...I am anal and can get tunnel vision and I tend to look at things literally and directly, whatever you want to call it.
Numbers is an array and so I assumed that we use brackets []. Notice that it says to add the number one(1) INSIDE of the loop and not outside; thus that's how I wrote the code. I thought about putting it outside too. The number of Ends matches the number of loops & If statements (total of 2) so we match and we are OK there.
And so I am totally lost on this.....help is appreciated. Thanks.
numbers = []
number = 0
loop do
numbers[].push(number)
if numbers[] > 3
break
end
number=number+1
end
4 Answers
Daniel Frehner
3,027 PointsHey kevin ive noticed a few of the challenges need a little more clarification. Maybe some sort of hint system would work as well. I've entered this code and it passed:
numbers = []
number = 0
loop do
numbers.push(number)
number += 1
if numbers.length > 2
break
end
end
Im a student as well and still learning, but i believe the reason yours didn't pass was that you had "number = number + 1" in the wrong area. it needs to be before the "if - break" statement or else you create an infinite loop. Or possibly because you broke at greater than three so it would actually break when it hit 4 and not 3. maybe someone else with a little more knowledge could say it a little better.
kevinthecoder
10,791 PointsThanks for your help/advice, Daniel.
Part of it is indeed the way that I actually learn with my own brain, of course. Part of it is the way the Challenge statement/sentence is written. I think that some of these problems that I am having will indeed go away when I have 'human' interaction in a real environment; I'm working on that. Based on suggestions from others locally that I have met so far (i.e. programmers), it's best to 'pair' with someone locally, such as a volunteer. I haven't done this yet and am still exploring options. At some point soon, I will eventually need the human interaction, face time etc if I am going to really learn this web development stuff.
Kevin Mulhern
20,374 PointsHey Kevin, numbers isn't exactly an array. Its a variable that points to an array. The brackets don't need to be included with the numbers variable unless you are looking for a specific index in the array eg. numbers[0] for the first element.
This is the code that will pass the challenge:
numbers = []
number = 0
# write your loop here
loop do
numbers.push(number)
number += 1
if numbers.length >= 3
break
end
end
Kevin Mulhern
20,374 Pointsalso you need to increment the number by one inside the loop because after each iteration the value will go up, until it eventually equals 3 and that will stop the loop.
kevinthecoder
10,791 PointsThanks, Kevin.