From e1c2c5edc0af75e2c6d0dd62c2105cb74e61a2c8 Mon Sep 17 00:00:00 2001 From: Vojta Oupicky Date: Mon, 12 Feb 2018 18:51:32 +0100 Subject: [PATCH 1/2] Ignoring .idea/ now --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index fd4f2b0..00a730f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ node_modules .DS_Store +.idea/ \ No newline at end of file From 6f1d75ea99a83011d607c1c508ccbd0a2db9c715 Mon Sep 17 00:00:00 2001 From: Vojta Oupicky Date: Mon, 12 Feb 2018 19:13:03 +0100 Subject: [PATCH 2/2] Gcd function and tests Function findGcd : finds the greatest common divider of two numbers. --- sketch.js | 42 ++++++++++++++++++++++++---------------- sketch.test.js | 52 +++++++------------------------------------------- 2 files changed, 33 insertions(+), 61 deletions(-) diff --git a/sketch.js b/sketch.js index 152defc..a7b3c79 100644 --- a/sketch.js +++ b/sketch.js @@ -1,36 +1,46 @@ function sum(a, b) { - return a + b; + return a + b; } -function sayHelloTo(username){ - return "Hello, " + username + "!"; +function sayHelloTo(username) { + return "Hello, " + username + "!"; } -function sub(a, b){ - return a - b; +function sub(a, b) { + return a - b; } function prod(a, b) { - return a * b; + return a * b; } function answer() { - return 42; + return 42; } function digital_root(n) { - return (n-1)%9+1; + return (n - 1) % 9 + 1; } + function sum42(a, b) { - return a + b + 42; + return a + b + 42; +} + +function findGcd(a, b) { + if (!b) { + return a; + } + + return findGcd(b, a % b); } module.exports = { - sum: sum, - sub: sub, - prod: prod, - sum42: sum42, - digital_root: digital_root, - hello: sayHelloTo, - answer: answer + sum: sum, + sub: sub, + prod: prod, + sum42: sum42, + digital_root: digital_root, + hello: sayHelloTo, + answer: answer, + gcd: findGcd } diff --git a/sketch.test.js b/sketch.test.js index 10c2fc8..b0400cb 100644 --- a/sketch.test.js +++ b/sketch.test.js @@ -1,59 +1,21 @@ -const { sum, prod, digital_root, sum42 } = require('./sketch'); +const {sum, prod, digital_root, sum42, gcd} = require('./sketch'); // test('adds 1 + 2 to equal 3', () => { // expect(sum(1, 2)).toBe(3); // }); -/** - * Sum to be defined - */ - -test('Sum function exists', () => { - expect(sum).toBeDefined(); -}); - test('adds 1 + 2 to equal 3', sumTest); function sumTest() { - expect(sum(1, 2)).toBe(3); + expect(sum(1, 2)).toBe(3); } function helloTest() { - expect(sayHelloTo("Dan")).toBe("Hello, Dan!"); + expect(sayHelloTo("Dan")).toBe("Hello, Dan!"); } -/** - * Prod to be defined - */ - -test('Prod function exists',()=>{ - expect(prod).toBeDefined(); -}); - -/** - * prod function output - */ - -test('prod calculates 2 * 10 = 20', () => { - expect(prod(2, 10)).toBe(20); -}); - -/** - * Digital Root to be defined - */ - - test('digital_root function exists', ()=>{ - expect('digital_root').toBeDefined(); - }); - -test('digital root of 265 should equal 4', () => { - expect(digital_root(265)).toBe(4); -}) - -test('Sum42 function exists', () => { - expect(sum42).toBeDefined(); -}); +test('GCD of 40 and 20', gcdSimpleTest); -test('Sum42 3 + 1 should be 46', () => { - expect(sum42(3, 1)).toBe(46); -}); +function gcdSimpleTest() { + expect(gcd(20, 40)).toBe(20); +} \ No newline at end of file