Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit f4d62ee

Browse files
committed
Hamming Distance
1 parent ac7c79f commit f4d62ee

File tree

4 files changed

+61
-0
lines changed

4 files changed

+61
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,5 @@ Once inside, run the command below:
1212
3. Most recurring character
1313
4. Sentence Capitalization
1414
5. Palindromes
15+
6. Hamming Distance
16+

src/hammingDistance/index.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// pick a solution and insert here to run the test.
2+
3+
4+
function hammingDistance(stringA, stringB) {
5+
let result = 0
6+
7+
if (stringA.length == stringB.length) {
8+
9+
for (let i = 0; i < stringA.length; i++) {
10+
if (stringA[i].toLowerCase() != stringB[i].toLowerCase()) {
11+
result++
12+
}
13+
}
14+
return result
15+
} else {
16+
throw new Error('Strings do not have equal length')
17+
}
18+
}
19+
20+
module.exports = hammingDistance

src/hammingDistance/solutions.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// USING FOR LOOP
2+
function hammingDistance(stringA, stringB) {
3+
let result = 0
4+
5+
if (stringA.length == stringB.length) {
6+
7+
for (let i = 0; i < stringA.length; i++) {
8+
if (stringA[i].toLowerCase() != stringB[i].toLowerCase()) {
9+
result++
10+
}
11+
}
12+
return result
13+
} else {
14+
throw new Error('Strings do not have equal length')
15+
}
16+
}

src/hammingDistance/test.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
const hammingDistance = require('./index')
2+
3+
test('hammingDistance is a function', () => {
4+
expect(typeof hammingDistance).toEqual('function')
5+
})
6+
7+
test('returns the hamming distance for letters', () => {
8+
expect(hammingDistance('river', 'rover')).toEqual(1)
9+
})
10+
11+
test('returns the hamming distance for numbers', () => {
12+
expect(hammingDistance('1011101', '1001001')).toEqual(2)
13+
})
14+
15+
test('returns the hamming distance for letters', () => {
16+
expect(hammingDistance('karolin', 'kerstin')).toEqual(3)
17+
})
18+
19+
test('returns the hamming distance for letters', () => {
20+
expect(hammingDistance('drummer', 'dresser')).toEqual(3)
21+
})
22+
23+

0 commit comments

Comments
 (0)