# 比较两个 arrays 并返回有效和错位的元素

[英]Comparing two arrays and returning valid and misplaced elements

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.游戏应该比较两个 arrays，其中一个存储用户输入，另一个随机生成。 Both arrays have the length of n (let's say n=3) and accept n unique characters as their values. arrays 的长度均为 n（假设 n=3）并接受 n 个唯一字符作为它们的值。 Let's say that the user input is ['A','A', 'B'] and that the winning combination is ['B', 'A', 'C'].假设用户输入是 ['A','A', 'B']，获胜组合是 ['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.但是，我还想跟踪放错位置的元素，其中 arr[i] 与 arr[j] 的值匹配，但对它们索引的相等性检查返回 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.这就是问题所在，如果我将其放在 else 语句中，并将 ['A', 'B', 'A'] 与 ['A', 'C', 'C'] 进行比较，它将返回 1 valid as它应该，但也有 1 个错位，这是不正确的，因为 'A' 在第二个数组中只出现一次。 How would you set up the statement to avoid this?您将如何设置语句来避免这种情况？

I'm quite new to this so I haven't tried much.我对此很陌生，所以我没有尝试太多。

This is the JS way.这就是 JS 的方式。

``````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).成本将为 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.如您所见，Array.prototype.forEach() 的工作方式与 for 正常。

If the input value and the win condition has the same length, you don't need two for loop.如果输入值和获胜条件的长度相同，则不需要两个 for 循环。 And name your variables correctly: `inputs` and `condition` .并正确命名您的变量： `inputs``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.不懂的可以问。