Passed "sum" using reduce() array method in exercise 08

This commit is contained in:
NetMan 2024-01-11 09:59:22 +01:00
parent f4e1b0d95a
commit bcd6da249a
2 changed files with 9 additions and 6 deletions

View File

@ -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() {

View File

@ -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);
});
});