From 3348f5471589de116e49bed7ab07f384d72abd04 Mon Sep 17 00:00:00 2001 From: helloShen Date: Mon, 7 Feb 2022 18:13:59 -0500 Subject: [PATCH 1/3] Update calculator and it's unit tests --- calculator/calculator.js | 2 ++ calculator/calculator.spec.js | 42 ++++++++++++++++++++++++++++++++++- 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/calculator/calculator.js b/calculator/calculator.js index d45cc64..78e4a6f 100644 --- a/calculator/calculator.js +++ b/calculator/calculator.js @@ -21,6 +21,7 @@ const power = function(a, b) { }; const factorial = function(n) { + if (n < 0) return undefined; if (n === 0) return 1; let product = 1; for (let i = n; i > 0; i--) { @@ -32,6 +33,7 @@ const factorial = function(n) { // This is another implementation of Factorial that uses recursion // THANKS to @ThirtyThreeB! const recursiveFactorial = function(n) { + if (n < 0) return undefined; if (n === 0) { return 1; } diff --git a/calculator/calculator.spec.js b/calculator/calculator.spec.js index dc317ec..55c04a1 100644 --- a/calculator/calculator.spec.js +++ b/calculator/calculator.spec.js @@ -39,22 +39,62 @@ describe('sum', () => { }); describe('multiply', () => { + test.skip('computes the product of an empty array', () => { + expect(calculator.multiply([])).toBe(0); + }); test.skip('multiplies two numbers', () => { expect(calculator.multiply([2,4])).toBe(8); }); - test.skip('multiplies several numbers', () => { expect(calculator.multiply([2,4,6,8,10,12,14])).toBe(645120); }); }); +/* + * Base facts: + * => x to the power of 0 is always 1. + * => with base of 0, the exponent must NOT be negative, otherwise the result will be Infinity. + */ describe('power', () => { test.skip('raises one number to the power of another number', () => { expect(calculator.power(4,3)).toBe(64); // 4 to third power is 64 }); + test.skip('to the negative exponent', () => { + expect(calculator.power(2,-2)).toBe(0.25); + }); + test.skip('with negative base', () => { + expect(calculator.power(-2,2)).toBe(4); + }); + test.skip('with negative base', () => { + expect(calculator.power(-2,3)).toBe(-8); + }); + test.skip('negative base to the negative exponent', () => { + expect(calculator.power(-2,-2)).toBe(0.25); + }); + test.skip('negative base to the negative exponent', () => { + expect(calculator.power(-2,-3)).toBe(-0.125); + }); + test.skip('to the power of 0', () => { + expect(calculator.power(2,0)).toBe(1); + }); + test.skip('to the power of 0', () => { + expect(calculator.power(-2,0)).toBe(1); + }); + test.skip('to the power of 0', () => { + expect(calculator.power(0,0)).toBe(1); + }); + test.skip('with base of 0', () => { + expect(calculator.power(0,2)).toBe(0); + }); + test.skip('with base of 0', () => { + expect(calculator.power(0,-2)).toBe(Infinity); + }); }); describe('factorial', () => { + test.skip('factorial of negative number should be undefined', () => { + expect(calculator.factorial(-1)).toBeUndefined(); + }); test.skip('computes the factorial of 0', () => { expect(calculator.factorial(0)).toBe(1); // 0! = 1 }); From 997cb06a7f38a2cdaf0124f9a60f00bfc8e3882e Mon Sep 17 00:00:00 2001 From: helloShen Date: Mon, 7 Feb 2022 20:54:14 -0500 Subject: [PATCH 2/3] Add another version of power() function --- calculator/calculator.js | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/calculator/calculator.js b/calculator/calculator.js index 78e4a6f..f295a39 100644 --- a/calculator/calculator.js +++ b/calculator/calculator.js @@ -20,6 +20,23 @@ const power = function(a, b) { return Math.pow(a, b); }; +/* + * If you want to write a handmade power() function by yourself, + * Here's a simple example. + * Only work with integer as input. + */ +const intPower = function(base, exponent) { + if (exponent === 0) return 1; + if (base === 0) return (exponent > 0)? 0 : Infinity; + let result = 1; + if (exponent > 0) { + while (exponent-- > 0) result *= base; + } else { + while (exponent++ < 0) result /= base; + } + return result; +}; + const factorial = function(n) { if (n < 0) return undefined; if (n === 0) return 1; From e100fe81c39cb47348046319e99ec2f2894dab93 Mon Sep 17 00:00:00 2001 From: helloShen Date: Tue, 8 Feb 2022 14:19:06 -0500 Subject: [PATCH 3/3] Add recursive and dynamic programming solution to fibonacci --- fibonacci/fibonacci.js | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/fibonacci/fibonacci.js b/fibonacci/fibonacci.js index 29ea89d..8d0e059 100644 --- a/fibonacci/fibonacci.js +++ b/fibonacci/fibonacci.js @@ -1,6 +1,6 @@ const fibonacci = function(count) { if (count < 0) return "OOPS"; - if (count === 0) return 0; + if (count == 0) return 0; let a = 0; let b = 1; for (let i = 1; i < count; i++) { @@ -11,4 +11,25 @@ const fibonacci = function(count) { return b; }; -module.exports = fibonacci; +/* naive recursive */ +const fibonacciRecursive = function(count) { + if (count < 0) return 'OOPS'; + if (count == 0) return 0; + if (count == 1) return 1; + return fibonacciRecursive(count - 1) + fibonacciRecursive(count - 2); +} + +/* Dynamic Programming (DP) recursive */ +const fibonacciDp= function(count) { + return fibonacciDpHelper(count, {'0': 0, '1': 1}); +} + +const fibonacciDpHelper = function(count, memo) { + if (count < 0) return 'OOPS'; + if (memo.hasOwnProperty(`${count}`)) return memo[`${count}`]; + const result = fibonacciDpHelper(count - 1, memo) + fibonacciDpHelper(count - 2, memo); + memo[`${count}`] = result; + return result; +} + +module.exports = fibonacci; \ No newline at end of file