diff --git a/solutions/61.js b/solutions/61.js index 6a75396..c4a5c19 100644 --- a/solutions/61.js +++ b/solutions/61.js @@ -3,11 +3,11 @@ // 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. +/** + * Factor is determined when both remainders of num1 and num2 are 0 when factor is divided from them. * @param num1 - the first number input * @param num2 - the second number input * @return {number} factor - the greatest common factor for num1 and num2 @@ -19,6 +19,48 @@ const solution = (num1, num2) => { } } }; + +const solution1 = (num1, num2) => { + let num1Array = []; + let num2Array = []; + let i = 1; + let j = 1; + + while (i <= num1) { + if (num1%i === 0){ + num1Array.push(i); + i++; + } else { + i++; + } + }; + + while (j <= num2) { + if (num2%j === 0){ + num2Array.push(j); + j++; + } else { + j++; + } + }; + + let k = 0; + let commonNumbers = []; + + while (k < num2Array.length){ + if (num1Array.indexOf(num2Array[k]) === -1){ + k++; + } else { + commonNumbers.push(num2Array[k]); + k++; + }; + }; + + let GCD = Math.max.apply(Math, commonNumbers); + return GCD; +}; + module.exports = { solution, + solution1 }; diff --git a/test/61.js b/test/61.js index e4106ef..a42bea1 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').solution1; // solution = require('./yourSolution').solution; describe('greatest common denominator', () => { @@ -13,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); }); });