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 trial

JavaScript JavaScript Loops, Arrays and Objects Simplify Repetitive Tasks with Loops Create a for Loop

Michael Hickling
Michael Hickling
1,261 Points

I 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);
}

script.js
var numbers = "";

for (var i = 4; i <= 156; i += 1) {
    numbers += i;
    console.log(numbers);  
}

2 Answers

Michael Hickling
Michael Hickling
1,261 Points

Damn, I'm an idiot. Thank you so much!

Dave StSomeWhere
Dave StSomeWhere
19,870 Points

Pitor'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

:tropical_drink: :palm_tree:

Michael Hickling
Michael Hickling
1,261 Points

Thank you for your feedback! I appreciate it!