Last active
May 23, 2019 10:46
-
-
Save sdesalas/ef7bfa82457125cd5f3083cc16a1a8a7 to your computer and use it in GitHub Desktop.
TDD - Greatest Common Divisor (https://learn.freecodecamp.org/coding-interview-prep/rosetta-code/greatest-common-divisor)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = function gcd(a, b) { | |
let arra = []; | |
let arrb = [] | |
for (let i=0; i<= a; i++){ | |
if (a%i ===0) { | |
arra.push(i); | |
} | |
} | |
for (let j=0; j<= b; j++){ | |
if (b%j ===0) { | |
arrb.push(j); | |
} | |
} | |
// arra = [1,2,5], arrb = [1,3,5] | |
let common = arra.filter(item => arrb.includes(item)); // x3 , common = [] | |
// 1-> item = 1 ---> arrb.includes(1) ?? ===> true ---> common [1] | |
// 2-> item = 2 ---> arrb.includes(2) ?? ===> false --> common [1] | |
// 3-> item = 5 ---> arrb.includes(5) ?? ===> true ---> common [1,5] | |
return common.pop(); // quita el ultimo elemento, primero usa .shift() | |
} | |
// otra solucion mas corta | |
module.exports = function gcd(a, b) { | |
let result; // PASS 1 // PASS 2 // PASS 3 | |
while ((a % b) > 0) { // a = 12, b = 18, % = 12 // a = 18, b = 12, % = 6 // a = 12, b = 6, % = 0 | |
result = a % b; // result = 12 // result = 6 // -> sale , result = 6 | |
a = b; // a = 18 // a = 12 | |
b = result; // b = 12 // b = 6 | |
} | |
return b; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const assert = require ('assert'); | |
const gcd = require ('./gcd.js'); | |
describe ('Testing GCD', () => { | |
// gcd should be a function | |
it('should be a function', () => { | |
assert.equal(typeof gcd, 'function'); | |
}) | |
// gcd(24,36) should return a number | |
it('should return "number" when encoding 24, 36', () => { | |
assert.equal(typeof gcd(24,36), 'number'); | |
}) | |
// gcd(24,36) should return 12 | |
it('should return "number" when encoding 24, 36', () => { | |
assert.equal(gcd(24,36), 12); | |
}) | |
// gcd(30,48) should return 6 | |
it('should return "6" when encoding (30,48) ', () => { | |
assert.equal(gcd(30,48), 6); | |
}) | |
// gcd(10,15) should return 5 | |
it('should return "5" when encoding (10,15)', () => { | |
assert.equal(gcd(10,15), 5); | |
}) | |
// gcd(100,25) should return 25 | |
it('should return "25" when encoding (100,25)', () => { | |
assert.equal(gcd(100,25), 25); | |
}) | |
// gcd(13,250) should return 1 | |
it('should return "1" when encoding (13,250)', () => { | |
assert.equal(gcd(13, 250), 1); | |
}); | |
//gcd(1300,250) should return 50 | |
it('should return "50" when encoding (1300,250)', () => { | |
assert.equal(gcd(1300, 250), 50); | |
}); | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To get started:
Then continue in VSCode and execute
npm test
from inbuilt terminal.