Learn Intermediate OOP by Building a Platformer Game - Step 86

Tell us what’s happening:

You should have an if statement that uses the every method to check if every rule in the collisionDetectionRules array is true.

Your code so far

<!-- file: index.html -->

/* file: styles.css */

/* file: script.js */
// User Editable Region

if(every(collisionDetectionRules)) {
  player.velocity.y=0;
  return;
}


// User Editable Region

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36

Challenge Information:

Learn Intermediate OOP by Building a Platformer Game - Step 86

every is an array method, that means you need to call it on an array, like myArr.every(), and you need to pass a callback as argument

1 Like

*Test failed with the following text:

You should have an if statement that uses the every method to check if every rule in the collisionDetectionRules array is true.

I have made my own tests the following way:

const collisionDetectionRules_one = [true, true, true, true];

const collisionDetectionRules_two = [true, false, true, true];

// output :  Result one:  true
 console.log("Result one: "  + collisionDetectionRules_one.every((rule) => rule === true));  

// output :  Result one:  false
 console.log("Result two: "  + collisionDetectionRules_two.every((rule) => rule === true));   

So, my callback seems to working correctly.
But when I use this callback function in Step 86 the test will fail.
How should I continue?

you don’t need to strict compare to true, you can just have rule as return value
also the parenthesis around rule may make the test fail

Thank you very much. Now I can move on.

Hello,

I had the same problem as jasakar. My code was:

if (collisionDetectionRules.every((rule) => rule === true)) {
      player.velocity.y = 0;
      return;
    }

The test failed. But I tested it in the console and it was working. So I don’t understand how can it fail the test.

the tests check what you have written, so often they pass with a limited range of solutions.

Would you like to open an issue for this?

Well I think it is worth opening an issue, because I googled how to use the every() method, followed the documentation and the test failed. I spent a lot of time adding and removing brackets and only completed the step after I searched the Forum and found this post.

My point is that if we checked e.g. if every element of an array is equal to 3, we would write:

[1, 2, 3, 4].every((item) => item === 3)

I believe that this is the general way of using this method.

Here we can shorten it to

every((item) => item )

only because we are “lucky” that the items in the array are only true or false. It is a special case. But the general approach should still work.
Or there could be at least some hint, that it is possible to shorten it in this case.

2 Likes

The collisionDetectionRules array is an array of booleans, I don’t think it’s “lucky” as you say. Sure, it works to say true === true and false === true which is what is happening in your callback function, but it’s more efficient to just check the boolean. That being said, the solution still works and it would be nice if the test wasn’t looking for a specific format in the callback function

1 Like