Skip to content

Instantly share code, notes, and snippets.

@sdesalas
Last active May 23, 2019 10:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sdesalas/ef7bfa82457125cd5f3083cc16a1a8a7 to your computer and use it in GitHub Desktop.
Save sdesalas/ef7bfa82457125cd5f3083cc16a1a8a7 to your computer and use it in GitHub Desktop.
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;
};
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);
});
});
@sdesalas
Copy link
Author

sdesalas commented May 23, 2019

To get started:

$ mkdir gcd_dojo

$ cd gcd_dojo

$ npm init -y
Wrote to /projects/wcs/gcd_dojo/package.json:

{
  "name": "gcd_dojo",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC"
}

$ npm i mocha

npm notice created a lockfile as package-lock.json. You should commit this file.
npm WARN gcd_dojo@1.0.0 No description
npm WARN gcd_dojo@1.0.0 No repository field.

+ mocha@6.1.4
added 115 packages from 509 contributors and audited 224 packages in 17.868s
found 0 vulnerabilities

$ code .

Then continue in VSCode and execute npm test from inbuilt terminal.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment