Home > OS >  Comparing two arrays and returning valid and misplaced elements
Comparing two arrays and returning valid and misplaced elements

Time:01-27

I wanted to make a simple game for my first project but I've encountered some problems with the logic behind it. The game should compare two arrays, one of which stores user input and the other which is randomly generated. Both arrays have the length of n (let's say n=3) and accept n unique characters as their values. Let's say that the user input is ['A','A', 'B'] and that the winning combination is ['B', 'A', 'C']. The win condition is simple, all three elements from the user input array must be valid. An element is valid if both it's value and index correspond to the element in the second array. Checking this is simple enough:

for (let i = 0; i<arr1.length; i  ) {
    for (let j = 0; j<arr1.length; j  ){
        if (arr[i] === arr1[j] && getIndices(arr[i], arr1[j]) === true){
                valid   ;
            }

However, I also want to keep track of misplaced elements, where arr[i] matches the value of arr[j] but the equality check on their indices returns false. Here's the problem, if I were to put this inside an else statement, and compare ['A', 'B', 'A'] to ['A', 'C', 'C'] it would return 1 valid as it should, but also 1 misplaced which is incorrect because 'A' only appears once in the second array. How would you set up the statement to avoid this?

I'm quite new to this so I haven't tried much.

CodePudding user response:

If the input value and the win condition has the same length, you don't need two for loop. And name your variables correctly: inputs and condition.

var points = 0
var misplacedElements = []

for (let i = 0; i<inputs.length; i  ) {
   //findIndex returns index of the element on the condition array
   //If element don't exist returns -1
   const indexOfInput = condition.findIndex(e=> e === inputs[i])

   if(indexOfInput != -1){
      //Element contains but might not be on the same index
      if(indexOfInput == i){
         //On the same index so give a point
         points  
      }else{
        //Hold the index or the element depends to your need
        misplacedElements.push( i )
      }
   }

You can ask if you don't understand.

CodePudding user response:

This is the JS way.

const userList = ['A', 'B', 'C'];
const winList = ['A', 'B', 'A'];

const scoreCalculator = ({ user, win }) => {
  let points = 0;
  user.forEach((value, index) => {
    if (value === win[index]) {
      points  ;
    }
  });
  
  return points;
  
}

console.log(scoreCalculator({user: userList, win: winList}));

The cost will be O(n).

With normal for execution.

const userList = ['A', 'B', 'C'];
const winList = ['A', 'B', 'A'];

const scoreCalculator = ({ user, win }) => {
  
  let points = 0;
  for(let i = 0; user.list; i  ) {
    if (user[i] === win[i]) {
      points  ;
    }
  });
  
  return points;
  
}

console.log(scoreCalculator({user: userList, win: winList}));

As you can see, Array.prototype.forEach() its work like normal for.

  • Related