From f202df5f37f2a5b8d8b223de8b1cc73918567f64 Mon Sep 17 00:00:00 2001 From: joanne342 <130585013+joanne342@users.noreply.github.com> Date: Sat, 4 Jul 2026 13:57:37 +0100 Subject: [PATCH 01/18] Update median.js --- Sprint-1/fix/median.js | 36 ++++++++++++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 4 deletions(-) diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index b22590bc6..69d5d1bc8 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -5,10 +5,38 @@ // Hint: Please consider scenarios when 'list' doesn't have numbers (the function is expected to return null) // 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; +function calculateMedian(arr) { + try { + if (!Array.isArray(arr)) return null; + + const nums = []; + + for (const x of arr) { + // skip null/undefined + if (x === null || x === undefined) continue; + + if (typeof x === "string" && x.trim() === "") continue; + + const n = Number(x); + + if (Number.isFinite(n)) { + nums.push(n); + } + } + + if (nums.length === 0) return null; + + const sorted = [...nums].sort((a, b) => a - b); + const mid = Math.floor(sorted.length / 2); + + if (sorted.length % 2 === 1) { + return sorted[mid]; + } else { + return (sorted[mid - 1] + sorted[mid]) / 2; + } + } catch (e) { + return null; + } } module.exports = calculateMedian; From 92d73301266aba42602a772a1b00335cc9e66ddc Mon Sep 17 00:00:00 2001 From: joanne342 <130585013+joanne342@users.noreply.github.com> Date: Sat, 4 Jul 2026 14:33:48 +0100 Subject: [PATCH 02/18] Update median.js --- Sprint-1/fix/median.js | 37 +++++++++++++++++-------------------- 1 file changed, 17 insertions(+), 20 deletions(-) diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index 69d5d1bc8..d3e0b406d 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -1,10 +1,3 @@ -// Fix this implementation -// Start by running the tests for this function -// If you're in the Sprint-1 directory, you can run `npm test -- fix` to run the tests in the fix directory - -// Hint: Please consider scenarios when 'list' doesn't have numbers (the function is expected to return null) -// or 'list' has mixed values (the function is expected to sort only numbers). - function calculateMedian(arr) { try { if (!Array.isArray(arr)) return null; @@ -12,29 +5,33 @@ function calculateMedian(arr) { const nums = []; for (const x of arr) { - // skip null/undefined - if (x === null || x === undefined) continue; - - if (typeof x === "string" && x.trim() === "") continue; + if (typeof x === "number" && Number.isFinite(x)) { + nums.push(x); + continue; + } - const n = Number(x); + if (typeof x === "string") { + const trimmed = x.trim(); + if (trimmed === "") continue; - if (Number.isFinite(n)) { - nums.push(n); + const n = Number(trimmed); + if (Number.isFinite(n)) { + nums.push(n); + } } } if (nums.length === 0) return null; const sorted = [...nums].sort((a, b) => a - b); + const mid = Math.floor(sorted.length / 2); - if (sorted.length % 2 === 1) { - return sorted[mid]; - } else { - return (sorted[mid - 1] + sorted[mid]) / 2; - } - } catch (e) { + return sorted.length % 2 === 1 + ? sorted[mid] + : (sorted[mid - 1] + sorted[mid]) / 2; + + } catch { return null; } } From a5229bb2c7fd602c383630f2e8e88167f2d3f66c Mon Sep 17 00:00:00 2001 From: joanne342 <130585013+joanne342@users.noreply.github.com> Date: Sat, 4 Jul 2026 14:38:00 +0100 Subject: [PATCH 03/18] Update median.js --- Sprint-1/fix/median.js | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index d3e0b406d..fcfc7f25f 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -11,20 +11,14 @@ function calculateMedian(arr) { } if (typeof x === "string") { - const trimmed = x.trim(); - if (trimmed === "") continue; - - const n = Number(trimmed); - if (Number.isFinite(n)) { - nums.push(n); - } + const n = Number(x.trim()); + if (Number.isFinite(n)) nums.push(n); } } if (nums.length === 0) return null; - const sorted = [...nums].sort((a, b) => a - b); - + const sorted = nums.slice().sort((a, b) => a - b); const mid = Math.floor(sorted.length / 2); return sorted.length % 2 === 1 From 914cc03826f30fe881c5ccb5398117ed086224cc Mon Sep 17 00:00:00 2001 From: joanne342 <130585013+joanne342@users.noreply.github.com> Date: Sat, 4 Jul 2026 14:42:34 +0100 Subject: [PATCH 04/18] Update median.js --- Sprint-1/fix/median.js | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index fcfc7f25f..9648ebfa8 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -4,14 +4,15 @@ function calculateMedian(arr) { const nums = []; - for (const x of arr) { + for (let i = 0; i < arr.length; i++) { + const x = arr[i]; + + if (!(i in arr)) continue; + if (typeof x === "number" && Number.isFinite(x)) { nums.push(x); - continue; - } - - if (typeof x === "string") { - const n = Number(x.trim()); + } else if (typeof x === "string") { + const n = Number(x); if (Number.isFinite(n)) nums.push(n); } } From 49dbd75697a18950d161b63d26a246d31a45e53d Mon Sep 17 00:00:00 2001 From: joanne342 <130585013+joanne342@users.noreply.github.com> Date: Sat, 4 Jul 2026 15:28:34 +0100 Subject: [PATCH 05/18] Update median.js --- Sprint-1/fix/median.js | 37 ++++++++++++------------------------- 1 file changed, 12 insertions(+), 25 deletions(-) diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index 9648ebfa8..8361a9d3e 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -1,34 +1,21 @@ -function calculateMedian(arr) { - try { - if (!Array.isArray(arr)) return null; +function calculate_median(arr) { + + if (!Array.isArray(arr)) return null; - const nums = []; + if (arr.length === 0) return null; - for (let i = 0; i < arr.length; i++) { - const x = arr[i]; + const cleaned = arr.filter(x => typeof x === "number" && Number.isFinite(x)); - if (!(i in arr)) continue; + if (cleaned.length === 0) return null; - if (typeof x === "number" && Number.isFinite(x)) { - nums.push(x); - } else if (typeof x === "string") { - const n = Number(x); - if (Number.isFinite(n)) nums.push(n); - } - } + const sorted = [...cleaned].sort((a, b) => a - b); - if (nums.length === 0) return null; + const mid = Math.floor(sorted.length / 2); - const sorted = nums.slice().sort((a, b) => a - b); - const mid = Math.floor(sorted.length / 2); - - return sorted.length % 2 === 1 - ? sorted[mid] - : (sorted[mid - 1] + sorted[mid]) / 2; - - } catch { - return null; + if (sorted.length % 2 === 1) { + return sorted[mid]; + } else { + return (sorted[mid - 1] + sorted[mid]) / 2; } } - module.exports = calculateMedian; From d2d77534f4ccaa6250a17e5adfa17ce5190b4f32 Mon Sep 17 00:00:00 2001 From: joanne342 <130585013+joanne342@users.noreply.github.com> Date: Sat, 4 Jul 2026 15:55:34 +0100 Subject: [PATCH 06/18] Update median.js --- Sprint-1/fix/median.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index 8361a9d3e..1b6edac4e 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -1,4 +1,4 @@ -function calculate_median(arr) { +function calculateMedian(arr) { if (!Array.isArray(arr)) return null; From 0d75782b8e1ba4c91c8e3cf5219b2d22bfdc674b Mon Sep 17 00:00:00 2001 From: joanne342 <130585013+joanne342@users.noreply.github.com> Date: Sat, 4 Jul 2026 16:04:50 +0100 Subject: [PATCH 07/18] Update dedupe.test.js --- Sprint-1/implement/dedupe.test.js | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/Sprint-1/implement/dedupe.test.js b/Sprint-1/implement/dedupe.test.js index d7c8e3d8e..b14bb8115 100644 --- a/Sprint-1/implement/dedupe.test.js +++ b/Sprint-1/implement/dedupe.test.js @@ -26,3 +26,18 @@ test.todo("given an empty array, it returns an empty array"); // When passed to the dedupe function // Then it should return a new array with duplicates removed while preserving the // first occurrence of each element from the original array. + +test("given an empty array, it returns an empty array", () => { + expect(dedupe([])).toEqual([]); +}); + +test("given an array with no duplicates, it returns a copy", () => { + const input = [1, 2, 3]; + expect(dedupe(input)).toEqual([1, 2, 3]); + expect(dedupe(input)).not.toBe(input); // ensures it's a new array +}); + +test("removes duplicates while preserving order", () => { + expect(dedupe([5, 1, 1, 2, 3, 2, 5, 8])).toEqual([5, 1, 2, 3, 8]); + expect(dedupe(["a", "a", "b", "c", "b"])).toEqual(["a", "b", "c"]); +}); From e45e7e1a946f3dd9d032dcd778749a268bde30cb Mon Sep 17 00:00:00 2001 From: joanne342 <130585013+joanne342@users.noreply.github.com> Date: Sat, 4 Jul 2026 16:22:41 +0100 Subject: [PATCH 08/18] Update dedupe.js --- 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..116d814a9 100644 --- a/Sprint-1/implement/dedupe.js +++ b/Sprint-1/implement/dedupe.js @@ -1 +1,9 @@ -function dedupe() {} +function dedupe(arr) { + const array = []; + for (const x of arr) { + if (!array.includes(x)) { + array.push(x); + } + } + return array; +} From 373358225d70266993c8f94037584db8a635c136 Mon Sep 17 00:00:00 2001 From: joanne342 <130585013+joanne342@users.noreply.github.com> Date: Wed, 8 Jul 2026 14:26:29 +0100 Subject: [PATCH 09/18] Update max.test.js --- Sprint-1/implement/max.test.js | 35 ++++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/Sprint-1/implement/max.test.js b/Sprint-1/implement/max.test.js index 82f18fd88..e7dfc5f32 100644 --- a/Sprint-1/implement/max.test.js +++ b/Sprint-1/implement/max.test.js @@ -1,43 +1,50 @@ -/* Find the maximum element of an array of numbers - -In this kata, you will need to implement a function that find the largest numerical element of an array. - -E.g. max([30, 50, 10, 40]), target output: 50 -E.g. max(['hey', 10, 'hi', 60, 10]), target output: 60 (sum ignores any non-numerical elements) - -You should implement this function in max.js, and add tests for it in this file. - -We have set things up already so that this file can see your function from the other file. -*/ - const findMax = require("./max.js"); // Given an empty array // 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([])).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 number", () => { + expect(findMax([-10, 5, -3, 20, 8])).toBe(20); +}); // 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([-8, -3, -15, -1])).toBe(-1); +}); // 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.5, 3.14, 2.72, 3.13])).toBe(3.14); +}); // 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 maximum number", () => { + 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(["hello", "world", true, null, {}])).toBe(-Infinity); +}); From 14896e8eb23d0498372f6e87bae8d7c0e1e2d32b Mon Sep 17 00:00:00 2001 From: joanne342 <130585013+joanne342@users.noreply.github.com> Date: Wed, 8 Jul 2026 14:42:21 +0100 Subject: [PATCH 10/18] Update max.js --- Sprint-1/implement/max.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/Sprint-1/implement/max.js b/Sprint-1/implement/max.js index 6dd76378e..e932dc598 100644 --- a/Sprint-1/implement/max.js +++ b/Sprint-1/implement/max.js @@ -1,4 +1,18 @@ function findMax(elements) { + const floats = elements + .filter(x => { + const str = String(x).replace("-", "").replace(".", ""); + return !isNaN(str) && str !== ""; + }) + .map(x => Number(x)); + + if (floats.length === 0) { + return -Infinity; + } else { + return Math.max(...floats); + } +} + } module.exports = findMax; From 8a28579a74331ec8d2f5109b2779a11b752226ed Mon Sep 17 00:00:00 2001 From: joanne342 <130585013+joanne342@users.noreply.github.com> Date: Wed, 8 Jul 2026 14:55:42 +0100 Subject: [PATCH 11/18] Update max.js --- Sprint-1/implement/max.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sprint-1/implement/max.js b/Sprint-1/implement/max.js index e932dc598..d9d84fea3 100644 --- a/Sprint-1/implement/max.js +++ b/Sprint-1/implement/max.js @@ -13,6 +13,6 @@ function findMax(elements) { } } -} + module.exports = findMax; From 93c86d2306298c087ce73c21f22c1e5f07b2fad2 Mon Sep 17 00:00:00 2001 From: joanne342 <130585013+joanne342@users.noreply.github.com> Date: Wed, 8 Jul 2026 15:06:56 +0100 Subject: [PATCH 12/18] Update sum.test.js --- Sprint-1/implement/sum.test.js | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/Sprint-1/implement/sum.test.js b/Sprint-1/implement/sum.test.js index dd0a090ca..2ecb1788f 100644 --- a/Sprint-1/implement/sum.test.js +++ b/Sprint-1/implement/sum.test.js @@ -13,24 +13,41 @@ 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([])).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([10])).toBe(10); +}); // 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, -20, 30])).toBe(0); +}); // 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([10.5, 20.25, 5.25])).toBe(36); +}); // 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 numbers only", () => { + 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(["hello", "world", true, null])).toBe(0); +}); From ab33f70fc89522f4b279295eaeb2ce080aa5d13a Mon Sep 17 00:00:00 2001 From: joanne342 <130585013+joanne342@users.noreply.github.com> Date: Wed, 8 Jul 2026 15:23:22 +0100 Subject: [PATCH 13/18] Update sum.js --- 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..ea225927d 100644 --- a/Sprint-1/implement/sum.js +++ b/Sprint-1/implement/sum.js @@ -1,4 +1,9 @@ function sum(elements) { + + return elements + .filter(x => String(x).replace("-", "").replace(".", "").match(/^\d+$/)) + .reduce((sum, x) => sum + x, 0); + } module.exports = sum; From ac761f89f0baf52e5a60b928077f460bf5dfea6f Mon Sep 17 00:00:00 2001 From: joanne342 <130585013+joanne342@users.noreply.github.com> Date: Wed, 8 Jul 2026 15:42:23 +0100 Subject: [PATCH 14/18] Update includes.js --- Sprint-1/refactor/includes.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Sprint-1/refactor/includes.js b/Sprint-1/refactor/includes.js index 29dad81f0..f727d5cc3 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 item of items) { if (element === target) { return true; } @@ -11,3 +10,4 @@ function includes(list, target) { } module.exports = includes; + From b3911a2c3b3d03b8eeba25cd72a5361c3df2b9fc Mon Sep 17 00:00:00 2001 From: joanne342 <130585013+joanne342@users.noreply.github.com> Date: Thu, 9 Jul 2026 13:36:07 +0100 Subject: [PATCH 15/18] Update dedupe.js --- Sprint-1/implement/dedupe.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Sprint-1/implement/dedupe.js b/Sprint-1/implement/dedupe.js index 116d814a9..91c65ac28 100644 --- a/Sprint-1/implement/dedupe.js +++ b/Sprint-1/implement/dedupe.js @@ -7,3 +7,5 @@ function dedupe(arr) { } return array; } + +module.exports = dedupe; From 409eefc292f6a24c8d0a5c13efd168ff4b244d90 Mon Sep 17 00:00:00 2001 From: joanne342 <130585013+joanne342@users.noreply.github.com> Date: Thu, 9 Jul 2026 13:43:16 +0100 Subject: [PATCH 16/18] Update dedupe.test.js --- Sprint-1/implement/dedupe.test.js | 1 - 1 file changed, 1 deletion(-) diff --git a/Sprint-1/implement/dedupe.test.js b/Sprint-1/implement/dedupe.test.js index b14bb8115..cb4dc4c7b 100644 --- a/Sprint-1/implement/dedupe.test.js +++ b/Sprint-1/implement/dedupe.test.js @@ -16,7 +16,6 @@ E.g. dedupe([1, 2, 1]) returns [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"); // Given an array with no duplicates // When passed to the dedupe function From 367204af2f7b2f8fbaa4c074d51cd4b489e20ddc Mon Sep 17 00:00:00 2001 From: joanne342 <130585013+joanne342@users.noreply.github.com> Date: Wed, 15 Jul 2026 09:34:33 +0100 Subject: [PATCH 17/18] Update max.js --- Sprint-1/implement/max.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sprint-1/implement/max.js b/Sprint-1/implement/max.js index d9d84fea3..277a90697 100644 --- a/Sprint-1/implement/max.js +++ b/Sprint-1/implement/max.js @@ -1,7 +1,7 @@ function findMax(elements) { const floats = elements .filter(x => { - const str = String(x).replace("-", "").replace(".", ""); + const str = String(x); return !isNaN(str) && str !== ""; }) .map(x => Number(x)); From 8100bd59bd5213fa52866f44a9e316523e5da9bf Mon Sep 17 00:00:00 2001 From: joanne342 <130585013+joanne342@users.noreply.github.com> Date: Wed, 15 Jul 2026 10:03:06 +0100 Subject: [PATCH 18/18] Update median.js --- Sprint-1/fix/median.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index 1b6edac4e..c39a5797b 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -8,7 +8,7 @@ function calculateMedian(arr) { if (cleaned.length === 0) return null; - const sorted = [...cleaned].sort((a, b) => a - b); + const sorted = cleaned.sort((a, b) => a - b); const mid = Math.floor(sorted.length / 2);