From f65f4f4ea0dfaace15904d6a1f22d1dafd1f0a55 Mon Sep 17 00:00:00 2001 From: Laura C Date: Tue, 3 Mar 2026 13:03:09 +0000 Subject: [PATCH 1/9] Fix calculateMedian --- Sprint-1/fix/median.js | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index b22590bc6..7c3cfcedf 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -6,9 +6,27 @@ // 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; + // must be an array + if (!Array.isArray(list)) return null; + + // keep only finite numbers (filters out strings, null, undefined, NaN, Infinity) + const numbers = list.filter((x) => typeof x === "number" && Number.isFinite(x)); + + // if there are no numbers (including empty array), return null + if (numbers.length === 0) return null; + + // sort without mutating the original input array + const sorted = [...numbers].sort((a, b) => a - b); + + const mid = Math.floor(sorted.length / 2); + + // odd length -> middle element + if (sorted.length % 2 === 1) { + return sorted[mid]; + } + + // even length -> average of two middle elements + return (sorted[mid - 1] + sorted[mid]) / 2; } -module.exports = calculateMedian; +module.exports = calculateMedian; \ No newline at end of file From 6a21ac3c19c0360d18d02ec884c7927fba344b90 Mon Sep 17 00:00:00 2001 From: Laura C Date: Tue, 3 Mar 2026 13:04:29 +0000 Subject: [PATCH 2/9] Update package-lock after npm install --- Sprint-1/package-lock.json | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Sprint-1/package-lock.json b/Sprint-1/package-lock.json index 83e427d0b..b52480af5 100644 --- a/Sprint-1/package-lock.json +++ b/Sprint-1/package-lock.json @@ -56,6 +56,7 @@ "integrity": "sha512-Oixnb+DzmRT30qu9d3tJSQkxuygWm32DFykT4bRoORPa9hZ/L4KhVB/XiRm6KG+roIEM7DBQlmg27kw2HZkdZg==", "dev": true, "license": "MIT", + "peer": true, "dependencies": { "@ampproject/remapping": "^2.2.0", "@babel/code-frame": "^7.25.7", @@ -1368,6 +1369,7 @@ } ], "license": "MIT", + "peer": true, "dependencies": { "caniuse-lite": "^1.0.30001663", "electron-to-chromium": "^1.5.28", From 711457b1540e87997401e7fdfdee354f36d3e452 Mon Sep 17 00:00:00 2001 From: Laura C Date: Tue, 3 Mar 2026 13:21:19 +0000 Subject: [PATCH 3/9] Add tests for dedupe --- Sprint-1/implement/dedupe.test.js | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/Sprint-1/implement/dedupe.test.js b/Sprint-1/implement/dedupe.test.js index 23e0f8638..2eb96cc0f 100644 --- a/Sprint-1/implement/dedupe.test.js +++ b/Sprint-1/implement/dedupe.test.js @@ -16,12 +16,33 @@ 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 array with no duplicates, it returns a copy of the original array", () => { + const input = ["apple", "banana", 1, 10]; + const result = dedupe(input); + + expect(result).toEqual(input); + expect(result).not.toBe(input); // ensures it's a copy +}); + // 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("removes duplicate values and preserves first occurrence", () => { + expect(dedupe(["a", "a", "a", "b", "b", "c"])).toEqual(["a", "b", "c"]); + expect(dedupe([5, 1, 1, 2, 3, 2, 5, 8])).toEqual([5, 1, 2, 3, 8]); + expect(dedupe([1, 2, 1])).toEqual([1, 2]); +}); + + + + From d7ea26f9702ee4ec435d76da2e6882cbff11fc73 Mon Sep 17 00:00:00 2001 From: Laura C Date: Tue, 3 Mar 2026 13:23:37 +0000 Subject: [PATCH 4/9] Implement dedupe function --- Sprint-1/implement/dedupe.js | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/Sprint-1/implement/dedupe.js b/Sprint-1/implement/dedupe.js index 781e8718a..370d93dc9 100644 --- a/Sprint-1/implement/dedupe.js +++ b/Sprint-1/implement/dedupe.js @@ -1 +1,15 @@ -function dedupe() {} +function dedupe(input) { + if (!Array.isArray(input)) return null; + + const result = []; + + for (const item of input) { + if (!result.includes(item)) { + result.push(item); + } + } + + return result; +} + +module.exports = dedupe; From 690b6a47ed0d61ee309a7fa985deb0cba13be5fc Mon Sep 17 00:00:00 2001 From: Laura C Date: Tue, 3 Mar 2026 13:28:49 +0000 Subject: [PATCH 5/9] Add tests for findMax --- Sprint-1/implement/max.test.js | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/Sprint-1/implement/max.test.js b/Sprint-1/implement/max.test.js index 82f18fd88..20cb11ca4 100644 --- a/Sprint-1/implement/max.test.js +++ b/Sprint-1/implement/max.test.js @@ -15,29 +15,48 @@ 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([5])).toBe(5); +}); // 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 positive and negative numbers, returns the largest overall", () => { + expect(findMax([1, -10, 50, 3])).toBe(50); +}); // Given an array with just negative numbers // When passed to the max function // Then it should return the closest one to zero +test("given 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 decimal numbers, returns the largest decimal", () => { + expect(findMax([1.5, 10.5, 0.5])).toBe(10.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("ignores non-number values", () => { + 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 only non-number values, returns -Infinity", () => { + expect(findMax(["apple", "banana"])).toBe(-Infinity); +}); \ No newline at end of file From 6c2f859289e19d0171cab663fddc3c5d463a84c4 Mon Sep 17 00:00:00 2001 From: Laura C Date: Tue, 3 Mar 2026 13:30:05 +0000 Subject: [PATCH 6/9] Implement findMax --- Sprint-1/implement/max.js | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/Sprint-1/implement/max.js b/Sprint-1/implement/max.js index 6dd76378e..d6639b4d9 100644 --- a/Sprint-1/implement/max.js +++ b/Sprint-1/implement/max.js @@ -1,4 +1,13 @@ function findMax(elements) { + if (!Array.isArray(elements)) return null; + + const numbers = elements.filter( + (x) => typeof x === "number" && Number.isFinite(x) + ); + + if (numbers.length === 0) return -Infinity; + + return Math.max(...numbers); } -module.exports = findMax; +module.exports = findMax; \ No newline at end of file From f58233e60c8b37cc19acfd855b4e6b64cb296196 Mon Sep 17 00:00:00 2001 From: Laura C Date: Tue, 3 Mar 2026 13:35:35 +0000 Subject: [PATCH 7/9] Add tests for sum --- Sprint-1/implement/sum.test.js | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/Sprint-1/implement/sum.test.js b/Sprint-1/implement/sum.test.js index dd0a090ca..7b1880c43 100644 --- a/Sprint-1/implement/sum.test.js +++ b/Sprint-1/implement/sum.test.js @@ -10,27 +10,45 @@ const sum = require("./sum.js"); // Acceptance Criteria: + // 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([5])).toBe(5); +}); // Given an array containing negative numbers // When passed to the sum function // Then it should still return the correct total sum +test("given negative numbers, returns correct sum", () => { + expect(sum([1, -1, -100])).toBe(-100); +}); // Given an array with decimal/float numbers // When passed to the sum function // Then it should return the correct total sum +test("given decimal numbers, returns correct sum", () => { + expect(sum([1.5, 10.5, 0.5])).toBeCloseTo(12.5); +}); // 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("ignores non-number values", () => { + expect(sum(["apple", 10, "banana", 5])).toBe(15); +}); // 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 +// Then it should return 0 +test("given only non-number values, returns 0", () => { + expect(sum(["apple", "banana"])).toBe(0); +}); \ No newline at end of file From 282b0a76c7c32231989694bd857adc1e8cf4c6bb Mon Sep 17 00:00:00 2001 From: Laura C Date: Tue, 3 Mar 2026 13:37:43 +0000 Subject: [PATCH 8/9] Implement sum function --- Sprint-1/implement/sum.js | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/Sprint-1/implement/sum.js b/Sprint-1/implement/sum.js index 9062aafe3..1792ef977 100644 --- a/Sprint-1/implement/sum.js +++ b/Sprint-1/implement/sum.js @@ -1,4 +1,19 @@ function sum(elements) { + if (!Array.isArray(elements)) return null; + + const numbers = elements.filter( + (x) => typeof x === "number" && Number.isFinite(x) + ); + + if (numbers.length === 0) return 0; + + let total = 0; + + for (const number of numbers) { + total += number; + } + + return total; } -module.exports = sum; +module.exports = sum; \ No newline at end of file From 3d4ab2d1e7071e75b33f1eb6ffbb09a5bafef198 Mon Sep 17 00:00:00 2001 From: Laura C Date: Tue, 3 Mar 2026 13:41:07 +0000 Subject: [PATCH 9/9] 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..4a23cb6d2 100644 --- a/Sprint-1/refactor/includes.js +++ b/Sprint-1/refactor/includes.js @@ -1,8 +1,5 @@ -// 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; } @@ -10,4 +7,4 @@ function includes(list, target) { return false; } -module.exports = includes; +module.exports = includes; \ No newline at end of file