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 trialMathew Yangang
4,433 Pointsfor loop
My program keep giving me a syntax error, what am i doing wrong?
for(i=0; 4<i<156;i += 1;){
}
console.log(i);
Steven Parker
231,275 PointsI added a comment to my answer to elaborate.
3 Answers
Steven Parker
231,275 PointsThe 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.
Mathew Yangang
4,433 Pointsso 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
231,275 Pointsfor (i=4; that's the way to set the initial condition
   i <= 156; 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.
Bob Kaplan Jr
8,984 PointsAre 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?
Bob Kaplan Jr
8,984 PointsYou 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.
Mathew Yangang
4,433 PointsMathew Yangang
4,433 Pointsso it should be i=1; 4<i<156; ?