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