From e1abbb2ff7abf41267e686a680d7d3804e75b096 Mon Sep 17 00:00:00 2001 From: Eugenie Ahangama Date: Wed, 11 Mar 2026 13:29:46 +0000 Subject: [PATCH 01/15] Fixed calculateMedian implementation --- Sprint-1/fix/median.js | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index b22590bc6..7f377a7fc 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -6,9 +6,19 @@ // 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; + // Return null for non arrays + if (!Array.isArray(list)) return null; + // Filter to only numeric values + const nums = list.filter((x) => typeof x === "number" && isFinite(x)); + // Return null if no valid numbers remain + if (nums.length === 0) return null; + + const sorted = [...nums].sort((a, b) => a - b); + const middleIndex = Math.floor(sorted.length / 2); + + return sorted.length % 2 !== 0 + ? sorted[middleIndex] + : (sorted[middleIndex - 1] + sorted[middleIndex]) / 2; } module.exports = calculateMedian; From ed6b0966698ecf00a3c14b21ee1db88ddf1b244b Mon Sep 17 00:00:00 2001 From: Eugenie Ahangama Date: Wed, 11 Mar 2026 14:00:10 +0000 Subject: [PATCH 02/15] Added dedupe implementation --- Sprint-1/implement/dedupe.js | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/Sprint-1/implement/dedupe.js b/Sprint-1/implement/dedupe.js index 781e8718a..f152f8185 100644 --- a/Sprint-1/implement/dedupe.js +++ b/Sprint-1/implement/dedupe.js @@ -1 +1,9 @@ -function dedupe() {} +function dedupe(arr) { + // Return null if the input is not an array + if (!Array.isArray(arr)) return null; + // Use Set to remove duplicates, + // then spread back into a new array + return [...new Set(arr)]; +} + +module.exports = dedupe; From afb1a3fa5472f509fd28f0acd4d874c9ff93dba3 Mon Sep 17 00:00:00 2001 From: Eugenie Ahangama Date: Wed, 11 Mar 2026 14:15:13 +0000 Subject: [PATCH 03/15] Added test cases, all tests passed --- Sprint-1/implement/dedupe.test.js | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/Sprint-1/implement/dedupe.test.js b/Sprint-1/implement/dedupe.test.js index 23e0f8638..4bbf09123 100644 --- a/Sprint-1/implement/dedupe.test.js +++ b/Sprint-1/implement/dedupe.test.js @@ -16,12 +16,23 @@ E.g. dedupe([1, 2, 1]) target output: [1, 2] // Given an empty array // When passed to the dedupe function // Then it should return an empty array -test.todo("given an empty array, it returns an empty array"); +test("given an empty array, it returns an empty array", () => { + expect(dedupe([])).toEqual([]); +}); // Given an array with no duplicates // When passed to the dedupe function // Then it should return a copy of the original array +test("given an araay with no duplicates, it returns a copy of the original array", () => { + expect(dedupe([1, 2, 3])).toEqual([1, 2, 3]); + expect(dedupe(["a", "b", "c"])).toEqual(["a", "b", "c"]); +}); // Given an array with strings or numbers // When passed to the dedupe function // Then it should remove the duplicate values, preserving the first occurence of each element +test("given an array with duplicate numbers or strings, it removes duplicates preserving first occurence", () => { + expect(dedupe([5, 1, 1, 2, 3, 2, 5, 8])).toEqual([5, 1, 2, 3, 8]); + expect(dedupe(["a", "a", "a", "b", "b", "c"])).toEqual(["a", "b", "c"]); + expect(dedupe([1, 2, 1])).toEqual([1, 2]); +}); From 4779fd16f23327c1da62296ab0b2e7885633868a Mon Sep 17 00:00:00 2001 From: Eugenie Ahangama Date: Wed, 11 Mar 2026 19:58:27 +0000 Subject: [PATCH 04/15] Add findMax implementation --- Sprint-1/implement/max.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Sprint-1/implement/max.js b/Sprint-1/implement/max.js index 6dd76378e..6e1ccee50 100644 --- a/Sprint-1/implement/max.js +++ b/Sprint-1/implement/max.js @@ -1,4 +1,8 @@ function findMax(elements) { + // Filter to only numeric values + const nums = elements.filter((x) => typeof x === "number" && isFinite(x)); + // Math.max with no arguments returns -Infinity, which handles empty arrays as well + return Math.max(...nums); } module.exports = findMax; From 71dc25ee3c10256aed110a081d6e5a3f3b4626ef Mon Sep 17 00:00:00 2001 From: Eugenie Ahangama Date: Wed, 11 Mar 2026 19:59:52 +0000 Subject: [PATCH 05/15] Add test cases for findMax function --- Sprint-1/implement/max.test.js | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/Sprint-1/implement/max.test.js b/Sprint-1/implement/max.test.js index 82f18fd88..ecc2c8753 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("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([42])).toEqual(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([30, 50, 10, 40])).toEqual(50); + expect(findMax([-10, 5, 3, -2])).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 only 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, return the largest decimal number", () => { + expect(findMax([1.5, 2.7, 0.3])).toEqual(2.7); +}); // 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-numeric values, returns the max and ignores non-numerics", () => { + 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-numeric values, returns -Infinity", () => { + expect(findMax(["hey", "hi", null, undefined])).toEqual(-Infinity); +}); From 6bf36a6ad9e22c5ca92deaa38a9d08c0a301c062 Mon Sep 17 00:00:00 2001 From: Eugenie Ahangama Date: Wed, 11 Mar 2026 21:29:53 +0000 Subject: [PATCH 06/15] Add sum implementation --- Sprint-1/implement/sum.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Sprint-1/implement/sum.js b/Sprint-1/implement/sum.js index 9062aafe3..3fa5cedf9 100644 --- a/Sprint-1/implement/sum.js +++ b/Sprint-1/implement/sum.js @@ -1,4 +1,9 @@ function sum(elements) { + // Filter to only numeric values, + // then reduce to a total + return elements + .filter((x) => typeof x === "number" && isFinite(x)) + .reduce((total, x) => total + x, 0); } module.exports = sum; From 68cee2e85dce695d69dcb9e301ff188c9fb79287 Mon Sep 17 00:00:00 2001 From: Eugenie Ahangama Date: Wed, 11 Mar 2026 21:30:24 +0000 Subject: [PATCH 07/15] Add sum test cases --- Sprint-1/implement/sum.test.js | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/Sprint-1/implement/sum.test.js b/Sprint-1/implement/sum.test.js index dd0a090ca..7e4c43232 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("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 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 with negative numbers, returns the correct sum", () => { + expect(sum([-10, -20, -30])).toEqual(-60); + expect(sum([-5, -3, -2])).toEqual(-10); +}); // 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 sum", () => { + expect(sum([1.5, 2.5, 3.0])).toEqual(7); +}); // 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-numeric values, ignores them and returns the sum", () => { + 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-numeric values, returns 0", () => { + expect(sum(["hey", "hi", null, undefined])).toEqual(0); +}); From 7866c4ba9916b32d4b3875c0ca12509edea12e3f Mon Sep 17 00:00:00 2001 From: Eugenie Ahangama Date: Wed, 11 Mar 2026 21:37:30 +0000 Subject: [PATCH 08/15] Refactor includes to use for...of loop --- Sprint-1/refactor/includes.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Sprint-1/refactor/includes.js b/Sprint-1/refactor/includes.js index 29dad81f0..2e60f61a6 100644 --- a/Sprint-1/refactor/includes.js +++ b/Sprint-1/refactor/includes.js @@ -1,12 +1,14 @@ // 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]; + // Iterate directly over each element in the list + for (const element of list) { + // Return true as soon as we find a match if (element === target) { return true; } } + // Target was not found in the list return false; } From f9f7dc04aac38f6225b70fa6c4253fe4a663b957 Mon Sep 17 00:00:00 2001 From: Eugenie Ahangama Date: Wed, 11 Mar 2026 21:38:05 +0000 Subject: [PATCH 09/15] Add comments to includes tests --- Sprint-1/refactor/includes.test.js | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/Sprint-1/refactor/includes.test.js b/Sprint-1/refactor/includes.test.js index 812158470..3d6babe53 100644 --- a/Sprint-1/refactor/includes.test.js +++ b/Sprint-1/refactor/includes.test.js @@ -2,6 +2,9 @@ const includes = require("./includes.js"); +// Given an array containing the target value +// When passed to the includes function +// Then it should return true test("returns true when target is in array", () => { const currentOutput = includes(["a", "b", "c", "d"], "c"); const targetOutput = true; @@ -9,6 +12,9 @@ test("returns true when target is in array", () => { expect(currentOutput).toEqual(targetOutput); }); +// Given an array that does not contain the target value +// When passed to the includes function +// Then it should return false test("returns false when target not in array", () => { const currentOutput = includes([1, 2, 3, 4], "a"); const targetOutput = false; @@ -16,6 +22,9 @@ test("returns false when target not in array", () => { expect(currentOutput).toEqual(targetOutput); }); +// Given an array where the target appears more than once +// When passed to the includes function +// Then it should still return true test("returns true when the target is in array multiple times", () => { const currentOutput = includes([1, 2, 2, 3], 2); const targetOutput = true; @@ -23,6 +32,9 @@ test("returns true when the target is in array multiple times", () => { expect(currentOutput).toEqual(targetOutput); }); +// Given an empty array +// When passed to the includes function +// Then it should return false test("returns false for empty array", () => { const currentOutput = includes([]); const targetOutput = false; @@ -30,6 +42,9 @@ test("returns false for empty array", () => { expect(currentOutput).toEqual(targetOutput); }); +// Given an array containing null as an element +// When passed to the includes function with null as the target +// Then it should return true test("searches for null", () => { const currentOutput = includes(["b", "z", null, "a"], null); const targetOutput = true; From 31bcc220092b0c1ab2fe095cbb293948cee27f6d Mon Sep 17 00:00:00 2001 From: Eugenie Ahangama Date: Thu, 19 Mar 2026 14:39:02 +0000 Subject: [PATCH 10/15] Fix: Remove unnecessary spread before sorting in calculateMedian --- Sprint-1/fix/median.js | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index 7f377a7fc..eb62ed801 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -12,10 +12,13 @@ function calculateMedian(list) { const nums = list.filter((x) => typeof x === "number" && isFinite(x)); // Return null if no valid numbers remain if (nums.length === 0) return null; - - const sorted = [...nums].sort((a, b) => a - b); + // Sort the numbers in ascending order + // filter already returns a new array so need to spread before spreading + const sorted = nums.sort((a, b) => a - b); + // Find the middle index of the sorted array const middleIndex = Math.floor(sorted.length / 2); - + // If odd length, return the middle index + // If even length, return the average of the two middle elements return sorted.length % 2 !== 0 ? sorted[middleIndex] : (sorted[middleIndex - 1] + sorted[middleIndex]) / 2; From f786fca3a6729152a170ce59845cea66d2af93bf Mon Sep 17 00:00:00 2001 From: Eugenie Ahangama Date: Thu, 19 Mar 2026 15:16:54 +0000 Subject: [PATCH 11/15] Fix: Add reference check to dedupe copy test --- Sprint-1/implement/dedupe.test.js | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/Sprint-1/implement/dedupe.test.js b/Sprint-1/implement/dedupe.test.js index 4bbf09123..7178b4dbe 100644 --- a/Sprint-1/implement/dedupe.test.js +++ b/Sprint-1/implement/dedupe.test.js @@ -23,9 +23,13 @@ test("given an empty array, it returns an empty array", () => { // Given an array with no duplicates // When passed to the dedupe function // Then it should return a copy of the original array -test("given an araay with no duplicates, it returns a copy of the original array", () => { - expect(dedupe([1, 2, 3])).toEqual([1, 2, 3]); - expect(dedupe(["a", "b", "c"])).toEqual(["a", "b", "c"]); +test("given an array with no duplicates, it returns a copy of the original array", () => { + const input = [1, 2, 3]; + const result = dedupe(input); + // Check the contents are the same + expect(result).toEqual(input); + // Check it's a different array, not the same reference + expect(result).not.toBe(input); }); // Given an array with strings or numbers From fb96503d1aa952bcd76cfd0393219064a6191911 Mon Sep 17 00:00:00 2001 From: Eugenie Ahangama Date: Thu, 19 Mar 2026 15:48:40 +0000 Subject: [PATCH 12/15] Fix: Add numeric string test case to findMax tests --- Sprint-1/implement/max.test.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Sprint-1/implement/max.test.js b/Sprint-1/implement/max.test.js index ecc2c8753..f5b52ee9c 100644 --- a/Sprint-1/implement/max.test.js +++ b/Sprint-1/implement/max.test.js @@ -54,6 +54,8 @@ test("given an array with decimal numbers, return the largest decimal number", ( // Then it should return the max and ignore non-numeric values test("given an array with non-numeric values, returns the max and ignores non-numerics", () => { expect(findMax(["hey", 10, "hi", 60, 10])).toEqual(60); + // "300" looks numeric but is a string, so it should be ignored + expect(findMax([10, "300", 20])).toEqual(20); }); // Given an array with only non-number values From 7dcc6d37a6bea04cd511f24b55830ee391e85f62 Mon Sep 17 00:00:00 2001 From: Eugenie Ahangama Date: Thu, 19 Mar 2026 16:05:05 +0000 Subject: [PATCH 13/15] Fix: Use toBeCloseTo for floating point sum test --- Sprint-1/implement/sum.test.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Sprint-1/implement/sum.test.js b/Sprint-1/implement/sum.test.js index 7e4c43232..436f4b3c4 100644 --- a/Sprint-1/implement/sum.test.js +++ b/Sprint-1/implement/sum.test.js @@ -37,6 +37,8 @@ test("given an array with negative numbers, returns the correct sum", () => { // Then it should return the correct total sum test("given an array with decimal numbers, returns the correct sum", () => { expect(sum([1.5, 2.5, 3.0])).toEqual(7); + // toBeCloseTo is used for floating point calculations to avoid precision issues + expect(sum([1.2, 0.6, 0.005])).toBeCloseTo(1.805); }); // Given an array containing non-number values From faa0f40cf18c52e9cd34fb95a7b96efb314d0076 Mon Sep 17 00:00:00 2001 From: Eugenie Ahangama Date: Thu, 19 Mar 2026 18:50:44 +0000 Subject: [PATCH 14/15] Fix: Strengthen dedupe copy test with duplicate input check --- Sprint-1/implement/dedupe.test.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Sprint-1/implement/dedupe.test.js b/Sprint-1/implement/dedupe.test.js index 7178b4dbe..2a32d52d5 100644 --- a/Sprint-1/implement/dedupe.test.js +++ b/Sprint-1/implement/dedupe.test.js @@ -30,6 +30,8 @@ test("given an array with no duplicates, it returns a copy of the original array expect(result).toEqual(input); // Check it's a different array, not the same reference expect(result).not.toBe(input); + // Check that a duplicate input is correctly deduped in the result + expect(dedupe([1, 1, 2, 3])).toEqual([1, 2, 3]); }); // Given an array with strings or numbers From 457ca658590b0ab314b4ea7da68764a6f5dbbdb7 Mon Sep 17 00:00:00 2001 From: Eugenie Ahangama Date: Thu, 19 Mar 2026 19:51:54 +0000 Subject: [PATCH 15/15] Fix: Compare result against hardcoded value in dedupe copy test --- Sprint-1/implement/dedupe.test.js | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/Sprint-1/implement/dedupe.test.js b/Sprint-1/implement/dedupe.test.js index 2a32d52d5..6f5ef6565 100644 --- a/Sprint-1/implement/dedupe.test.js +++ b/Sprint-1/implement/dedupe.test.js @@ -26,12 +26,10 @@ test("given an empty array, it returns an empty array", () => { test("given an array with no duplicates, it returns a copy of the original array", () => { const input = [1, 2, 3]; const result = dedupe(input); - // Check the contents are the same - expect(result).toEqual(input); + // Check the contents match a hardcoded expected value, not the input itself + expect(result).toEqual([1, 2, 3]); // Check it's a different array, not the same reference expect(result).not.toBe(input); - // Check that a duplicate input is correctly deduped in the result - expect(dedupe([1, 1, 2, 3])).toEqual([1, 2, 3]); }); // Given an array with strings or numbers