Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions codewars/7kyu/guess-the-word-count-matching-letters/koronya.js
Original file line number Diff line number Diff line change
@@ -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