Skip to content

Commit

Permalink
Merge pull request #68 from ColinX13/colin4
Browse files Browse the repository at this point in the history
Solutions and test #61
  • Loading branch information
yjlim5 authored Jun 5, 2017
2 parents fde5bb0 + 377c1c5 commit 7c1ecfd
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
23 changes: 23 additions & 0 deletions solutions/61.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Takes in two numbers and returns their greatest common denominator (GCD)

// Input: 16,24
// Output: 8

// Solutions by Colin Xie @ColinX13

/**
* 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
*/
const solution = (num1, num2) => {
for(let factor = num1; factor >= 1; factor--){
if(num1 % factor === 0 && num2 % factor === 0){
return factor;
}
}
};
module.exports = {
solution
};
24 changes: 24 additions & 0 deletions test/61.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const expect = require('chai').expect;
let solution = require('../solutions/61').solution;
// solution = require('./yourSolution').solution;

describe('greatest common denominator', () => {
it('the gcd for 16 and 24 is 8', () => {
expect(solution(16,24)).eql(8);
});
it('the gcd for 5 and 6 is 1', () => {
expect(solution(5,6)).eql(1);
});
it('the gcd for 50 and 60 is 10', () => {
expect(solution(50,60)).eql(10);
});
it('the gcd for 40 and 20 is 20', () => {
expect(solution(40,20)).eql(20);
});
it('the gcd for 20 and 40 is 20', () => {
expect(solution(20,40)).eql(20);
});
it('the gcd for 1 and 4 is 1', () => {
expect(solution(1,4)).eql(1);
});
});

0 comments on commit 7c1ecfd

Please sign in to comment.