From db3f9e4d54f678277adbac0301e05f3d6fa63da7 Mon Sep 17 00:00:00 2001 From: Tony Le Date: Tue, 26 Nov 2024 16:29:13 -0500 Subject: [PATCH 1/2] Adding sum func --- flag1/mafs.js | 8 ++++---- flag1/mafs.test.js | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/flag1/mafs.js b/flag1/mafs.js index 4a9f43b..51c234a 100644 --- a/flag1/mafs.js +++ b/flag1/mafs.js @@ -18,8 +18,8 @@ function divide(a, b) { } } -// function sum(...args) { -// return args.reduce((acc, curr) => acc + curr, 0); -// } +function sum(...args) { + return args.reduce((acc, curr) => acc + curr, 0); +} -module.exports = { add, subtract, multiply, divide }; +module.exports = { add, subtract, multiply, divide, sum }; diff --git a/flag1/mafs.test.js b/flag1/mafs.test.js index 2d37cb8..a7179d5 100644 --- a/flag1/mafs.test.js +++ b/flag1/mafs.test.js @@ -1,4 +1,4 @@ -const { add, subtract, multiply, divide } = require("./mafs"); +const { add, subtract, multiply, divide, sum } = require("./mafs"); test("adds 1 + 2 to equal 3", () => { expect(add(1, 2)).toBe(3); @@ -20,6 +20,6 @@ test('divides 8 / 2 to equal 4', () => { expect(divide(8, 2)).toBe(4); }); -// test('sums 1 + 2 + 3 + 4 to equal 10', () => { -// expect(sum(1, 2, 3, 4)).toBe(10); -// }); +test('sums 1 + 2 + 3 + 4 to equal 10', () => { + expect(sum(1, 2, 3, 4)).toBe(10); +}); From 13a8b089e2355819add5bb06b6cb73a5fc27e50c Mon Sep 17 00:00:00 2001 From: Tony Le Date: Wed, 27 Nov 2024 16:12:38 -0500 Subject: [PATCH 2/2] Sum 0 test --- flag1/mafs.js | 1 + flag1/mafs.test.js | 4 ++++ 2 files changed, 5 insertions(+) diff --git a/flag1/mafs.js b/flag1/mafs.js index 51c234a..819ce1b 100644 --- a/flag1/mafs.js +++ b/flag1/mafs.js @@ -19,6 +19,7 @@ function divide(a, b) { } function sum(...args) { + if (args.length === 0) return 0; return args.reduce((acc, curr) => acc + curr, 0); } diff --git a/flag1/mafs.test.js b/flag1/mafs.test.js index a7179d5..fbd16a0 100644 --- a/flag1/mafs.test.js +++ b/flag1/mafs.test.js @@ -23,3 +23,7 @@ test('divides 8 / 2 to equal 4', () => { test('sums 1 + 2 + 3 + 4 to equal 10', () => { expect(sum(1, 2, 3, 4)).toBe(10); }); + +test('sums 1 + 2 + 3 + 4 to equal 10', () => { + expect(sum()).toBe(0); +});