-2

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.

  • 1
    Well, this is just not an accepted syntax. Did you glance at the documentation ? – Denys Séguret May 15 '19 at 15:28
  • 1
    Look at [Iterators](https://stackoverflow.com/questions/26661771/what-does-include-do-in-linq) (i.e. you are doing it wrong) – crashmstr May 15 '19 at 15:28
  • What is `generationNum`, `output` and `generations`? – k3llydev May 15 '19 at 15:30
  • Also **do** declare your variables. A free i like this is a ton of bugs – Denys Séguret May 15 '19 at 15:37
  • I do declare my variables, also I am making an AI to learn English. – Citrus fruit boy May 15 '19 at 15:43
  • Of course it points to your "for(i <= 4) {", where do you have that syntax from? Can you please point to a documentation? Where is your variable initializer, where the increment? A normal for loop expects 3 (three) expressions. Why do you think something like that could work, if you don't stick to the syntax your language expects? – Michael S. May 12 '23 at 13:18

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,754
  • 32
  • 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
  • 913
  • 10
  • 20