File tree 4 files changed +61
-0
lines changed 4 files changed +61
-0
lines changed Original file line number Diff line number Diff line change @@ -12,3 +12,5 @@ Once inside, run the command below:
12
12
3 . Most recurring character
13
13
4 . Sentence Capitalization
14
14
5 . Palindromes
15
+ 6 . Hamming Distance
16
+
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+
You can’t perform that action at this time.
0 commit comments