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

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

``````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 ++;
}
``````

``````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}));
``````

``````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}));
``````

` `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 ) } }``