0

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);
tb_03
  • 61
  • 10

1 Answers1

0
let arr=[1,3,1,2]       //two array we operate on
let arr2=[4,4,1,2]

let red=0
let white=0

//we need to check the current length of remaining array
let currentLength=arr.length            

for(let i=0; i<currentLength; i++){
    if(arr[i]===arr2[i]){       
    arr.splice(i,1)             //if same number same index, remove this item from array
    arr2.splice(i,1)
    currentLength-=1            //currentLength-1 because we reduced the array
    i-=1                                    //i-1 because we'd skip the next element
    red+=1                              //update red
  }
}

//we basically do the same thing but with white
//but in the second array we look for the first index of the current element and remove that
for(let i=0; i<arr.length; i++){
if(arr2.includes(arr[i])){   
    //1: I should've taken away the item from arr2 first
    //2: arr2.splice(arr2.findIndex(f=>f===arr[i],1))  notice I messed up where I put the 1 at the end
    arr2.splice(arr2.findIndex(f=>f===arr[i]),1)
    arr.splice(i,1)  
    i-=1
    white+=1
}
}

Now this might not be the most optimal solution, you can do this in one loop, but for visibility I created 2 for loops, in the first we check the 'red' elements, and we take those out in the second loop we check if there are any 'white' elements, and we take those out.

Patrick Visi
  • 100
  • 1
  • 7
  • Welcome, could you please explain what you have changed and why that fixed the problem the OP is having, as it currently stands this is a code only answer for all intents and purposes – Barkermn01 Mar 30 '22 at 18:19
  • That works sometimes, but when I have two of the same number in the first array such as [1, 3, 1, 2], when I compare it with [4, 4, 1, 2] it returns 2 red 1 white. – tb_03 Mar 30 '22 at 19:21
  • It may be my bad, but I'm having trouble understanding what is the problem with that? Red: same number, same index White: same number, distinct index So red should be 2, because nothing but the last 2 item of the array is the same number, same index And white should be 1, because arr1[0] can be found in the second array. – Patrick Visi Mar 30 '22 at 20:33
  • I don't think I explained it clearly enough. There can't be duplicates if that makes sense. arr[2] has already been marked as red, so it can't later be marked as white. It is like Wordle or Mastermind if you have played either of those. The expected result should be 2 red, 0 white. I'm sorry for the confusion. – tb_03 Mar 31 '22 at 02:49
  • Oh I think I get it now! I updated my above code with comments, please check if that is what you wanted – Patrick Visi Mar 31 '22 at 07:58
  • It's close, but I tried arr as [1, 2, 2, 2] and arr2 as [2, 1, 2, 2] and it returns 2 red, and 1 white. It should be 2 white, 2 red. I am confused as to why your code isn't working because the logic makes total sense. It seems to break down when all the numbers are correct but in different orders. It works otherwise though. – tb_03 Mar 31 '22 at 16:44
  • Yes, I see it now, sorry, I updated my code once again, I made some stupid mistakes, but I edited it so it should work now. 3rd time's the charm as they say. – Patrick Visi Mar 31 '22 at 17:59