I have two arrays in Javascript: code and submittedCode. I am trying to compare the two arrays. Each of the arrays is 4 integers long, and each integer is a random value of 1 to 6. I also have two variables: red and white. When the two are compared, the red variable should be set to the number of similarities in the arrays that are the same number, same index. White should be set to the number of similarities in the array that are the same number, but different indexes. For example, if the code array is [1, 3, 6, 5] and submittedCode is [1, 6, 4, 5], then red would be set to 2, and white would be set to 1. This is the same logic as the game Mastermind if anyone has played that. Below is what I have tried, but it is not working as intended.
for(let i = 0; i < code.length; i++) {
if(code[i] == submittedCode[i])
{
code.splice(i, 1);
submittedCode.splice(i, 1);
red++;
//console.log(i);
}
}
console.log(code);
var saveLength = code.length;
code = code.filter(function(val) {
return submittedCode.indexOf(val) == -1;
});
white = saveLength - code.length;
console.log(red + ", " + white);