From d2616eda8cb7d907e1123b38f01c64112c67f97f Mon Sep 17 00:00:00 2001 From: Eric Ti Yu Chiang Date: Mon, 15 Oct 2018 14:44:08 -0400 Subject: [PATCH 1/4] Fix eslint errors in hamming exercise --- .eslintignore | 1 - exercises/hamming/example.js | 9 +++------ 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/.eslintignore b/.eslintignore index 0bec4e621a..0b3edb86c6 100644 --- a/.eslintignore +++ b/.eslintignore @@ -24,7 +24,6 @@ forth gigasecond grade-school grains -hamming hexadecimal isbn-verifier isogram diff --git a/exercises/hamming/example.js b/exercises/hamming/example.js index 6aaf00b095..68b4df7484 100644 --- a/exercises/hamming/example.js +++ b/exercises/hamming/example.js @@ -1,6 +1,5 @@ class Hamming { - - compute(strand1, strand2) { + static compute(strand1, strand2) { const len1 = strand1.length; const len2 = strand2.length; if (len1 !== len2) { @@ -8,17 +7,15 @@ class Hamming { } let distance = 0; - let idx = -1; const end = len1; // there could be len2, they're equal - while (++idx < end) { + for (let idx = 0; idx < end; idx += 1) { if (strand1[idx] !== strand2[idx]) { - distance++; + distance += 1; } } return distance; } - } export default Hamming; From 20cbe3740039f1698ae2f4eb6b0a771215812b71 Mon Sep 17 00:00:00 2001 From: Eric Ti Yu Chiang Date: Mon, 15 Oct 2018 15:51:19 -0400 Subject: [PATCH 2/4] export `compute` function instead of `Hamming` class This addresses the `Expected 'this' to be used by class method 'compute'` lint error. --- exercises/hamming/example.js | 30 ++++++++++++--------------- exercises/hamming/hamming.spec.js | 34 +++++++++++++++---------------- 2 files changed, 29 insertions(+), 35 deletions(-) diff --git a/exercises/hamming/example.js b/exercises/hamming/example.js index 68b4df7484..043a811d75 100644 --- a/exercises/hamming/example.js +++ b/exercises/hamming/example.js @@ -1,21 +1,17 @@ -class Hamming { - static compute(strand1, strand2) { - const len1 = strand1.length; - const len2 = strand2.length; - if (len1 !== len2) { - throw new Error('left and right strands must be of equal length'); - } +export const compute = (strand1, strand2) => { + const len1 = strand1.length; + const len2 = strand2.length; + if (len1 !== len2) { + throw new Error('left and right strands must be of equal length'); + } - let distance = 0; - const end = len1; // there could be len2, they're equal - for (let idx = 0; idx < end; idx += 1) { - if (strand1[idx] !== strand2[idx]) { - distance += 1; - } + let distance = 0; + const end = len1; // there could be len2, they're equal + for (let idx = 0; idx < end; idx += 1) { + if (strand1[idx] !== strand2[idx]) { + distance += 1; } - - return distance; } -} -export default Hamming; + return distance; +}; diff --git a/exercises/hamming/hamming.spec.js b/exercises/hamming/hamming.spec.js index 8aeb99f0ce..4d2b6f7c8f 100644 --- a/exercises/hamming/hamming.spec.js +++ b/exercises/hamming/hamming.spec.js @@ -1,68 +1,66 @@ -import Hamming from './hamming'; +import { compute } from './hamming'; describe('Hamming', () => { - const hamming = new Hamming(); - test('no difference between empty strands', () => { - expect(hamming.compute('', '')).toEqual(0); + expect(compute('', '')).toEqual(0); }); xtest('no difference between identical strands', () => { - expect(hamming.compute('A', 'A')).toEqual(0); + expect(compute('A', 'A')).toEqual(0); }); xtest('long identical strands', () => { - expect(hamming.compute('GGACTGA', 'GGACTGA')).toEqual(0); + expect(compute('GGACTGA', 'GGACTGA')).toEqual(0); }); xtest('complete distance in single nucleotide strands', () => { - expect(hamming.compute('A', 'G')).toEqual(1); + expect(compute('A', 'G')).toEqual(1); }); xtest('complete distance in small strands', () => { - expect(hamming.compute('AG', 'CT')).toEqual(2); + expect(compute('AG', 'CT')).toEqual(2); }); xtest('small distance in small strands', () => { - expect(hamming.compute('AT', 'CT')).toEqual(1); + expect(compute('AT', 'CT')).toEqual(1); }); xtest('small distance', () => { - expect(hamming.compute('GGACG', 'GGTCG')).toEqual(1); + expect(compute('GGACG', 'GGTCG')).toEqual(1); }); xtest('small distance in long strands', () => { - expect(hamming.compute('ACCAGGG', 'ACTATGG')).toEqual(2); + expect(compute('ACCAGGG', 'ACTATGG')).toEqual(2); }); xtest('non-unique character in first strand', () => { - expect(hamming.compute('AAG', 'AAA')).toEqual(1); + expect(compute('AAG', 'AAA')).toEqual(1); }); xtest('non-unique character in second strand', () => { - expect(hamming.compute('AAA', 'AAG')).toEqual(1); + expect(compute('AAA', 'AAG')).toEqual(1); }); xtest('same nucleotides in different positions', () => { - expect(hamming.compute('TAG', 'GAT')).toEqual(2); + expect(compute('TAG', 'GAT')).toEqual(2); }); xtest('large distance', () => { - expect(hamming.compute('GATACA', 'GCATAA')).toEqual(4); + expect(compute('GATACA', 'GCATAA')).toEqual(4); }); xtest('large distance in off-by-one strand', () => { - expect(hamming.compute('GGACGGATTCTG', 'AGGACGGATTCT')).toEqual(9); + expect(compute('GGACGGATTCTG', 'AGGACGGATTCT')).toEqual(9); }); xtest('disallow first strand longer', () => { - expect(() => hamming.compute('AATG', 'AAA')).toThrow( + expect(() => compute('AATG', 'AAA')).toThrow( new Error('left and right strands must be of equal length'), ); }); xtest('disallow second strand longer', () => { - expect(() => hamming.compute('ATA', 'AGTG')).toThrow( + expect(() => compute('ATA', 'AGTG')).toThrow( new Error('left and right strands must be of equal length'), ); }); From ee30e2750963758979a7283707aa166536019062 Mon Sep 17 00:00:00 2001 From: Eric Ti Yu Chiang Date: Tue, 16 Oct 2018 11:20:30 -0400 Subject: [PATCH 3/4] Use `array.reduce` instead of for loop Per feedback at https://github.com/exercism/javascript/pull/505#pullrequestreview-165118429 prefer `array.reduce` over for loop. --- exercises/hamming/example.js | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/exercises/hamming/example.js b/exercises/hamming/example.js index 043a811d75..054f7c74e8 100644 --- a/exercises/hamming/example.js +++ b/exercises/hamming/example.js @@ -5,13 +5,8 @@ export const compute = (strand1, strand2) => { throw new Error('left and right strands must be of equal length'); } - let distance = 0; - const end = len1; // there could be len2, they're equal - for (let idx = 0; idx < end; idx += 1) { - if (strand1[idx] !== strand2[idx]) { - distance += 1; - } - } - - return distance; + return [...strand1].reduce( + (distance, _, i) => (strand1[i] !== strand2[i] ? distance + 1 : distance), + 0, + ); }; From 1b6ff01997303746112fd255f10fa37d8c3b69b2 Mon Sep 17 00:00:00 2001 From: Eric Ti Yu Chiang Date: Tue, 16 Oct 2018 14:57:42 -0400 Subject: [PATCH 4/4] Use `filter` instead of `reduce` Based on feedback from https://github.com/exercism/javascript/pull/505#pullrequestreview-165309766 using `filter` here is semantically appropriate. --- exercises/hamming/example.js | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/exercises/hamming/example.js b/exercises/hamming/example.js index 054f7c74e8..6ff63e5c61 100644 --- a/exercises/hamming/example.js +++ b/exercises/hamming/example.js @@ -5,8 +5,5 @@ export const compute = (strand1, strand2) => { throw new Error('left and right strands must be of equal length'); } - return [...strand1].reduce( - (distance, _, i) => (strand1[i] !== strand2[i] ? distance + 1 : distance), - 0, - ); + return [...strand1].filter((element, index) => element !== strand2[index]).length; };