2

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.

3
  • 3
    How about: for(; row < ROWS; row++) Commented Mar 18, 2014 at 22:01
  • 1
    since the issue seems to be a syntax error (missing or extra item) - it is hard to know what's going on with only a code snippet. Can you provide more information / code to narrow it down a bit? Commented Mar 18, 2014 at 22:02
  • Since you're relatively new to JavaScript, have a look at MDN's JavaScript guide. Commented Mar 18, 2014 at 22:36

4 Answers 4

5

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) {
          // ...
Sign up to request clarification or add additional context in comments.

2 Comments

This is how the code was setup in the first place, but doing this returns this error:Uncaught TypeError: Cannot read property '0' of undefined
@user3055668 that means you need to make sure that levelMap[row] is non-empty before trying to find a column in it.
2

A for loop in javascript is:

for (pre initialization; loop condition; post operation) {}

Your code doesn't work when you remove the ; as then you have only 2 of the 3 parts of the loop:

for(row < ROWS; row++) // missing post operation

With the ;, for(row < ROWS; row++;) has row < ROWS; as the initialization (which does nothing), row++; as the loop condition, which will pass as long as row != 0, and no post operation.

What you probably intended to use was

for(row = 0; row < ROWS; row++) 

3 Comments

I just tried this and it returns the same error of: Unexpected ')'. for( column < COLUMNS; column++;)
Right, same problem there. for(column = 0; column < COLUMNS; column++)
thank you for the help, this appears to have solved my issues. it also appears that im not cut out for this... thanks for walking me though it.
2

Quick and point answer:

You missed the for statement. Use like this:

for(; row < ROWS; row++)
{
  ...
}

Comments

0
  ...
  for(row < ROWS; row++;) 
  { 
    for( column < COLUMNS; column++;) 
  ... 

should probably be...

  ...
  for(;row < ROWS; row++) 
  { 
    for(;column < COLUMNS; column++)
  ...

which is valid since your row and column are already defined. It's essentially the shorthand for omitting the var row = 0; var column=0; definitions before the loops like:

  ...
  for(var row = 0;row < ROWS; row++) 
  { 
    for(var column = 0;column < COLUMNS; column++)
  ...

Comments

Your Answer

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.