From 614b9ff903aca09f37b76714950a7aee5cabcd28 Mon Sep 17 00:00:00 2001 From: reddforman Date: Mon, 14 Nov 2022 08:13:04 -0800 Subject: [PATCH] Add files via upload --- 08_calculator/calculator.js | 24 ++++++++++++++++-------- 08_calculator/calculator.spec.js | 12 ++++++------ 2 files changed, 22 insertions(+), 14 deletions(-) diff --git a/08_calculator/calculator.js b/08_calculator/calculator.js index 298a9f4..a578460 100644 --- a/08_calculator/calculator.js +++ b/08_calculator/calculator.js @@ -11,11 +11,11 @@ const sum = function(array) { if (array.length == 0) { return 0; } else { - for (let i = 0; i < array.length; i++) { - sumArray += array[i]; + for (let i = 0; i < array.length; i++) { + sumArray += array[i]; + } + return sumArray; } - return sumArray; -} }; const multiply = function(numbers) { @@ -26,12 +26,20 @@ const multiply = function(numbers) { return multiplyNum; }; -const power = function() { - +const power = function(num, power) { + powerNum = 1; + for (let i = 0; i < power; i++) { + powerNum *= num; + } + return powerNum; }; -const factorial = function() { - +const factorial = function(fact) { + factNum = 1; + for (let i = fact; i > 0; i--) { + factNum *= i; + } + return factNum; }; // Do not edit below this line diff --git a/08_calculator/calculator.spec.js b/08_calculator/calculator.spec.js index f617e63..c82a04b 100644 --- a/08_calculator/calculator.spec.js +++ b/08_calculator/calculator.spec.js @@ -49,29 +49,29 @@ describe('multiply', () => { }); describe('power', () => { - test.skip('raises one number to the power of another number', () => { + test('raises one number to the power of another number', () => { expect(calculator.power(4,3)).toBe(64); // 4 to third power is 64 }); }); describe('factorial', () => { - test.skip('computes the factorial of 0', () => { + test('computes the factorial of 0', () => { expect(calculator.factorial(0)).toBe(1); // 0! = 1 }); - test.skip('computes the factorial of 1', () => { + test('computes the factorial of 1', () => { expect(calculator.factorial(1)).toBe(1); }); - test.skip('computes the factorial of 2', () => { + test('computes the factorial of 2', () => { expect(calculator.factorial(2)).toBe(2); }); - test.skip('computes the factorial of 5', () => { + test('computes the factorial of 5', () => { expect(calculator.factorial(5)).toBe(120); }); - test.skip('computes the factorial of 10', () => { + test('computes the factorial of 10', () => { expect(calculator.factorial(10)).toBe(3628800); }); });