I'm trying to check if all the elements of an array(arr1,arr2,arr3...) in a multi-dimensional array(arrs) match the ones of a given array(randomArr) that can be of any length. This is what I came up with so far:
var randomArr = ['1','4','3'];
function check(arrA,arrB){
for(i=0;i<arrA.length;i++){
if(arrB.indexOf(arrA[i])==-1){return false;}
}
return true;
}
function superCheck(){
var arr1 = ['1','2','3']
, arr2 = ['4','5','6']
, arr3 = ['7','8','9']
, arr4 = ['1','4','7']
, arr5 = ['2','5','8']
, arr6 = ['3','6','9']
, arr7 = ['1','5','9']
, arr8 = ['3','5','7'];
var arrs = [arr1, arr2, arr3, arr4, arr5, arr6, arr7, arr8];
for(i=0;i<arrs.length;i++){
if(check(arrs[i],randomArr)){return true;}
}
return false;
}
console.log(superCheck());
But when I try to run it I get this error(An infinite loop (or a loop taking too long) was detected, so we stopped its execution.). How can I make this work??
I would do it this way:
var winning = [win1, win2, win3, win4, win5, win6, win7, win8],
xScore = 0,
oScore = 0,
scored = false,
xMarkedStr = xMarked.sort().join(''),
oMarkedStr = oMarked.sort().join('');
winning.some(win => {
var winStr = win.join('');
if (xMarkedStr === winStr) {
xScore++;
scored = true;
}
if (oMarkedStr === winStr) {
oScore++;
scored = true;
}
return scored;
});
console.log(xScore, oScore);
I found a way to check if avery element of a single array is inside another array. This is the solution:
function check(superset,subset){
return subset.every(function(val){
return (superset.indexOf(val)>=0);
})
}
Here, you'll check if all the values inside subset are present in the superset. If one of the items in the subset were not to be present, it's index would be -1 and therefore the function would return false.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.