From 1de25de379bdc3224fc67c682ea979429953965c Mon Sep 17 00:00:00 2001 From: koronya Date: Thu, 5 Mar 2026 20:56:51 +0900 Subject: [PATCH] [JS][7kyu] Guess the Word: Count Matching Letters --- .../koronya.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 codewars/7kyu/guess-the-word-count-matching-letters/koronya.js diff --git a/codewars/7kyu/guess-the-word-count-matching-letters/koronya.js b/codewars/7kyu/guess-the-word-count-matching-letters/koronya.js new file mode 100644 index 000000000..4a5eea915 --- /dev/null +++ b/codewars/7kyu/guess-the-word-count-matching-letters/koronya.js @@ -0,0 +1,19 @@ +// [JS][7kyu] Guess the Word: Count Matching Letters +// guess-the-word-count-matching-letters +// https://www.codewars.com/kata/5912ded3f9f87fd271000120/train/javascript + +const countCorrectCharacters = (correctWord, guess) => { + const correctWordArr = correctWord.split('') + const guessArr = guess.split('') + if (correctWordArr.length !== guessArr.length) { + throw new Error('Words must be of the same length') + } + return correctWordArr.reduce((acc, curr, index) => acc + (curr === guessArr[index] ? 1 : 0), 0) +} + +countCorrectCharacters('dog', 'car') === 0 +countCorrectCharacters('dog', 'god') === 1 +countCorrectCharacters('dog', 'cog') === 2 +countCorrectCharacters('dog', 'cod') === 1 +countCorrectCharacters('dog', 'bog') === 2 +countCorrectCharacters('dog', 'dog') === 3