Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript- Unexpected ) in a for loop

Jslint is returning an odd error to a very annoying section of code I've copied from my textbook. Here is how the code was in the book:

....
{
for(var column = 0; column < COLUMNS; column++)
{
var currentTile = levelMap[row][column];
if(currentTile !== EMPTY)

and that threw up a bunch of errors, like you cant assign a value of 0 to undefined or whatever. so i switched the var statements around like this...

{var row = 0; 
  var column=0;
  for(row < ROWS; row++;) 
  { 
    for( column < COLUMNS; column++;) 
    { 
      var currentTile = levelMap[row][column];

      if(currentTile !== EMPTY)
      {

so having it this way- it works now. (sort of...chrome doesnt throw up bugs but its not working well. things are not displaying in my game) but if i run it through jslint i get this error.

Unexpected ')'. for(row < ROWS; row++;)

taking the ; off of row++ breaks it. taking the ) out breaks it.

And even though it runs, it doesn't run right. I can provide more information if you'd like, thought i'd just keep it on the shorter end .

im an idiot, apparently, cause i cant figure it out.

like image 709
user3055668 Avatar asked Aug 23 '26 17:08

user3055668


1 Answers

A for loop consists of four pieces of information:

  • the initial action*, that will be done before the actual loop
  • the condition* which determines whether the statement is executed
  • the post action*, that is done after the statement is executed
  • the statement

*actually those are all expressions, but it's more important to remember what they're for

for(init; cond; post) 
    statement;

it can be directly translated into a while loop, if you feel more comfortable using that one:

init;
while(cond){
    statement;
    post;
}

As you can see, you we're missing the init. Note that all of the four can be empty. Overall we get:

var row, column, currentTile;
for(row = 0; row < ROWS; row++) {
    for(column = 0; column < COLUMNS; column++) { 
      currentTile = levelMap[row][column];

      if(currentTile !== EMPTY) {
          // ...
like image 180
Zeta Avatar answered Aug 25 '26 06:08

Zeta