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 trialMichael Hickling
1,261 PointsI can't figure out this for loop question
Its' asking me to write to the console via a for loop starting at 4 and ending at 156. When I run this same code on my local machine, it shows it starts at 4 and ends at 156. But when I submit this code in the question, it says it's wrong: "your loop should start at 4 and end at 156" I don't get it?
var numbers = "";
for (var i = 4; i <= 156; i += 1) {
numbers += i;
console.log(numbers);
}
var numbers = "";
for (var i = 4; i <= 156; i += 1) {
numbers += i;
console.log(numbers);
}
2 Answers
Piotr Manczak
Front End Web Development Techdegree Graduate 29,363 PointsYou don't need var number. It should be like that:
for (var i=4; I <157; i += 1) { console.log(i); }
Dave StSomeWhere
19,870 PointsPitor's answer is the easy/straightforward solution - since i
contains the desired value.
Now, if you wanted to use your numbers variable, You would just need to change the numbers += i line to numbers = i
- which makes you say why use numbers, and just console.log the value i - it would work though...
Your code produces concatenated strings (you defined numbers as a string) of the loop - like below:
"456789101112131415161718192021"
"45678910111213141516171819202122"
"4567891011121314151617181920212223"0
Michael Hickling
1,261 PointsThank you for your feedback! I appreciate it!
Michael Hickling
1,261 PointsMichael Hickling
1,261 PointsDamn, I'm an idiot. Thank you so much!