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

for loop

My program keep giving me a syntax error, what am i doing wrong?

script.js
for(i=0; 4<i<156;i += 1;){

}
console.log(i);

so it should be i=1; 4<i<156; ?

Steven Parker
Steven Parker
231,275 Points

I added a comment to my answer to elaborate.

3 Answers

Steven Parker
Steven Parker
231,275 Points

The first clause of the loop establishes the starting condition. That's where the "4" should go.

The second clause should contain a single comparison expression to determine when the loop will end.

so i should be initialized to 4?, like for(var i = 4; 4<i<156; i+=1){} Also what goes inside the set of braces?

Steven Parker
Steven Parker
231,275 Points

for (i=4; :point_left: that's the way to set the initial condition
      i <= 156; :point_left: that's a single comparison, and you want to include the last value this time

I think you'll have it after that, but don't put a third semicolon after the last clause.

Are you missing the term var in front of i=0 ? Even so, I think your loop will never run because you have it set to run when i is between 4 and 156; but you have initialized i to 1. How will i ever get larger than 4?

You are very confused. Please review your basic instructions for the for loop. The code that you want to repeat goes inside the braces, not after the closing brace. Even if you set var i=4, the loop will never run because your conditions state that i must be greater than 4 to start the loop. Try var i=5.