forked from CodeToExpress/dailycodebase
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hamming.js
27 lines (23 loc) · 817 Bytes
/
hamming.js
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
function hammingDistance (str1, str2) {
let size1 = str1.length,
size2 = str2.length,
distance = 0;
// Check if the strings are of equal length
if (size1 !== size2) {
console.log ("The strings \"" + str1 + "\" and \"" + str2 + "\" do not have equal lengths");
return -1;
}
// Check the different characters at corresponding positions
for (let i=0; i<size1; i++) {
if (str1[i] !== str2[i]) {
distance++;
}
}
// Print the result and return the hamming distance
console.log(`The hamming distance between "${str1}" and "${str2}" is ${distance}`);
return distance;
}
hammingDistance('karolin', 'kathrin');
hammingDistance('karolin', 'kerstin');
hammingDistance('1011101', '1001001');
hammingDistance('2173896', '2233796');