From 7d8f40217b35f51aaa9e271c5627a222a2a27cf0 Mon Sep 17 00:00:00 2001 From: ChinweP Date: Mon, 27 Jul 2026 16:02:59 +0100 Subject: [PATCH 1/5] Fix calculateMedian implementation so it passes all tests --- Sprint-1/fix/median.js | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index b22590bc6..db759d5ba 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -6,9 +6,31 @@ // or 'list' has mixed values (the function is expected to sort only numbers). function calculateMedian(list) { - const middleIndex = Math.floor(list.length / 2); - const median = list.splice(middleIndex, 1)[0]; - return median; + // Validate input: must be an array + if (!Array.isArray(list)) { + return null; + } + + // Filter out non-numeric values + const numbers = list.filter((item) => typeof item === "number"); + + // If no numeric values, return null + if (numbers.length === 0) { + return null; + } + + // Sort numbers without mutating original list + const sorted = [...numbers].sort((a, b) => a - b); + + const middleIndex = Math.floor(sorted.length / 2); + + // Odd length → return middle number + if (sorted.length % 2 !== 0) { + return sorted[middleIndex]; + } + + // Even length → average of two middle numbers + return (sorted[middleIndex - 1] + sorted[middleIndex]) / 2; } module.exports = calculateMedian; From 46499633bcc3b0d67808e9fd9efb7b8910ec8787 Mon Sep 17 00:00:00 2001 From: ChinweP Date: Mon, 27 Jul 2026 16:33:57 +0100 Subject: [PATCH 2/5] Add dedupe implementation that passes all current tests --- Sprint-1/implement/dedupe.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Sprint-1/implement/dedupe.js b/Sprint-1/implement/dedupe.js index 781e8718a..209465014 100644 --- a/Sprint-1/implement/dedupe.js +++ b/Sprint-1/implement/dedupe.js @@ -1 +1,5 @@ -function dedupe() {} +function dedupe(arr) { + return [...new Set(arr)]; +} + +module.exports = dedupe; \ No newline at end of file From bb247fd85a200cb06047f8fe029766a8c81c3e7d Mon Sep 17 00:00:00 2001 From: ChinweP Date: Mon, 27 Jul 2026 17:05:14 +0100 Subject: [PATCH 3/5] Implement findMax and add full test coverage for all input scenarios --- Sprint-1/implement/max.js | 5 +++++ Sprint-1/implement/max.test.js | 23 ++++++++++++++++++++++- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/Sprint-1/implement/max.js b/Sprint-1/implement/max.js index 6dd76378e..ceacf7c5a 100644 --- a/Sprint-1/implement/max.js +++ b/Sprint-1/implement/max.js @@ -1,4 +1,9 @@ function findMax(elements) { + const numbers = elements.filter((item) => typeof item === "number"); + if (numbers.length === 0) { + return -Infinity; + } + 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..ebfabf0fa 100644 --- a/Sprint-1/implement/max.test.js +++ b/Sprint-1/implement/max.test.js @@ -16,28 +16,49 @@ 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.todo("given an empty array, returns -Infinity"); +test("given an empty array, returns -Infinity", () => { + expect(findMax([])).toBe(-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([42])).toBe(42); +}); // 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([-10, 5, 3, -2])).toBe(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 only negative numbers, returns the closest to zero", () => { + expect(findMax([-10, -3, -20])).toBe(-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.2, 3.5, 2.8])).toBe(3.5); +}); // 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])).toBe(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', null, undefined])).toBe(-Infinity); +}); \ No newline at end of file From 816b1041eb978c9b1316554f62dbe666734a286b Mon Sep 17 00:00:00 2001 From: ChinweP Date: Mon, 27 Jul 2026 18:51:37 +0100 Subject: [PATCH 4/5] Implement sum function and add full test coverage --- Sprint-1/implement/sum.js | 5 +++++ Sprint-1/implement/sum.test.js | 20 +++++++++++++++++++- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/Sprint-1/implement/sum.js b/Sprint-1/implement/sum.js index 9062aafe3..7d4ec4186 100644 --- a/Sprint-1/implement/sum.js +++ b/Sprint-1/implement/sum.js @@ -1,4 +1,9 @@ function sum(elements) { + const numbers = elements.filter((item) => typeof item === "number"); + if (numbers.length === 0) { + return 0; + } + return numbers.reduce((total, num) => total + num, 0); } module.exports = sum; diff --git a/Sprint-1/implement/sum.test.js b/Sprint-1/implement/sum.test.js index dd0a090ca..32bab4ade 100644 --- a/Sprint-1/implement/sum.test.js +++ b/Sprint-1/implement/sum.test.js @@ -13,24 +13,42 @@ 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.todo("given an empty array, returns 0") +test("given an empty array, returns 0", () => { + expect(sum([])).toBe(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 one number, returns that number", () => { + expect(sum([42])).toBe(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 with negative numbers, returns the correct total", () => { + expect(sum([-5, 10, -3])).toBe(2); +}); // 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, 3.1])).toBe(7.1); +}); // 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 with non-number values, ignores them and returns the sum of numbers", () => { + expect(sum(["hey", 10, "hi", 60, 10])).toBe(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", null, undefined, "b"])).toBe(0); +}); From 64a2b806f781c961d1a8bf7e8dacff4390d5884d Mon Sep 17 00:00:00 2001 From: ChinweP Date: Mon, 27 Jul 2026 19:18:19 +0100 Subject: [PATCH 5/5] Refactor includes to use a for-of loop --- Sprint-1/refactor/includes.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Sprint-1/refactor/includes.js b/Sprint-1/refactor/includes.js index 29dad81f0..8c9ae2e66 100644 --- a/Sprint-1/refactor/includes.js +++ b/Sprint-1/refactor/includes.js @@ -1,8 +1,7 @@ // 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]; + for (const element of list) { if (element === target) { return true; }