From 0b63649557da07c270d1ef789195289457c9172d Mon Sep 17 00:00:00 2001 From: KhotKeys Date: Fri, 7 Aug 2026 16:06:31 +0100 Subject: [PATCH 1/3] implement sum function and tests --- Sprint-1/implement/sum.js | 1 + Sprint-1/implement/sum.test.js | 34 ++++++++++++++++++---------------- 2 files changed, 19 insertions(+), 16 deletions(-) diff --git a/Sprint-1/implement/sum.js b/Sprint-1/implement/sum.js index 9062aafe3..6230724e3 100644 --- a/Sprint-1/implement/sum.js +++ b/Sprint-1/implement/sum.js @@ -1,4 +1,5 @@ function sum(elements) { + return elements.filter(n => typeof n === "number").reduce((acc, n) => acc + n, 0); } module.exports = sum; diff --git a/Sprint-1/implement/sum.test.js b/Sprint-1/implement/sum.test.js index dd0a090ca..cff277f5f 100644 --- a/Sprint-1/implement/sum.test.js +++ b/Sprint-1/implement/sum.test.js @@ -13,24 +13,26 @@ const sum = require("./sum.js"); // Given an empty array // When passed to the sum function // Then it should return 0 -test.todo("given an empty array, returns 0") +test("given an empty array, returns 0", () => { + expect(sum([])).toEqual(0); +}); -// Given an array with just one number -// When passed to the sum function -// Then it should return that number +test("given an array with just one number, returns that number", () => { + expect(sum([42])).toEqual(42); +}); -// Given an array containing negative numbers -// When passed to the sum function -// Then it should still return the correct total sum +test("given an array containing negative numbers, returns the correct total", () => { + expect(sum([10, -5, 3])).toEqual(8); +}); -// Given an array with decimal/float numbers -// When passed to the sum function -// Then it should return the correct total sum +test("given an array with decimal numbers, returns the correct total", () => { + expect(sum([1.5, 2.5])).toEqual(4); +}); -// Given an array containing non-number values -// When passed to the sum function -// Then it should ignore the non-numerical values and return the sum of the numerical elements +test("given an array containing non-number values, ignores them and sums the rest", () => { + expect(sum(["hey", 10, "hi", 60, 10])).toEqual(80); +}); -// Given an array with only non-number values -// When passed to the sum function -// Then it should return the least surprising value given how it behaves for all other inputs +test("given an array with only non-number values, returns 0", () => { + expect(sum(["a", "b"])).toEqual(0); +}); From 4213a22c9b80b28cc6cf7ff1a98427a50b15d455 Mon Sep 17 00:00:00 2001 From: KhotKeys Date: Fri, 7 Aug 2026 16:19:05 +0100 Subject: [PATCH 2/3] implement max function and tests --- Sprint-1/implement/max.js | 2 ++ Sprint-1/implement/max.test.js | 40 ++++++++++++++++++---------------- 2 files changed, 23 insertions(+), 19 deletions(-) diff --git a/Sprint-1/implement/max.js b/Sprint-1/implement/max.js index 6dd76378e..e5462ddfa 100644 --- a/Sprint-1/implement/max.js +++ b/Sprint-1/implement/max.js @@ -1,4 +1,6 @@ function findMax(elements) { + const numbers = elements.filter(n => typeof n === "number"); + return Math.max(...numbers); } module.exports = findMax; diff --git a/Sprint-1/implement/max.test.js b/Sprint-1/implement/max.test.js index 82f18fd88..393b73d69 100644 --- a/Sprint-1/implement/max.test.js +++ b/Sprint-1/implement/max.test.js @@ -16,28 +16,30 @@ const findMax = require("./max.js"); // When passed to the max function // Then it should return -Infinity // Delete this test.todo and replace it with a test. -test.todo("given an empty array, returns -Infinity"); +test("given an empty array, returns -Infinity", () => { + expect(findMax([])).toEqual(-Infinity); +}); -// Given an array with one number -// When passed to the max function -// Then it should return that number +test("given an array with one number, returns that number", () => { + expect(findMax([7])).toEqual(7); +}); -// Given an array with both positive and negative numbers -// When passed to the max function -// Then it should return the largest number overall +test("given an array with positive and negative numbers, returns the largest", () => { + expect(findMax([-3, 0, 5, -1])).toEqual(5); +}); -// Given an array with just negative numbers -// When passed to the max function -// Then it should return the closest one to zero +test("given an array with just negative numbers, returns the closest to zero", () => { + expect(findMax([-10, -3, -7])).toEqual(-3); +}); -// Given an array with decimal numbers -// When passed to the max function -// Then it should return the largest decimal number +test("given an array with decimal numbers, returns the largest decimal", () => { + expect(findMax([1.1, 2.9, 2.1])).toEqual(2.9); +}); -// Given an array with non-number values -// When passed to the max function -// Then it should return the max and ignore non-numeric values +test("given an array with non-number values, ignores them and returns the max", () => { + expect(findMax(["hey", 10, "hi", 60, 10])).toEqual(60); +}); -// Given an array with only non-number values -// When passed to the max function -// Then it should return the least surprising value given how it behaves for all other inputs +test("given an array with only non-number values, returns -Infinity", () => { + expect(findMax(["a", "b"])).toEqual(-Infinity); +}); From d2c79da40dcb3350a41ff8dc5533769cc91eebb9 Mon Sep 17 00:00:00 2001 From: KhotKeys Date: Fri, 7 Aug 2026 16:59:57 +0100 Subject: [PATCH 3/3] refactor includes to use for...of loop --- Sprint-1/refactor/includes.js | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/Sprint-1/refactor/includes.js b/Sprint-1/refactor/includes.js index 29dad81f0..cc167f1bb 100644 --- a/Sprint-1/refactor/includes.js +++ b/Sprint-1/refactor/includes.js @@ -1,11 +1,8 @@ // Refactor the implementation of includes to use a for...of loop function includes(list, target) { - for (let index = 0; index < list.length; index++) { - const element = list[index]; - if (element === target) { - return true; - } + for (const element of list) { + if (element === target) return true; } return false; }