From d3de7c2d67f4021d4e5bc7167508208bece108b5 Mon Sep 17 00:00:00 2001 From: Sakiya Mayow Date: Sat, 7 Nov 2026 22:53:15 +0000 Subject: [PATCH 01/12] "fix: correct calculateMedian implementation" --- Sprint-1/fix/median.js | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index 5c5b796e1..8e4dca012 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -6,9 +6,27 @@ // or contains values that aren't numbers (the function is expected to throw - see the tests). function calculateMedian(list) { - const middleIndex = Math.floor(list.length / 2); - const median = list.splice(middleIndex, 1)[0]; - return median; + if (!Array.isArray(list)) { + throw new Error("calculateMedian requires an array of numbers"); + } + + if (list.length === 0) { + throw new Error("calculateMedian requires a non-empty array"); + } + + if (!list.every((number) => typeof number === "number")) { + throw new Error("calculateMedian requires an array of numbers"); + } + + const sortedList = [...list].sort((a, b) => a - b); + + const middleIndex = Math.floor(sortedList.length / 2); + + if (sortedList.length % 2 !== 0) { + return sortedList[middleIndex]; + } + + return (sortedList[middleIndex - 1] + sortedList[middleIndex]) / 2; } module.exports = calculateMedian; From 15d231b0b730537126f4b317a855d31963cb6cd0 Mon Sep 17 00:00:00 2001 From: Sakiya Mayow Date: Sat, 7 Nov 2026 23:39:00 +0000 Subject: [PATCH 02/12] "test: add tests for dedupe function" --- Sprint-1/implement/dedupe.test.js | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/Sprint-1/implement/dedupe.test.js b/Sprint-1/implement/dedupe.test.js index d7c8e3d8e..a184993b6 100644 --- a/Sprint-1/implement/dedupe.test.js +++ b/Sprint-1/implement/dedupe.test.js @@ -16,13 +16,33 @@ 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"); + +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", () => { + const numbers = [1, 2, 3]; + const result = dedupe(numbers); + + expect(result).toEqual([1, 2, 3]); + expect(result).not.toBe(numbers); +}); + // Given an array of strings or numbers // When passed to the dedupe function -// Then it should return a new array with duplicates removed while preserving the +// 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 array with duplicates, it removes duplicates and preserves the first occurrence", () => { + const numbers = [5, 1, 1, 2, 3, 2, 5, 8]; + + const result = dedupe(numbers); + + expect(result).toEqual([5, 1, 2, 3, 8]); + expect(result).not.toBe(numbers); +}); From 11887fc7506be414834f2fb51cd0294d4a212a8c Mon Sep 17 00:00:00 2001 From: Sakiya Mayow Date: Sat, 7 Nov 2026 23:41:01 +0000 Subject: [PATCH 03/12] "feat: implement dedupe function" --- Sprint-1/implement/dedupe.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/Sprint-1/implement/dedupe.js b/Sprint-1/implement/dedupe.js index 781e8718a..a55e859ca 100644 --- a/Sprint-1/implement/dedupe.js +++ b/Sprint-1/implement/dedupe.js @@ -1 +1,7 @@ -function dedupe() {} +function dedupe(array) { + return array.filter((item, index) => { + return array.indexOf(item) === index; + }); +} + +module.exports = dedupe; From c107da3c82a5222b94d245ee4c5337dee6a7a835 Mon Sep 17 00:00:00 2001 From: Sakiya Mayow Date: Sun, 8 Nov 2026 01:08:15 +0000 Subject: [PATCH 04/12] "test: add describeMedian tests" --- Sprint-1/implement/describe-median.test.js | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/Sprint-1/implement/describe-median.test.js b/Sprint-1/implement/describe-median.test.js index e7d18a264..acbf51f08 100644 --- a/Sprint-1/implement/describe-median.test.js +++ b/Sprint-1/implement/describe-median.test.js @@ -21,12 +21,27 @@ const describeMedian = require("./describe-median.js"); // When passed to describeMedian // Then it should return "The median is " followed by the median // Delete this test.todo and replace it with a test. -test.todo('given [1, 2, 3], returns "The median is 2"'); + +test('given [1, 2, 3], returns "The median is 2"', () => { + expect(describeMedian([1, 2, 3])).toBe("The median is 2"); +}); // Given an empty array // When passed to describeMedian // Then it should return "Could not calculate a median: calculateMedian requires a non-empty array" +test("given an empty array, returns an error message", () => { + expect(describeMedian([])).toBe( + "Could not calculate a median: calculateMedian requires a non-empty array" + ); +}); + // Given something that isn't an array of numbers, e.g. "banana" // When passed to describeMedian // Then it should return "Could not calculate a median: calculateMedian requires an array of numbers" + +test("given something that isn't an array of numbers, returns an error message", () => { + expect(describeMedian("apple")).toBe( + "Could not calculate a median: calculateMedian requires an array of numbers" + ); +}); From eae219df28ea331cc27cf9fdca3dbaf1a2b54427 Mon Sep 17 00:00:00 2001 From: Sakiya Mayow Date: Sun, 8 Nov 2026 01:08:52 +0000 Subject: [PATCH 05/12] "feat: implement describeMedian function" --- Sprint-1/implement/describe-median.js | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/Sprint-1/implement/describe-median.js b/Sprint-1/implement/describe-median.js index 3381cfc0a..f6fdd5660 100644 --- a/Sprint-1/implement/describe-median.js +++ b/Sprint-1/implement/describe-median.js @@ -20,6 +20,13 @@ function calculateMedian(list) { } // Implement this function. See describe-median.test.js for the acceptance criteria. -function describeMedian(list) {} +function describeMedian(list) { + try { + const median = calculateMedian(list); + return `The median is ${median}`; + } catch (error) { + return `Could not calculate a median: ${error.message}`; + } +} module.exports = describeMedian; From 0f5f6829f988424bc216bc45d1c327f60cbc9685 Mon Sep 17 00:00:00 2001 From: Sakiya Mayow Date: Sun, 8 Nov 2026 03:11:18 +0000 Subject: [PATCH 06/12] "test: add findMax tests" --- Sprint-1/implement/max.test.js | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/Sprint-1/implement/max.test.js b/Sprint-1/implement/max.test.js index ff3fbb5e5..fc0a4b0d5 100644 --- a/Sprint-1/implement/max.test.js +++ b/Sprint-1/implement/max.test.js @@ -16,28 +16,59 @@ 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([])).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 number", () => { + expect(findMax([-5, 10, 3, -2])).toBe(10); +}); + // 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 zero", () => { + expect(findMax([-10, -5, -2, -8])).toBe(-2); +}); + // 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 number", () => { + expect(findMax([2.5, 4.7, 1.2, 3.9])).toBe(4.7); +}); + // Given an array containing a value that isn't a number // When passed to the max function // Then it should throw Error("findMax requires an array of numbers") +test("given a non-number array, throws an error", () => { + expect(() => findMax(["hello", 10, "apple", 15])).toThrow( + "findMax requires an array of numbers" + ); +}); + // Given something that isn't an array at all, such as "hey", 42 or no argument // When passed to the max function // Then it should throw Error("findMax requires an array of numbers") + +test("given something that isn't an array, throws an error", () => { + expect(() => findMax("hey")).toThrow("findMax requires an array of numbers"); + expect(() => findMax("42")).toThrow("findMax requires an array of numbers"); + expect(() => findMax()).toThrow("findMax requires an array of numbers"); +}); From 92438ea62ca4f768f681a129ab06bc0b0f855037 Mon Sep 17 00:00:00 2001 From: Sakiya Mayow Date: Sun, 8 Nov 2026 03:11:38 +0000 Subject: [PATCH 07/12] "feat: implement findMax function" --- Sprint-1/implement/max.js | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/Sprint-1/implement/max.js b/Sprint-1/implement/max.js index 6dd76378e..013aee4e6 100644 --- a/Sprint-1/implement/max.js +++ b/Sprint-1/implement/max.js @@ -1,4 +1,14 @@ function findMax(elements) { + if (!Array.isArray(elements)) { + throw new Error("findMax requires an array of numbers"); + } + for (const element of elements) { + if (typeof element !== "number") { + throw new Error("findMax requires an array of numbers"); + } + } + + return Math.max(...elements); } module.exports = findMax; From 613fa408b4b8592cef504b19ade8556eb14674d0 Mon Sep 17 00:00:00 2001 From: Sakiya Mayow Date: Sun, 8 Nov 2026 03:40:25 +0000 Subject: [PATCH 08/12] "test: add calculateMean tests" --- Sprint-1/implement/mean.test.js | 46 ++++++++++++++++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) diff --git a/Sprint-1/implement/mean.test.js b/Sprint-1/implement/mean.test.js index 847b88676..5dd7c1187 100644 --- a/Sprint-1/implement/mean.test.js +++ b/Sprint-1/implement/mean.test.js @@ -23,28 +23,72 @@ const calculateMean = require("./mean.js"); // When passed to calculateMean // Then it should return their mean // Delete this test.todo and replace it with a test. -test.todo("given [1, 2, 6], returns 3"); + +test("given [1, 2, 6], returns 3", () => { + expect(calculateMean([1, 2, 6])).toBe(3); +}); // Given an array with a single number // When passed to calculateMean // Then it should return that number +test("given an array with one number, returns that number", () => { + expect(calculateMean([6])).toBe(6); +}); + // Given an array containing negative or decimal numbers // When passed to calculateMean // Then it should return the correct mean +test("given negative and decimal numbers, returns the correct mean", () => { + expect(calculateMean([-2, 3.5, 4.5])).toBe(2); +}); + // Given an empty array // When passed to calculateMean // Then it should throw Error("calculateMean requires a non-empty array") +test("given an empty array, throws an error", () => { + expect(() => calculateMean([])).toThrow( + "calculateMean requires a non-empty array" + ); +}); + // Given a value that isn't an array, e.g. "banana", 42, null or {} // When passed to calculateMean // Then it should throw Error("calculateMean requires an array of numbers") +test("given something that isn't an array, throws an error", () => { + expect(() => calculateMean("banana")).toThrow( + "calculateMean requires an array of numbers" + ); + expect(() => calculateMean(42)).toThrow( + "calculateMean requires an array of numbers" + ); + expect(() => calculateMean(null)).toThrow( + "calculateMean requires an array of numbers" + ); + expect(() => calculateMean({})).toThrow( + "calculateMean requires an array of numbers" + ); +}); + // Given no argument at all // When passed to calculateMean // Then it should throw Error("calculateMean requires an array of numbers") +test("given no argument, throws an error", () => { + expect(() => calculateMean()).toThrow( + "calculateMean requires an array of numbers" + ); +}); + // Given an array containing a non-number value, e.g. [1, "2", 3] // When passed to calculateMean // Then it should throw Error("calculateMean requires an array of numbers") + +test("given an array containing a non-number, throws an error", () => { + expect(() => calculateMean([1, "2", 3])).toThrow( + "calculateMean requires an array of numbers" + ); +}); From 0ad4f5e5d95cc00d0f12e13305af158b2c73f208 Mon Sep 17 00:00:00 2001 From: Sakiya Mayow Date: Sun, 8 Nov 2026 03:40:52 +0000 Subject: [PATCH 09/12] "feat: implement calculateMean function" --- Sprint-1/implement/mean.js | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/Sprint-1/implement/mean.js b/Sprint-1/implement/mean.js index 36909ea1f..be6cd78ee 100644 --- a/Sprint-1/implement/mean.js +++ b/Sprint-1/implement/mean.js @@ -1,3 +1,21 @@ -function calculateMean(list) {} +function calculateMean(list) { + if (!Array.isArray(list)) { + throw new Error("calculateMean requires an array of numbers"); + } + if (list.length === 0) { + throw new Error("calculateMean requires a non-empty array"); + } + for (const number of list) { + if (typeof number !== "number") { + throw new Error("calculateMean requires an array of numbers"); + } + } + let total = 0; + for (const number of list) { + total += number; + } + + return total / list.length; +} module.exports = calculateMean; From 1fb0ce9ce79e12ae13c59bd5a4632483d733e7e9 Mon Sep 17 00:00:00 2001 From: Sakiya Mayow Date: Sun, 8 Nov 2026 04:08:06 +0000 Subject: [PATCH 10/12] "test: add sum tests" --- Sprint-1/implement/sum.test.js | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/Sprint-1/implement/sum.test.js b/Sprint-1/implement/sum.test.js index a288e0f0a..719d2b94a 100644 --- a/Sprint-1/implement/sum.test.js +++ b/Sprint-1/implement/sum.test.js @@ -13,24 +13,49 @@ 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([3])).toBe(3); +}); + // 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([-2, -3, 5])).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 sum", () => { + expect(sum([1.5, 2.5, 3])).toBe(7); +}); + // Given an array containing a value that isn't a number // When passed to the sum function // Then it should throw Error("sum requires an array of numbers") +test("given an array containing a non-number, throws an error", () => { + expect(() => sum([1, "2", 3])).toThrow("sum requires an array of numbers"); +}); + // Given something that isn't an array at all, such as "hey", 42 or no argument // When passed to the sum function // Then it should throw Error("sum requires an array of numbers") + +test("given something that isn't an array, throws an error", () => { + expect(() => sum("hey")).toThrow("sum requires an array of numbers"); + expect(() => sum(42)).toThrow("sum requires an array of numbers"); + expect(() => sum()).toThrow("sum requires an array of numbers"); +}); From 8191a04e1c198f22fa17af1ae0621d2031b4d06e Mon Sep 17 00:00:00 2001 From: Sakiya Mayow Date: Sun, 8 Nov 2026 04:08:22 +0000 Subject: [PATCH 11/12] "feat: implement sum function" --- Sprint-1/implement/sum.js | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/Sprint-1/implement/sum.js b/Sprint-1/implement/sum.js index 9062aafe3..e01821a2f 100644 --- a/Sprint-1/implement/sum.js +++ b/Sprint-1/implement/sum.js @@ -1,4 +1,21 @@ function sum(elements) { + if (!Array.isArray(elements)) { + throw new Error("sum requires an array of numbers"); + } + + for (const element of elements) { + if (typeof element !== "number") { + throw new Error("sum requires an array of numbers"); + } + } + + let total = 0; + + for (const element of elements) { + total += element; + } + + return total; } module.exports = sum; From 7a5ad817201b4fc15464958923203ff6ec55e4f4 Mon Sep 17 00:00:00 2001 From: Sakiya Mayow Date: Sun, 8 Nov 2026 04:17:23 +0000 Subject: [PATCH 12/12] "refactor: simplify includes function" --- Sprint-1/refactor/includes.js | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/Sprint-1/refactor/includes.js b/Sprint-1/refactor/includes.js index 29dad81f0..158c8ac96 100644 --- a/Sprint-1/refactor/includes.js +++ b/Sprint-1/refactor/includes.js @@ -1,13 +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]; - if (element === target) { - return true; - } - } - return false; + return list.includes(target); } module.exports = includes;