diff --git a/Beginner/hammingDistance/index-SOLUTION.js b/Beginner/hammingDistance/index-SOLUTION.js index 0201452..80f1fde 100644 --- a/Beginner/hammingDistance/index-SOLUTION.js +++ b/Beginner/hammingDistance/index-SOLUTION.js @@ -13,4 +13,24 @@ function hammingDistance(stringA, stringB) { } else { throw new Error('Strings do not have equal length') } -} \ No newline at end of file +} + +// USING FOREACH LOOP +function hammingDistanceForEach(stringA, stringB) { + if (stringA.length !== stringB.length) { + console.error('The two strings should have the same length.') + return + } + + let diffCounter = 0 + const arrStringA = [...stringA.toLowerCase()] + const arrStringB = [...stringB.toLowerCase()] + + arrStringA.forEach((value, index) => { + if (arrStringA[index] !== arrStringB[index]) { + diffCounter++ + } + }) + + return diffCounter +}