From 6e118484eb7a65296642a9e401f58ee8242af181 Mon Sep 17 00:00:00 2001 From: seemcat Date: Mon, 5 Jun 2017 10:45:35 -0700 Subject: [PATCH 1/4] Test & Solution for #61 --- solutions/61.js | 54 ++++++++++++++++++++++++++++++++++++++++++++++--- test/61.js | 1 + 2 files changed, 52 insertions(+), 3 deletions(-) diff --git a/solutions/61.js b/solutions/61.js index a65a660..2fa0275 100644 --- a/solutions/61.js +++ b/solutions/61.js @@ -3,8 +3,8 @@ // Input: 16,24 // Output: 8 -// Solutions by Colin Xie @ColinX13 - +// Solution #0 by Colin Xie @ColinX13 +// Solution #1 by Maricris Bonzo @seemcat /** * Factor is determined when both remainders of num1 and num2 are 0 when factor is divided from them. * @param num1 - the first number input @@ -18,6 +18,54 @@ const solution = (num1, num2) => { } } }; + +const solution1 = (num1, num2) => { + if (num1 > num2){ + let long = num1; + let short = num2; + } else { + let long = num2; + let short = num1; + }; + + let num1Array = []; + let num2Array = []; + let i = 1; + let j = 1; + + while (i < long) { + if (long%i === 0){ + num1Array.push(i); + i++; + } else { + i++; + } + }; + + while (j < short) { + if (short%i === 0){ + num2Array.push(j); + j++; + } else { + j++; + } + }; + + let k, l = 0; + let commonNumbers = []; + for (k; k < num1Array.length; k++){ + for (l; l < num2Array.length; l++){ + if (num1Array[k] === num2Array[l]){ + commonNumbers.push(num1Array[k]); + } + } + }; + + let GCD = Math.max(commonNumbers); + return GCD; +}; + module.exports = { - solution + solution, + solution1 }; diff --git a/test/61.js b/test/61.js index 49d6f3b..b8fa8e6 100644 --- a/test/61.js +++ b/test/61.js @@ -1,5 +1,6 @@ const expect = require('chai').expect; let solution = require('../solutions/61').solution; +let solution1 = require('../solutions/61').solution; // solution = require('./yourSolution').solution; describe('greatest common denominator', () => { From c3142331292b1b0f74d308c26e2bfe10ff413d0a Mon Sep 17 00:00:00 2001 From: seemcat Date: Tue, 6 Jun 2017 12:02:29 -0700 Subject: [PATCH 2/4] Modified test file for #61 --- test/61.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/61.js b/test/61.js index b8fa8e6..f653159 100644 --- a/test/61.js +++ b/test/61.js @@ -14,12 +14,12 @@ describe('greatest common denominator', () => { expect(solution(50,60)).eql(10); }); it('the gcd for 40 and 20 is 20', () => { - expect(solution(40,20)).eql(20); + expect(solution1(40,20)).eql(20); }); it('the gcd for 20 and 40 is 20', () => { - expect(solution(20,40)).eql(20); + expect(solution1(20,40)).eql(20); }); it('the gcd for 1 and 4 is 1', () => { - expect(solution(1,4)).eql(1); + expect(solution1(1,4)).eql(1); }); }); From 5c5967894c5c2c4dd24b42c04a6d3b3680bb4813 Mon Sep 17 00:00:00 2001 From: seemcat Date: Wed, 7 Jun 2017 08:20:13 -0700 Subject: [PATCH 3/4] Modified test file for #61 again --- test/61.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/61.js b/test/61.js index f653159..228a628 100644 --- a/test/61.js +++ b/test/61.js @@ -1,6 +1,6 @@ const expect = require('chai').expect; let solution = require('../solutions/61').solution; -let solution1 = require('../solutions/61').solution; +let solution1 = require('../solutions/61').solution1; // solution = require('./yourSolution').solution; describe('greatest common denominator', () => { From 5684ed99286ff9ade574c7e37b2edc31cc49178a Mon Sep 17 00:00:00 2001 From: seemcat Date: Thu, 8 Jun 2017 11:07:47 -0700 Subject: [PATCH 4/4] Corrected solution for #61. --- solutions/61.js | 42 ++++++++++++++++++------------------------ 1 file changed, 18 insertions(+), 24 deletions(-) diff --git a/solutions/61.js b/solutions/61.js index 2fa0275..166304f 100644 --- a/solutions/61.js +++ b/solutions/61.js @@ -19,49 +19,43 @@ const solution = (num1, num2) => { } }; -const solution1 = (num1, num2) => { - if (num1 > num2){ - let long = num1; - let short = num2; - } else { - let long = num2; - let short = num1; - }; - +const solution1 = (num1, num2) => { let num1Array = []; let num2Array = []; let i = 1; let j = 1; - while (i < long) { - if (long%i === 0){ + while (i <= num1) { + if (num1%i === 0){ num1Array.push(i); i++; } else { i++; } }; - - while (j < short) { - if (short%i === 0){ + + while (j <= num2) { + if (num2%j === 0){ num2Array.push(j); j++; } else { j++; } }; - - let k, l = 0; + + let k = 0; let commonNumbers = []; - for (k; k < num1Array.length; k++){ - for (l; l < num2Array.length; l++){ - if (num1Array[k] === num2Array[l]){ - commonNumbers.push(num1Array[k]); - } - } + + while (k < num2Array.length){ + if (num1Array.indexOf(num2Array[k]) === -1){ + k++; + } else { + commonNumbers.push(num2Array[k]); + k++; + }; }; - - let GCD = Math.max(commonNumbers); + + let GCD = Math.max.apply(Math, commonNumbers); return GCD; };