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 trialReid Larson
12,711 PointsCode wasn't working?
I wasn't getting my code to work. I finally did after removing the ' ; ' after the i+=1 in the for loop. Why would that be the case?
2 Answers
Stephen Van Delinder
21,457 PointsReid,
Most likely that would cause a syntax error for an unexpected token. Punctuation is very important in control structures like loops. For loops in JS have three statements, one to initialize a variable, one to evaluate a variable and one to change or manipulate a variable on each loop iteration. These statements are separated by semi-colons.
for ( initialize ; evaluate ; manipulate ) {
...code code code...
}
Within a for loop, semi-colons can also indicate when a statement has been omitted. For example if your variable is already initialized prior to the loop.
for (; evaluate ; manipulate ) {
...code code code...
}
Following this logic, a semicolon on the end of the third statement will lead JS to believe that a fourth statement has been omitted, and it will error out because JS only accepts 3 statements in for loops.
I hope this helps!
Reid Larson
12,711 PointsOk, that explains it. I assumed that was the case, tried watching the part of the video where it's added but didn't hear that. If it was said elsewhere in the video that was my fault but it may be worth explaining to those who are new? Either way, thanks a lot for the explanation!
Stephen Van Delinder
21,457 PointsI don't remember it being covered in the video, but there's a full explanation here.
Andrew Jensen-Battaglia
10,051 PointsAndrew Jensen-Battaglia
10,051 PointsThat's just the syntax rule. In a for loop you have a semicolon after the first and second statement in the ( ) but not the last.