From bcd6da249aa08ac55b809b09998a241783306bf7 Mon Sep 17 00:00:00 2001 From: NetMan <13informatyka14@gmail.com> Date: Thu, 11 Jan 2024 09:59:22 +0100 Subject: [PATCH] Passed "sum" using reduce() array method in exercise 08 --- 08_calculator/calculator.js | 7 +++++-- 08_calculator/calculator.spec.js | 8 ++++---- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/08_calculator/calculator.js b/08_calculator/calculator.js index a89ce8c..28a6476 100644 --- a/08_calculator/calculator.js +++ b/08_calculator/calculator.js @@ -6,8 +6,11 @@ const subtract = function(a, b) { return a - b; }; -const sum = function() { - +const sum = function(array) { + const output = array.reduce((total, value) => { + return total + value; + }, 0); + return output; }; const multiply = function() { diff --git a/08_calculator/calculator.spec.js b/08_calculator/calculator.spec.js index 996f4c9..33e9367 100644 --- a/08_calculator/calculator.spec.js +++ b/08_calculator/calculator.spec.js @@ -21,19 +21,19 @@ describe('subtract', () => { }); describe('sum', () => { - test.skip('computes the sum of an empty array', () => { + test('computes the sum of an empty array', () => { expect(calculator.sum([])).toBe(0); }); - test.skip('computes the sum of an array of one number', () => { + test('computes the sum of an array of one number', () => { expect(calculator.sum([7])).toBe(7); }); - test.skip('computes the sum of an array of two numbers', () => { + test('computes the sum of an array of two numbers', () => { expect(calculator.sum([7, 11])).toBe(18); }); - test.skip('computes the sum of an array of many numbers', () => { + test('computes the sum of an array of many numbers', () => { expect(calculator.sum([1, 3, 5, 7, 9])).toBe(25); }); });