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

How do I solve this

I dont how to solve this!?

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

2 Answers

Quentin Durantay
Quentin Durantay
17,880 Points

Hi Micheal,

The issue here is that you used the "var" keyword too many times. You just need it when you initialize a variable the first time, so that the memory knows that it has to store a new variable. When you call this variable again, the variable object is already in memory, so there's no need to initialize it again, as the memory will think you want to create a new variable with the same name, instead of updating its value.

Also, you omitted a semicolon after the statement in the loop, so that JavaScript struggles to understand where do the statement ends. The same way, in a for loop, the semicolon is used to separate initialization, condition and final expression of the counter variable (here i). You don't need it after the final expression where you increment the variable i by 1.

Try this instead :

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

I hope my answer is clear, as it is the first time I answer on the Treehouse forum :)

thanks!