Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 46 additions & 4 deletions solutions/61.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
};
7 changes: 4 additions & 3 deletions test/61.js
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand All @@ -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);
});
});