forked from gutsyphilip/js-algorithms-demo-
-
Notifications
You must be signed in to change notification settings - Fork 236
Expand file tree
/
Copy pathindex-SOLUTION.js
More file actions
36 lines (30 loc) · 869 Bytes
/
index-SOLUTION.js
File metadata and controls
36 lines (30 loc) · 869 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// USING FOR LOOP
function hammingDistance(stringA, stringB) {
let result = 0
if (stringA.length == stringB.length) {
for (let i = 0; i < stringA.length; i++) {
if (stringA[i].toLowerCase() != stringB[i].toLowerCase()) {
result++
}
}
return result
} else {
throw new Error('Strings do not have equal length')
}
}
// 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
}