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 trialUnsubscribed User
3,436 PointsError using `for` loop in JavaScript
The challenge is asking to create a for loop that logs number from 4-156 and log the numbers using console.log
.
I wrote the code attached but it keeps giving me an error. When I used the console to figure out the error I got this error message Uncaught SyntaxError: Unexpected token +=
. Can you point me to the right direction?
for (var i = 4, i = 156, i += 1) {
}
console.log (i);
4 Answers
Steven Parker
231,269 PointsYou have a few issues here:
- the 3 clauses of a for loop should be separated by semicolons, not commas
- a single "=" symbol is an assignment, not a comparison operator
- for the conditional (second) clause you probably meant to write "
i <= 156
" - if you want to log each number, the console.log statement needs to be inside the loop block
Evgeniia Mas
4,452 PointsSure!!!! I mentioned your name at very beginning of my post as your answer was first and than when I posted my additional notes I saw extra answer from Krishna Pratap Chouhan.
Krishna Pratap Chouhan
15,203 Pointsfor (var i = 4; i <= 156; i += 1) {
console.log (i);
}
Three problems:
replace ,
by ;
change =156
to <=156
bring console.log inside the loop
Evgeniia Mas
4,452 PointsI agree with Steven and would like to add that in your variant there are's no action which must be executed during the loop as the {} are empty.
Syntax of for loop is such for (i = 0; i < cars.length; i++) { text += cars[i] + "<br>"; } (according w3s example here https://www.w3schools.com/js/js_loop_for.asp) You can miss the conditions of loop, but semicolon should be. for (; i < 5; i++) { alert( i ); // 0, 1, 2 }
Even a loop for (;;) { } can be. But it is INFINITE, don't try last one)).
So in your case use for (i = 4; i < 156; i++) { console.log (i); } This loop will print in console numbers from 4 to 155, if you need to include 156, than write i < 157 at the second position.
Evgeniia Mas
4,452 Pointsor i<=156; as Krishna Pratap Chouhan wrote.
James Okrah
2,061 Pointsfor (i=4; i<=156; i+=){
console.log(i); } this code still did not work. can someone help me out?
Evgeniia Mas
4,452 PointsDid you try ? perhaps in concrete task you should use only use < only and 157 therefore. for (i = 4; i <=156; i++) { console.log (i); }
Steven Parker
231,269 PointsYour increment clause is incomplete.
Instead of "i+=
" you probably intended to write "i+=1
".
Unsubscribed User
3,436 PointsUnsubscribed User
3,436 PointsThis was helpful. Thanks!