Testing true or false if it has numbers 1-9 in an array

I am trying to check if an array has a number from 1 to 9. I have 9 different arrays to test, so I am trying to concatenate a set of arrays one by one and convert it to a string and check this array if it has numbers from 1 to 9. When I output my code, it outputs as

[true, true, true, true, true, true, true, true, false] 

      

The latter should turn out to be false because array [8] does not contain all numbers from 1 to 9. I'm not sure if my regex coding is wrong, but the test prints true for arrays that should be false.

function doneOrNot(board){


  var numberTest = /[1-9]/g;
  var boardStr = "";
  var boardTest;
  var testResult = [];

    for(var i = 0; i < board.length; i++) {
      boardStr = board[i].toString();
      boardTest = numberTest.test(boardStr);
      testResult.push(boardTest);   
      console.log(boardStr);
    }

console.log(testResult);

}

doneOrNot([[5, 3, 4, 6, 7, 8, 9, 1, 2], 
       [6, 7, 2, 1, 9, 0, 3, 4, 9],
       [1, 0, 0, 3, 4, 2, 5, 6, 0],
       [8, 5, 9, 7, 6, 1, 0, 2, 0],
       [4, 2, 6, 8, 5, 3, 7, 9, 1],
       [7, 1, 3, 9, 2, 4, 8, 5, 6],
       [9, 0, 1, 5, 3, 7, 2, 1, 4],
       [2, 8, 7, 4, 1, 9, 6, 3, 5],
       [3, 0, 0, 4, 8, 1, 1, 7, 9]]);

      

+3


source to share


3 answers


A regex is just not good to use. It doesn't tell you if every number exists. It tells you if any digit exists, or some other form of regex can tell you if only digits exist, but it doesn't tell you if every digit exists (in any order).




Here's a conceptually simple approach for testing any given array to see if all digits 1-9 are present:

function test(arr) {
    for (var i = 1; i <= 9; i++) {
        if (arr.indexOf(i) === -1) {
            return false;
        }
    }
    return true;
}

      

And you can combine this with your test data:

function doneOrNot(list) {
    return list.map(function(arr) {
        return test(arr);
    });
}

doneOrNot([[5, 3, 4, 6, 7, 8, 9, 1, 2], 
   [6, 7, 2, 1, 9, 0, 3, 4, 9],
   [1, 0, 0, 3, 4, 2, 5, 6, 0],
   [8, 5, 9, 7, 6, 1, 0, 2, 0],
   [4, 2, 6, 8, 5, 3, 7, 9, 1],
   [7, 1, 3, 9, 2, 4, 8, 5, 6],
   [9, 0, 1, 5, 3, 7, 2, 1, 4],
   [2, 8, 7, 4, 1, 9, 6, 3, 5],
   [3, 0, 0, 4, 8, 1, 1, 7, 9]]);

      

Working demo: http://jsfiddle.net/jfriend00/w04b6frv/




FYI, maybe there are more favorable circuits that could work better. The point above was to find a conceptually simple mechanism that will work and will tolerate any input as long as the array contains each of the nine digits.

I don't understand all the limitations of your test arrays, but if you are really trying to see if the arrays have exactly 9 elements in them that include all the numbers from 1 to 9 and they can be in any order, then you can do it here So:

function test(arr) {
    var s = arr.slice(0).sort().toString();
    return s === "1,2,3,4,5,6,7,8,9";
}

      

Working demo: http://jsfiddle.net/jfriend00/6cdg5b3g/




And here is another approach that starts with a 9-bit bitmask and then clears the bit every time it finds one of the 1-9 digits, and then it can just see if the whole bitmask was cleared at the end. This version allows values ​​outside the range 1-9 (and an array longer than 9), but this can be easily changed simply by checking that the length is 9

at the beginning.

function test(arr) {
    // initalize bits to 111111111 in binary
    // one bit for each value 1-9
    var bits = 511;
    arr.forEach(function(item) {
        // if the number is in range, then clear the appropriate bit
        if (item >= 1 && item <= 9) {
            bits &= ~(1 << (item - 1));
        }
    });
    // return if all bits have been cleared
    return bits === 0;
}

      

Working demo: http://jsfiddle.net/jfriend00/nt1mya4d/

+7


source


I suggest

var testResult = board.map(function(arr) {
  arr = arr.slice().sort(); // Copy and sort
  for(var j=0; j<9; ++j)    // Iterate numbers
    if(arr[j] !== j+1) return false;
  return true;
});

      



function doneOrNot(board){
  var testResult = board.map(function(arr) {
    arr = arr.slice().sort(); // Copy and sort
    for(var j=0; j<9; ++j)    // Iterate numbers
      if(arr[j] !== j+1) return false;
    return true;
  });
  console.log(testResult);
}
doneOrNot([[5, 3, 4, 6, 7, 8, 9, 1, 2], 
           [6, 7, 2, 1, 9, 0, 3, 4, 9],
           [1, 0, 0, 3, 4, 2, 5, 6, 0],
           [8, 5, 9, 7, 6, 1, 0, 2, 0],
           [4, 2, 6, 8, 5, 3, 7, 9, 1],
           [7, 1, 3, 9, 2, 4, 8, 5, 6],
           [9, 0, 1, 5, 3, 7, 2, 1, 4],
           [2, 8, 7, 4, 1, 9, 6, 3, 5],
           [3, 0, 0, 4, 8, 1, 1, 7, 9]]);
      

Run codeHide result


+2


source


Your mistake is when you convert the whole string to a string and check the whole string for a regex. So for this line:

[9, 0, 1, 5, 3, 7, 2, 1, 4]

We would expect false

:

(/[1-9]/g).test([9, 0, 1, 5, 3, 7, 2, 1, 4].toString())

returns true

because the string actually contains at least one of the expected numbers.

You can fix your possibilities by using match

instead test

. match

will return the number of matches in the string, so in this case:

[9, 0, 1, 5, 3, 7, 2, 1, 4].toString().match(/[1-9]/g)

returns ["9", "1", "5", "3", "7", "2", "1", "4"]

Then you could count the length of the result of the match, but what if the string contained two "9s"? You need to make it unique. I suggest to simplify the function doneOrNot

:

var done = '123456789';

function doneOrNot(board) {
    var results = [];

    for(var i = 0; i < board.length; i++) {
        var boardStr = board[i].sort().join('');
        results.push(boardStr === done);
    }
    console.log(results);
}

      

JSFiddle: http://jsfiddle.net/99bxLt3s/

Better solution would be to use another library that gives you Array equality check. Then you can write your code like this:

var done = [1, 2, 3, 4, 5, 6, 7, 8, 9];

function doneOrNot(board) {
    var results = [];

    for(var i = 0; i < board.length; i++) {
        results.push(ArrayEquals(board[i], done));
    }
    console.log(results);
}

      

Where ArrayEquals

is a function that compares two arrays for equality from an external library.

+1


source







All Articles