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

I don't know what I am doing wrong...

I need help with configuring a for loop I keep getting a syntax error, however I can't spot it, and I don't know what I am doing wrong. Any help with this issue will be much appreciated. Thanks!

script.js
var count = "";

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

2 Answers

Some hints:

  • Your console.log should be inside your loop so all values are logged
  • It will be easier to log i than to create another variable
  • i+= 1 increments so you don't need to try to do it again in the loop
for (var i = 4; i < 157; i++) {
  console.log(i);
}

You need the console.log() in the loop. Variable i shouldn't be a string. Assigning a float or integer to a string variable isn't allowed unless the variable is parsed (turned into a different type, like string to integer: '4' -> 4). No need for more than one variable in this situation. It shouldn't be "i <= 156", this would make the loop run when i is equal to 156; this would log numbers 4-157, instead of 4-156. Make sure that you have the correct spacing and tabs.