From afbd11dad191fa5d35f7e27c7963d51f67b8ae3b Mon Sep 17 00:00:00 2001 From: NetMan <13informatyka14@gmail.com> Date: Thu, 11 Jan 2024 10:05:10 +0100 Subject: [PATCH] Passed "factorial" using recursive function in exercise 08 --- 08_calculator/calculator.js | 7 +++++-- 08_calculator/calculator.spec.js | 10 +++++----- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/08_calculator/calculator.js b/08_calculator/calculator.js index 12dd568..cebeaf5 100644 --- a/08_calculator/calculator.js +++ b/08_calculator/calculator.js @@ -24,8 +24,11 @@ const power = function(a, x) { return 4 ** 3; }; -const factorial = function() { - +const factorial = function(a) { + if (a <= 1) { + return 1; + } + return a * factorial(a - 1); }; // Do not edit below this line diff --git a/08_calculator/calculator.spec.js b/08_calculator/calculator.spec.js index 119673a..59956fe 100644 --- a/08_calculator/calculator.spec.js +++ b/08_calculator/calculator.spec.js @@ -55,23 +55,23 @@ describe('power', () => { }); 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); }); });