function checkArray(arr) {
//target array to check against
const targetArray = [
[0, 1, 2],
[3, 4, 5],
[6, 7, 8],
[0, 3, 6],
[1, 4, 7],
[2, 5, 8],
[0, 4, 8],
[2, 4, 6],
]
for (let i = 0; i < targetArray.length; i++) {
compareArrays(arr, targetArray[i])
}
}
function compareArrays(supplied, subArray) {
supplied.sort()
subArray.sort()
let i = 0
let j = 0
for (i, j; i < supplied.length && j < subArray.length;) {
if (supplied[i] < subArray[j]) {
++i
} else if (supplied[i] == subArray[j]) {
++i, ++j
} else {
return false
}
}
return j == subArray.length;
}
checkArray([1, 3, 7, 4])
I'm attempting to create a function that checks if every element in an array is present in any of the nested arrays within the object.
I referred to a function from this post, but I am puzzled as to why I am receiving undefined output instead of true.