Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I'm trying to test if an array has numbers 1 through 9. I have 9 different arrays to test, so I'm trying to loop set of array one by one and convert it to a string and test that array if it has numbers 1 through 9. When I output my code, it comes out as

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

The last one should come out false, because the array[8] does not contain all the numbers from 1 to 9. I'm not sure if my regex coding is wrong, but the test prints out 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]]);
like image 715
jyoon006 Avatar asked Jan 23 '26 22:01

jyoon006


2 Answers

A regex is simply not the right tool to use. It does not tell you if every digit exists. It tells you if any digit exists or a different form of the regex could tell you if nothing but digits exist, but it doesn't tell you if each 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 could combine that 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, there are probably fancier schemes that might perform better. The point above was to find the conceptually simplest mechanism that would work and would tolerate any input as long as the array contained each of the 9 digits.

I don't understand all the constraints of the your test arrays, but if what you're really trying to see if if the arrays have exactly 9 elements in them that include all the digits from 1 to 9 and they can be in any order, then you could do that like this:

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's a different approach that starts with a 9-bit bitmask and then clears a bit each time it finds one of the 1-9 digits and then it can just see if the whole bitmask has been cleared at the end. This version tolerates values outside the range of 1-9 (and an array longer than 9), but that could easily be changed by just checking to see if the length was 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/

like image 147
jfriend00 Avatar answered Jan 26 '26 10:01

jfriend00


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]]);
like image 29
Oriol Avatar answered Jan 26 '26 12:01

Oriol