Solution explanation - Learn Intermediate OOP by Building a Platformer Game - Step 86

Ive solved this one by some luck. but I dont understand the answer. It says “Next, add an if statement that checks if every rule in the collisionDetectionRules array is true
and I used the word ‘rule’ in my parameter and then returned it. But even though it sounds logical to check every “rule”, I don’t see any reference of that word in my code? So how does it know to check every element in my array?

Your code so far

WARNING

The challenge seed code and/or your solution exceeded the maximum length we can port over from the challenge.

You will need to take an additional step here so the code you wrote presents in an easy to read format.

Please copy/paste all the editor code showing in the challenge from where you just linked.

Replace these two sentences with your copied code.
Please leave the ``` line above and the ``` line below,
because they allow your code to properly format in the post.

Your browser information:

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

Challenge Information:

Learn Intermediate OOP by Building a Platformer Game - Step 86

The every method will go through each element in the array and if each element passes the test then, it will return true otherwise it will return false.

For example, let’s say we had this list of numbers

const numbersArr = [10, 15, 18, 22, 66, 88];

if we use the every method to see if every single number here is greater than 5, then it would return true

numbersArr.every((num) => num > 5); // return true

if we changed it to check if every single number is greater then 10 then it would be false because the first number of 10 is not greater then 10

numbersArr.every((num) => num > 10); // return false

how does this relate to our OOP game here?

the collisionDetectionRules array is a collection boolean expressions.

  const collisionDetectionRules = [
      player.position.y + player.height <= platform.position.y,
      player.position.y + player.height + player.velocity.y >= platform.position.y,
      player.position.x >= platform.position.x - player.width / 2,
      player.position.x <=
        platform.position.x + platform.width - player.width / 3,
    ];

each of these expressions will either be true or false.

for example, this will either evaluate to true or false

      player.position.y + player.height <= platform.position.y,

same goes for all of the other expressions in that array.

If every single one of those rules evluates to true then you will have a whole bunch of trues

[true, true,...]

if all of the elements in that array are true, when you use the every method, then that would return true

you are basically testing this if every rule in that array is true

[true, true, true].every(val=> val)

but if one of those rules is false, then the every method will return false

[true, false, true].every(val=>val)

the rule parameter represents each element in the collisionDetectionRules array

      player.position.y + player.height <= platform.position.y,
      player.position.y + player.height + player.velocity.y >= platform.position.y,
      player.position.x >= platform.position.x - player.width / 2,
      player.position.x <=
        platform.position.x + platform.width - player.width / 3,

If you were building this project out on your own, you could technically call that parameter whatever you want to.

You could technically write something like this and it still be valid code.

const numbersArr = [10, 15, 18, 22, 66, 88];

numbersArr.every((awesomeSauce) => awesomeSauce > 5); // return true

but it is best to use names that relate to what you are trying to do.

So rule makes better sense here for this OOP game.

hope that helps

4 Likes

thanks your examples really got me going after being stock for hours.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.