Return true when all elements in array are false (p5js)?

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

    var allTrue = true;
    
    for (var r = 0; r < rows; r++) {
        for (var c = 0; c < col; c++) {
                allTrue   &= ! ( boxes[r][c] );     
            }
    }
    
  • edited June 2017
    1. https://developer.Mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/some
    2. https://developer.Mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/fill
    3. https://developer.Mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...of
      
    // https://forum.Processing.org/two/discussion/22930/
    // return-true-when-all-elements-in-array-are-false-p5js#Item_2
    
    // GoToLoop (2017-Jun-05)
    
    "use strict";
    
    const ROWS = 3, COLS = 9,
          IS_TRUE = bool => bool,
          boxes = Array(ROWS);
    
    function createBoxes2dArray() {
      for (let i = 0; i < ROWS; boxes[i++] = new Int8Array(COLS));
    }
    
    function resetAllBoxes() {
      for (const row of boxes)  row.fill(true);
    }
    
    function areAllBoxesDestroyed() {
      for (const row of boxes)  if (row.some(IS_TRUE))  return false;
      return true;
    }
    
    createBoxes2dArray(); // 2d array boxes is created, but not initialized yet
    console.log(areAllBoxesDestroyed()); // true b/c all is still false
    
    resetAllBoxes(); // fill 2d array boxes w/ value true
    console.log(areAllBoxesDestroyed()); // false b/c all or some is now true
    

    P.S.: The sample above can be copied & pasted & run directly at any modern browser's console. \m/

  • edited June 2017

    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

    // https://forum.Processing.org/two/discussion/22930/
    // return-true-when-all-elements-in-array-are-false-p5js#Item_3
    
    // GoToLoop (2017-Jun-05)
    
    "use strict";
    
    const ROWS = 3, COLS = 9,
          IS_TRUE = bool => bool,
          ROW_FALSE = row => !row.some(IS_TRUE),
          boxes = Array(ROWS);
    
    function createBoxes2dArray() {
      for (let i = 0; i < ROWS; boxes[i++] = new Int8Array(COLS));
    }
    
    function resetAllBoxes() {
      for (const row of boxes)  row.fill(true);
    }
    
    function areAllBoxesDestroyed() {
      return boxes.every(ROW_FALSE);
    }
    
    createBoxes2dArray(); // 2d array boxes is created, but not initialized yet
    console.log(areAllBoxesDestroyed()); // true b/c all is still false
    
    resetAllBoxes(); // fill 2d array boxes w/ value true
    console.log(areAllBoxesDestroyed()); // false b/c all or some is now true
    

    Again, we can open browser's console by hitting F12 and paste the sample above at console's tab: :ar!

  • edited June 2017

    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

Sign In or Register to comment.