-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDCC-12.js
28 lines (22 loc) · 978 Bytes
/
DCC-12.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
// Prompt:
// In information theory, the hamming distance refers to the count of the differences between two strings of equal length. It is used in computer science for such things as implementing "fuzzy search" capability.
// - Write a function named hammingDistance that accepts two arguments which are both strings of equal length.
// - The function should return the count of the symbols (characters, numbers, etc.) at the same position within each string that are different.
// - If the strings are not of the same length, the function should return NaN.
// Examples:
// hammingDistance('abc', 'abc'); //=> 0
// hammingDistance('a1c', 'a2c'); //=> 1
// hammingDistance('!!!!', '****'); //=> 4
// hammingDistance('abc', 'ab'); //=> NaN
const hammingDistance = (str1, str2) => {
if (str1.length !== str2.length) {
return NaN;
}
let diffCount = 0;
for (let i = 0; i < str1.length; i++) {
if (str1[i] !== str2[i]) {
diffCount++;
}
}
return diffCount;
};