curriculum/challenges/english/blocks/daily-coding-challenges-javascript/69162d64f96574d9bb629efb.md
Given two strings of the same length, a secret word and a guess, compare the guess to the secret word using the following rules:
"A" to "Z");"2" if the letter is in the secret word and in the correct position."1" if the letter is in the secret word but in the wrong position."0" if the letter is not in the secret word."2") are assigned first, then partial matches ("1") are assigned from left to right for remaining letters.For example, given a secret word of "APPLE" and a guess of "POPPA", return "10201":
The first "P" is not in the correct location ("1"), the "O" isn't in the secret word ("0"), the second "P" is in the correct location ("2"), the third "P" is a zero ("0") because the two "P"'s in the secret word have been used, and the "A" is not in the correct location ("1").
compare("APPLE", "POPPA") should return "10201".
assert.equal(compare("APPLE", "POPPA"), "10201");
compare("REACT", "TRACE") should return "11221".
assert.equal(compare("REACT", "TRACE"), "11221");
compare("DEBUGS", "PYTHON") should return "000000".
assert.equal(compare("DEBUGS", "PYTHON"), "000000");
compare("JAVASCRIPT", "TYPESCRIPT") should return "0000222222".
assert.equal(compare("JAVASCRIPT", "TYPESCRIPT"), "0000222222");
compare("ORANGE", "ROUNDS") should return "110200".
assert.equal(compare("ORANGE", "ROUNDS"), "110200");
compare("WIRELESS", "ETHERNET") should return "10021000".
assert.equal(compare("WIRELESS", "ETHERNET"), "10021000");
function compare(word, guess) {
return word;
}
function compare(word, guess) {
const n = word.length;
let result = Array(n).fill("0");
let used = Array(n).fill(false);
for (let i = 0; i < n; i++) {
if (guess[i] === word[i]) {
result[i] = "2";
used[i] = true;
}
}
for (let i = 0; i < n; i++) {
if (result[i] === "0") {
for (let j = 0; j < n; j++) {
if (!used[j] && guess[i] === word[j]) {
result[i] = "1";
used[j] = true;
break;
}
}
}
}
return result.join("");
}