0

I've been staring at this for half an hour and can't find my error. I just have to make a simple for loop that can print something onto the console.

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

It also is asking for a while loop and a do/while loop. I haven't done those yet, but I don't think that's the error. It says: SyntaxError: Unexpected token ILLEGAL

asked Aug 21, 2014 at 0:39
7
  • 3
    Or the more ubiquitous: i++ Commented Aug 21, 2014 at 0:40
  • i + 1 would not change the value of i after the steps Commented Aug 21, 2014 at 0:42
  • 1
    Suggestion for next times: using browser's debugger may help you find the errors. Commented Aug 21, 2014 at 0:46
  • 3
    The code shown does not contain a syntax error. It will, however, result in an endless loop because the i variable is set to 0 and then never changed. The "Unexpected token ILLEGAL" error you quote doesn't make sense for the code shown - do you have more code that is not shown? Commented Aug 21, 2014 at 0:46
  • 1
    I suggest to learn how to debug JavaScript :) Commented Aug 21, 2014 at 0:54

3 Answers 3

2

Your syntax is incorrect. You have to add the 1 to the i, which you are not currently doing.

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

The classic way, of course is to just do, but I think the first one is more elegant and passes JSLint (which is, of course, the only right way to write javascript).

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

If you want your loop to fully pass JSLint, you can do as follows:

var i; // at the top of your function
// ...
for (i = 0; i < 4; i += 1) {
 console.log(i);
}
answered Aug 21, 2014 at 0:40
Sign up to request clarification or add additional context in comments.

8 Comments

"and passes JSLint" - Does putting var inside the for pass JSLint?
No, it doesn't. I was just referring to the i += 1. When I write a loop, I put the var i outside the loop.
Technically the syntax is correct but the logic is incorrect. "which is, of course, the only right way to write javascript" That's very opinionated, isn't it? ;)
No! I'm being completely objective! :). What do you mean by the logic being incorrect, I'd love to correct my answer.
I mean that if the syntax was incorrect, the engine would actually throw a syntax error. i+1 is valid syntax, but it doesn't do what the OP intends to do (increment i), thus it's a logic error. I can also be very pedantic so feel free to ignore me ;)
|
0

or how 'bout:

for(var i = 0; i < 4; )
 console.log(i++);
answered Aug 21, 2014 at 1:02

Comments

0

I have run your code in Chrome console, and I think your code have no SyntaxError, but it will cause en endless loop.

If you want to run a four loop, you'd better change the code like this:

for(var i=0; i<4; i+=1){
 console.log(i);
}
answered Aug 21, 2014 at 2:07

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.