We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi. I'm trying to recreate the classic breakout game. To go to the next level I need to have a variable that changes to true when all element on array are false (when all the boxes have been destroyed). This is what I think should work, but it returns true as soon as one element becomes false.
var allTrue = false;
for (var r = 0; r < rows; r++) {
for (var c = 0; c < col; c++) {
if(boxes[r][c] == false){
allTrue = true;
}
}
}
Answers
Check this below. If only one box is false, allTrue becomes false.
Kf
P.S.: The sample above can be copied & pasted & run directly at any modern browser's console. \m/
A slightly variant version, mixing up some() & every() methods inside areAllBoxesDestroyed(): :bz
https://developer.Mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/every
Again, we can open browser's console by hitting F12 and paste the sample above at console's tab: :ar!
Is this what you're looking for?
[false, true, false].every(element => element === false) // false
[false, false, false].every(element => element === false) // true
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/every