-1

I am using the for command and when I do for(i < 4) { i++ } it pops out: Unexpected token ) at line *line of the command*

I have tried moving some variables and moving where the for command is, even checking StackOverflow, but I can not find an answer. This is strange since it has not happened in the past.

function learnEng() {
    while(generationNum <= generations) {
        for(i <= 4) {
            i++;
        }
    document.write("<h2>sentence "+generationNum+": "+output+"</h2>");
        generationNum++;
        }
}
learnEng();

I didn't expect to get the error because this is the first time I have gotten it, and it spat out an error, and Its sometimes on the line that the for(){} command is on and sometimes it is not, but I know the for(){} command is causing it because it doesn't happen when its not there.

2 Answers2

0

The for statement requires three (optional) expressions, separated by semicolons:

for ([initialization]; [condition]; [final-expression])

If you want to leave out any of the optional expressions, you still need the semi-colons.

For example, here is a for statement with none of the optional expressions:

var i = 0;

for (;;) {
  if (i > 3) break;
  console.log(i);
  i++;
}
Todd Chaffee
  • 6,616
  • 30
  • 41
0

I think the error is due to invalid for syntax. You need to insert 2 ;.

function learnEng() {
    while(generationNum <= generations) {
        for(;i <= 4;) {
            i++;
        }
     document.write("<h2>sentence "+generationNum+": "+output+"</h2>");
        generationNum++;
        }
}
learnEng();
A Rogue Otaku
  • 863
  • 10
  • 19