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


i'm trying test if array has numbers 1 through 9. have 9 different arrays test, i'm trying loop set of array 1 one , convert string , test array if has numbers 1 through 9. when output code, comes out

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

the last 1 should come out false, because array[8] not contain numbers 1 9. i'm not sure if regex coding wrong, test prints out true arrays should false.

function doneornot(board){     var numbertest = /[1-9]/g;   var boardstr = "";   var boardtest;   var testresult = [];      for(var = 0; < 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]]); 

a regex not right tool use. not tell if every digit exists. tells if digit exists or different form of regex tell if nothing digits exist, doesn't tell if each digit exists (in order).


here's conceptually simple approach testing given array see if digits 1-9 present:

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

and, combine 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 fancier schemes might perform better. point above find conceptually simplest mechanism work , tolerate input long array contained each of 9 digits.

i don't understand constraints of test arrays, if you're trying see if if arrays have 9 elements in them include digits 1 9 , can in order, 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 different approach starts 9-bit bitmask , clears bit each time finds 1 of 1-9 digits , can see if whole bitmask has been cleared @ end. version tolerates values outside range of 1-9 (and array longer 9), changed checking see if length 9 @ beginning.

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

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


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -