-3

I'm racking my brain on this, and I've read all of the previous questions that have been answered, but I feel that I'm just missing something.

JSHint gives the error:

The body of a for in should be wrapped in an if statement to filter unwanted properties from the prototype.

How would you all go about resolving this error?

function checkCollision(X, Y, arrayObjs) {
for (var obj in arrayObjs) {

 var objX = (arrayObjs[obj].x / 101).toFixed(0);
    var objY = (arrayObjs[obj].y / 83).toFixed(0);




    //checking collision by checking character placement as well as enemies

    if ((objX == (X / 101).toFixed(0)) && (objY == (Y / 83).toFixed(0))) {
        //collision
        return true;
    }
}

return false; }
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
  • The body of a for in should be wrapped in an if statement to filter unwanted properties from the prototype. – Andrew King Oct 27 '15 at 23:32
  • Im more curious if this error is relevant in this case, most of the other comments state that an if statement should be used to check if the item is present or if it has just been inherited from the prototype. the code runs fine as is. Im just curious about the error. – Andrew King Oct 27 '15 at 23:39
  • That is not a standard JavaScript error message. I also don't think that is a *JavaScript error*. That must a warning from some tool you are using. Please elaborate. – Felix Kling Oct 27 '15 at 23:40
  • apologies, i forgot to mention that, I was checking using JSHint. I resolved the issue using a standard for loop instead of a for in as suggested by another user. – Andrew King Oct 27 '15 at 23:55

1 Answers1

0

Instead of using a for in loop, use a plain for loop, this way you can iterate over the array without going over the prototypes:

function checkCollision(X, Y, arrayObjs) {
    for (var i = 0, length = arrayObjs.length; i < length; i++) {
        var objX = (arrayObjs[i].x / 101).toFixed(0);
        var objY = (arrayObjs[i].y / 83).toFixed(0);
        //checking collision by checking character placement as well as enemies

        if ((objX == (X / 101).toFixed(0)) && (objY == (Y / 83).toFixed(0))) {
            //collision
            return true;
        }
    }
    return false;
}

See the following Stack Overflow post for more information: What does the JSLint error 'body of a for in should be wrapped in an if statement' mean?

Community
  • 1
  • 1
Quill
  • 2,729
  • 1
  • 33
  • 44