JavaScript - Checking if all values in an array are not Null - Array / Each /

Ash Pettit

Working on TicTacToe in JavaScript/JQuery. Beginner programmer but couldn't find this online.

At the moment I have a normal array called "game_board". The array has 9 values which are 0-8. It is not a 'hash'.

I'm looking into a loop or similar that checks if all values in array are not null. Should all values in the "game_board" array not be Null then the code should make an action such as [Alert("Draw")]. If any values in the array have a value the function should do nothing.

Here's my code attempt. Any ideas would be aweesome. My attempt is the theory that the loop breaks if it finds a null value. If it does not find a null value it goes to ELSE and reports 'draw'

    // check draw
for (var p = 0; p < 9; p++) { 
    if (game_board[p] === null) {
        break;
    }
    else {
        alert("Draw");
    }
}

Thanks very much. I know this maybe a basic question but appreciate any answers.

Please excuse my newness! Not 100% sure of the logic. Perhaps something with Jquery will help!

Nathan Friend

Use Array.some to check if any of the values are null:

var isAtLeastOneNull = game_board.some(function(i) { return i === null; });

Alternatively, use Array.every to do the reverse check:

var areAllNotNull = game_board.every(function(i) { return i !== null; });

Demo:

var game_board = [true, true, true, true, true, true, true, null, true];

document.writeln('There is at least one null item: ' + game_board.some(function(i) { return i === null; }));
document.writeln('<br />');

game_board = [true, true, true, true, true, true, true, true, true];

document.writeln('There is at least one null item: ' + game_board.some(function(i) { return i === null; }));

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Determine if all the values in a PHP array are null

Checking Array Emptiness with Javascript

Is PHP @ safe for checking array values ?

Checking the equality of all values in array in php

Checking the Tags of each TextBox in an array

Checking if an array within an array is empty/null in PHP

Avoid null values while filtering javascript array

Checking all elements in an array in Swift against each other

Checking values for an array for every $i

Remove all null array values before sorting

Checking if an array is null or empty

Checking if content of an array is all integers

checking values within array of arrays

Checking nan values in a numpy array

Checking Each Item in Array for Substring

How to simplify logic checking if all cells in a record of array are null using for

checking an array of string objects for null values

Reduce the use of foreach in array when checking null values php

Error checking array element to null

Checking if all values in one array exist in another array

Checking if an array contains all the elements

Checking for null and empty values in javascript

Checking the axis of array for zero values

qsort turns all values of array to null

How to return a boolean true if all the values in an array are true (strings) and if one of the value is false(string) stop checking using Javascript

JS checking object/array for ALL highest values

Finding and checking values in an array in ReactJS

Checking an array if all objects inside contains a given key value(javascript)

JavaScript: How to match array values with each occurrence and not with all the matches