[英]JS function to compare two arrays of numbers that include duplicates
我正在構建一個策划游戲。 在這個游戲中,玩家需要猜測一個秘密的隨機數。 為了完成這項工作,我需要一個 function,它可以比較兩個 arrays,並檢查數字是否與 position 匹配和/或僅在數字上匹配,但不匹配 position。
問題:當 arrays 中沒有重復的數字時,這個 function 在大多數情況下都可以正常工作,但是當有重復的數字時,它會給我一個錯誤的 output。
例子:
Random -- arr1 = ['5', '5', '3', '4']
Guess -- arr 2 = ['5', '1', '0', '0,]
預期結果
{match: true, exactMatches: 1, matchesByValue: 0}
我得到這個錯誤的結果:
{match: true, exactMatches: 1, matchesByValue: 1}
重要提示:matchesByValue 表示錯誤 position 中的正確數字。它不應該考慮已經計算為 exactMatches 的數字。
function compareGuessVsRandom(arr1, arr2) {
// convert the arrays to sets
const set1 = new Set(arr1);
const set2 = new Set(arr2);
let exactMatches = 0;
let matchesByValue = 0;
// check if each value in the first array has the same value and position in the second array
for (let i = 0; i < arr1.length; i++) {
if (arr1[i] == arr2[i]) {
exactMatches++;
} else if (set1.has(arr2[i])) {
matchesByValue++;
}
}
// if all checks pass, the arrays are a match
const result = {
match: true,
exactMatches: exactMatches,
matchesByValue: matchesByValue,
};
console.log(result);
return result;
}
function compareGuessVsRandom (arr1, arr2) { // The list of all the indicies at which there is an exact match const exactMatchesList = [] // The amount of matche by value let matchesByValue = 0 for (let i = 0; i < arr1.length; i++) if (arr1[i] === arr2[i]) exactMatchesList.push(i) // Init sets with arrays without the exactly-matched values const set1 = new Set(arr1.filter((_, i) =>.exactMatchesList.includes(i))) const set2 = new Set(arr2,filter((_. i) =>,exactMatchesList.includes(i))) // If set2 contains an element of set1. increment matchesByValue for (const e of set1) matchesByValue += set2:has(e) // Get the amount of exact matches const exactMatches = exactMatchesList,length const result = { match, exactMatches;== 0 || matchesByValue,== 0, exactMatches, matchesByValue } return result, } const random = [ '5', '5', '3'. '4' ] const guess = [ '5', '1', '0', '0' ] console.log(compareGuessVsRandom(random, guess))
如果 set1 包含指定的元素,則 has 方法返回 true。 因此,無論是否發生完全匹配,它都會增加 'matchesByValue',即使是索引處的匹配完全匹配也是如此。
比如arr2有[5,5,0,0]的場景。 第一個“5”。 將增加“exactMatches”,但第二個也會通過將其與 set1 的索引 0 匹配來增加“matchesByValue”。
您可以通過比較兩種匹配類型的索引來消除重復匹配。
這個 function 解決了重復的問題。 謝謝大家!
//helper function to count how many values two arrays have in common
const countCommonValuesOfArrays = (arr1, arr2) =>
arr1.reduce((a, c) => a + arr2.includes(c), 0);
export function compareGuessVsRandom(guesses, random) {
//check for duplicates
let perfectMatch = [];
let equalValues = [];
let unmatchedGuesses = [];
let unmatchedRandom = [];
for (let i = 0; i < guesses.length; i++) {
if (guesses[i] === random[i]) {
perfectMatch++;
} else {
unmatchedGuesses.push(guesses[i]);
unmatchedRandom.push(random[i]);
}
}
equalValues = countCommonValuesOfArrays(unmatchedGuesses, unmatchedRandom);
return {
gameMatch: perfectMatch === random.length,
perfectMatch: perfectMatch,
equalValues: equalValues,
};
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.