From bd388e49496e0128b08241df18aa534e864259c6 Mon Sep 17 00:00:00 2001 From: Rahana Suleiman Date: Sun, 1 Nov 2026 10:22:39 +0000 Subject: [PATCH 01/24] Implement input validation and error handling in calculateMedian function --- Sprint-1/fix/median.js | 34 ++++++++++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index 5c5b796e1..2f00b07ba 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -2,13 +2,39 @@ // 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' isn't an array, is empty, // 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; + // 1. Check if 'list' is a valid array and not null/undefined + if (!Array.isArray(list)) { + throw new Error("calculateMedian requires an array of numbers"); + } + + // 2. Check if the array is empty + if (list.length === 0) { + throw new Error("calculateMedian requires a non-empty array"); + } + + // 3. Check if every item in the array is a valid number (no strings, null, undefined, etc.) + for (const item of list) { + if (typeof item !== "number" || Number.isNaN(item)) { + throw new Error("calculateMedian requires an array of numbers"); + } + } + + // 4. Create a sorted copy of the array so we don't mutate the original input + const sorted = [...list].sort((a, b) => a - b); + const mid = Math.floor(sorted.length / 2); + + // 5. Calculate median for odd vs even length arrays + if (sorted.length % 2 !== 0) { + return sorted[mid]; + } else { + return (sorted[mid - 1] + sorted[mid]) / 2; + } } -module.exports = calculateMedian; +module.exports = calculateMedian; \ No newline at end of file From dbf4e214659dc2d8f464ad3cc8f6c394b68fc74f Mon Sep 17 00:00:00 2001 From: Rahana Suleiman Date: Sun, 1 Nov 2026 11:15:59 +0000 Subject: [PATCH 02/24] Add a blank line for better readability in dedupe test file --- Sprint-1/implement/dedupe.test.js | 1 + 1 file changed, 1 insertion(+) diff --git a/Sprint-1/implement/dedupe.test.js b/Sprint-1/implement/dedupe.test.js index d7c8e3d8e..8182af7ee 100644 --- a/Sprint-1/implement/dedupe.test.js +++ b/Sprint-1/implement/dedupe.test.js @@ -18,6 +18,7 @@ E.g. dedupe([1, 2, 1]) returns [1, 2] // 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 // Then it should return a copy of the original array From 1e13f783af03204b64404869da4df9d725974552 Mon Sep 17 00:00:00 2001 From: Rahana Suleiman Date: Sun, 1 Nov 2026 11:36:42 +0000 Subject: [PATCH 03/24] Add tests for dedupe function to validate behavior with empty and unique arrays --- Sprint-1/implement/dedupe.test.js | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/Sprint-1/implement/dedupe.test.js b/Sprint-1/implement/dedupe.test.js index 8182af7ee..73fd8841e 100644 --- a/Sprint-1/implement/dedupe.test.js +++ b/Sprint-1/implement/dedupe.test.js @@ -18,12 +18,33 @@ E.g. dedupe([1, 2, 1]) returns [1, 2] // Then it should return an empty array test.todo("given an empty array, it returns an empty array"); +it("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.todo("given an array, it returns a copy of the original array"); + +it("given an array, it returns a copy of the original array", () => { +const input = [1,2,3]; +const result = dedupe(input) + + expect(result).toEqual([1,2,3]); + expect(result).not.toBe(input); +}); // 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 // first occurrence of each element from the original array. + +test.todo("given an array, it returns a duplicate while preserving the first occurrence of each element from the original array"); + +it("given an array, it removes duplicates while preserving the first occurrence of elements", () => { +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 d940b04ee6b274395dca887d27a11df69cb57241 Mon Sep 17 00:00:00 2001 From: Rahana Suleiman Date: Sun, 1 Nov 2026 11:38:35 +0000 Subject: [PATCH 04/24] Implement dedupe function to remove duplicate values from an array --- Sprint-1/implement/dedupe.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Sprint-1/implement/dedupe.js b/Sprint-1/implement/dedupe.js index 781e8718a..20f2603e0 100644 --- a/Sprint-1/implement/dedupe.js +++ b/Sprint-1/implement/dedupe.js @@ -1 +1,4 @@ -function dedupe() {} +function dedupe(arr) { + return [... new Set(arr)] +} +module.exports = dedupe; From 26c125ec3cd77d3a0d91d3da1279c1c50f7d15df Mon Sep 17 00:00:00 2001 From: Rahana Suleiman Date: Sun, 1 Nov 2026 11:54:51 +0000 Subject: [PATCH 05/24] Implement describeMedian function to calculate and return the median value, with error handling for invalid inputs --- 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..da67bbd60 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 { + return `Could not calculate a median: ${error.message}`; + } +} module.exports = describeMedian; From 96de1955913b9725020c086d5c669346ae74c774 Mon Sep 17 00:00:00 2001 From: Rahana Suleiman Date: Sun, 1 Nov 2026 11:55:04 +0000 Subject: [PATCH 06/24] Add tests for describeMedian function to validate median calculation and error handling --- Sprint-1/implement/describe-median.test.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/Sprint-1/implement/describe-median.test.js b/Sprint-1/implement/describe-median.test.js index e7d18a264..5fad3d748 100644 --- a/Sprint-1/implement/describe-median.test.js +++ b/Sprint-1/implement/describe-median.test.js @@ -23,10 +23,19 @@ const describeMedian = require("./describe-median.js"); // Delete this test.todo and replace it with a test. test.todo('given [1, 2, 3], returns "The median is 2"'); +describe("describeMedian", () => { + it("given [1, 2, 3], returns {The median is 2}", () => { + expect(describeMedian([1, 2, 3])).toEqual("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" + + // 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" From 9ec2074c8a218894b3aa373640eff967a0449656 Mon Sep 17 00:00:00 2001 From: Rahana Suleiman Date: Sun, 1 Nov 2026 11:57:17 +0000 Subject: [PATCH 07/24] Add test case for describeMedian to handle empty array input --- Sprint-1/implement/describe-median.test.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Sprint-1/implement/describe-median.test.js b/Sprint-1/implement/describe-median.test.js index 5fad3d748..12b4254bf 100644 --- a/Sprint-1/implement/describe-median.test.js +++ b/Sprint-1/implement/describe-median.test.js @@ -34,7 +34,13 @@ describe("describeMedian", () => { // When passed to describeMedian // Then it should return "Could not calculate a median: calculateMedian requires a non-empty array" +test.todo('given [1, 2, 3], returns "The median is 2"'); +describe("describeMedian", () => { + it("given [], returns Could not calculate a median : calculateMedian requires a non-empty array", () => { + expect(describeMedian([])).toEqual("calculateMedian requires a non-empty array"); + }) +}) // Given something that isn't an array of numbers, e.g. "banana" // When passed to describeMedian From 547e68b658c544c082df3080567948ed6b34a6af Mon Sep 17 00:00:00 2001 From: Rahana Suleiman Date: Sun, 1 Nov 2026 12:00:58 +0000 Subject: [PATCH 08/24] Add test case for describeMedian to handle non-array input --- Sprint-1/implement/describe-median.test.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/Sprint-1/implement/describe-median.test.js b/Sprint-1/implement/describe-median.test.js index 12b4254bf..a1971366c 100644 --- a/Sprint-1/implement/describe-median.test.js +++ b/Sprint-1/implement/describe-median.test.js @@ -45,3 +45,10 @@ describe("describeMedian", () => { // 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.todo('given [1, 2, 3], returns "The median is 2"'); + +describe("describeMedian", () => { + it("given something is not an array, returns Could not calculate a median : calculateMedian requires an array of numbers", () => { + expect(describeMedian([])).toEqual("calculateMedian requires an array of numbers"); + }) +}) \ No newline at end of file From fe34b63a57adcc8bb93d3ba4dcb302de92fb4341 Mon Sep 17 00:00:00 2001 From: Rahana Suleiman Date: Sun, 1 Nov 2026 12:02:54 +0000 Subject: [PATCH 09/24] Refactor describeMedian tests to remove todos and ensure correct error handling for empty arrays --- Sprint-1/implement/describe-median.test.js | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/Sprint-1/implement/describe-median.test.js b/Sprint-1/implement/describe-median.test.js index a1971366c..37d36d85c 100644 --- a/Sprint-1/implement/describe-median.test.js +++ b/Sprint-1/implement/describe-median.test.js @@ -26,22 +26,20 @@ test.todo('given [1, 2, 3], returns "The median is 2"'); describe("describeMedian", () => { it("given [1, 2, 3], returns {The median is 2}", () => { expect(describeMedian([1, 2, 3])).toEqual("The median is 2"); - }) -}) + }); + + it("given an empty array, returns the correct error message", () => { + expect(describeMedian([])).toEqual( + "Could not calculate a median: calculateMedian requires a non-empty array" + ); + }); +}); // Given an empty array // When passed to describeMedian // Then it should return "Could not calculate a median: calculateMedian requires a non-empty array" -test.todo('given [1, 2, 3], returns "The median is 2"'); - -describe("describeMedian", () => { - it("given [], returns Could not calculate a median : calculateMedian requires a non-empty array", () => { - expect(describeMedian([])).toEqual("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" From 411ad3fa8329644d9ef427ad43eff2be5d18200f Mon Sep 17 00:00:00 2001 From: Rahana Suleiman Date: Sun, 1 Nov 2026 12:03:57 +0000 Subject: [PATCH 10/24] Refactor describeMedian tests to improve clarity and organization --- Sprint-1/implement/describe-median.test.js | 29 ++++++++++------------ 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/Sprint-1/implement/describe-median.test.js b/Sprint-1/implement/describe-median.test.js index 37d36d85c..abdce10b8 100644 --- a/Sprint-1/implement/describe-median.test.js +++ b/Sprint-1/implement/describe-median.test.js @@ -28,25 +28,22 @@ describe("describeMedian", () => { expect(describeMedian([1, 2, 3])).toEqual("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" + it("given an empty array, returns the correct error message", () => { expect(describeMedian([])).toEqual( "Could not calculate a median: calculateMedian requires a non-empty array" ); }); -}); - - -// Given an empty array -// When passed to describeMedian -// Then it should return "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.todo('given [1, 2, 3], returns "The median is 2"'); -describe("describeMedian", () => { - it("given something is not an array, returns Could not calculate a median : calculateMedian requires an array of numbers", () => { - expect(describeMedian([])).toEqual("calculateMedian requires an array of numbers"); - }) -}) \ No newline at end of file + // 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" + it("given something that isn't an array, returns the correct error message", () => { + expect(describeMedian("banana")).toEqual( + "Could not calculate a median: calculateMedian requires an array of numbers" + ); + }); +}); From 1c76aa49f538fbb95a39356f2db15c172c22072b Mon Sep 17 00:00:00 2001 From: Rahana Suleiman Date: Sun, 1 Nov 2026 12:06:37 +0000 Subject: [PATCH 11/24] Improve error handling in describeMedian by capturing error parameter --- Sprint-1/implement/describe-median.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sprint-1/implement/describe-median.js b/Sprint-1/implement/describe-median.js index da67bbd60..23764c40a 100644 --- a/Sprint-1/implement/describe-median.js +++ b/Sprint-1/implement/describe-median.js @@ -24,7 +24,7 @@ function describeMedian(list) { try { const median = calculateMedian(list); return `The median is ${median}`; - } catch { + } catch(error) { return `Could not calculate a median: ${error.message}`; } } From 34fbfe574b0d1ae3d24fe090c1453752e2dbe08e Mon Sep 17 00:00:00 2001 From: Rahana Suleiman Date: Sun, 1 Nov 2026 12:14:00 +0000 Subject: [PATCH 12/24] Implement findMax function with input validation and error handling --- Sprint-1/implement/max.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/Sprint-1/implement/max.js b/Sprint-1/implement/max.js index 6dd76378e..ea0b63558 100644 --- a/Sprint-1/implement/max.js +++ b/Sprint-1/implement/max.js @@ -1,4 +1,23 @@ function findMax(elements) { + // 1. Check if input is a valid array and not null/undefined + if (!Array.isArray(elements)) { + throw new Error("findMax requires an array of numbers"); + } + + // 2. Handle empty array edge case per requirements + if (elements.length === 0) { + return -Infinity; + } + + // 3. Validate that every item in the array is strictly a number (no strings, null, etc.) + for (const item of elements) { + if (typeof item !== "number" || Number.isNaN(item)) { + throw new Error("findMax requires an array of numbers"); + } + } + + // 4. Return the maximum number using Math.max + return Math.max(...elements); } module.exports = findMax; From 866d1d04ca07a56ff273fa83f64b747b6deace1b Mon Sep 17 00:00:00 2001 From: Rahana Suleiman Date: Sun, 1 Nov 2026 12:14:09 +0000 Subject: [PATCH 13/24] Add tests for findMax function to cover various scenarios including empty arrays, negative numbers, decimals, and non-array inputs --- Sprint-1/implement/max.test.js | 71 +++++++++++++++++++++++++--------- 1 file changed, 52 insertions(+), 19 deletions(-) diff --git a/Sprint-1/implement/max.test.js b/Sprint-1/implement/max.test.js index ff3fbb5e5..8b136e896 100644 --- a/Sprint-1/implement/max.test.js +++ b/Sprint-1/implement/max.test.js @@ -16,28 +16,61 @@ 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"); +describe("findMax", () => { + it("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 + // Given an array with one number + // When passed to the max function + // Then it should return that number -// Given an array with both positive and negative numbers -// When passed to the max function -// Then it should return the largest number overall + it("given an array with one number, returns that number", () => { + expect(findMax([42])).toEqual(42); + }); -// Given an array with just negative numbers -// When passed to the max function -// Then it should return the closest one to zero + // Given an array with both positive and negative numbers + // When passed to the max function + // Then it should return the largest number overall + it("given an array with both positive and negative numbers, returns the largest number overall", () => { + expect(findMax([30, -50, 10, 40, -5])).toEqual(40); + }); -// Given an array with decimal numbers -// When passed to the max function -// Then it should return the largest decimal number + // Given an array with just negative numbers + // When passed to the max function + // Then it should return the closest one to zero + it("given an array with just negative numbers, returns the closest one to zero", () => { + expect(findMax([-10, -3, -50, -5])).toEqual(-3); + }); -// 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") + // Given an array with decimal numbers + // When passed to the max function + // Then it should return the largest decimal number + it("given an array with decimal numbers, returns the largest decimal number", () => { + expect(findMax([1.5, 2.7, 2.1, 0.4])).toEqual(2.7); + }); -// 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") + // 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") + it("given an array containing a value that isn't a number, throws an error", () => { + expect(() => findMax(["hey", 10, "hi", 60, 10])).toThrow( + new Error("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") + it("given something that isn't an array at all, throws an error", () => { + expect(() => findMax("hey")).toThrow( + new Error("findMax requires an array of numbers") + ); + expect(() => findMax(42)).toThrow( + new Error("findMax requires an array of numbers") + ); + expect(() => findMax()).toThrow( + new Error("findMax requires an array of numbers") + ); + }); +}); From bc3b0d2a15f56612d60a5d691f21cbb261b41ac5 Mon Sep 17 00:00:00 2001 From: Rahana Suleiman Date: Mon, 2 Nov 2026 21:32:01 +0000 Subject: [PATCH 14/24] Implement calculateMean function with input validation and error handling --- Sprint-1/implement/mean.js | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/Sprint-1/implement/mean.js b/Sprint-1/implement/mean.js index 36909ea1f..b7ac32e55 100644 --- a/Sprint-1/implement/mean.js +++ b/Sprint-1/implement/mean.js @@ -1,3 +1,25 @@ -function calculateMean(list) {} +function calculateMean(list) { + // 1. Check if input is a valid array + if (!Array.isArray(list)) { + throw new Error("calculateMean requires an array of numbers"); + } -module.exports = calculateMean; + // 2. Check if the array is empty + if (list.length === 0) { + throw new Error("calculateMean requires a non-empty array"); + } + + // 3. Validate that every item in the array is strictly a number + let sum = 0; + for (const item of list) { + if (typeof item !== "number" || Number.isNaN(item)) { + throw new Error("calculateMean requires an array of numbers"); + } + sum += item; + } + + // 4. Return the calculated mean + return sum / list.length; +} + +module.exports = calculateMean; \ No newline at end of file From ddaa794594f6c8bbe41d853d0f64db50afff8d68 Mon Sep 17 00:00:00 2001 From: Rahana Suleiman Date: Mon, 2 Nov 2026 21:34:03 +0000 Subject: [PATCH 15/24] Add test for calculateMean function to validate mean calculation for an array of numbers --- Sprint-1/implement/mean.test.js | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/Sprint-1/implement/mean.test.js b/Sprint-1/implement/mean.test.js index 847b88676..8eb65faa6 100644 --- a/Sprint-1/implement/mean.test.js +++ b/Sprint-1/implement/mean.test.js @@ -19,11 +19,18 @@ const calculateMean = require("./mean.js"); // Acceptance criteria: -// Given an array of numbers + +// Delete this test.todo and replace it with a test. +describe("calculateMean", () => { + + // Given an array of numbers // 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"); + + it("given [1, 2, 6], returns 3", () => { + expect(calculateMean([1, 2, 6])).toEqual(3); + }); +}) // Given an array with a single number // When passed to calculateMean From 3e45546d4884dc406d1bd70703c0678a687fd971 Mon Sep 17 00:00:00 2001 From: Rahana Suleiman Date: Mon, 2 Nov 2026 21:34:36 +0000 Subject: [PATCH 16/24] Add test for calculateMean function to verify single number input --- Sprint-1/implement/mean.test.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/Sprint-1/implement/mean.test.js b/Sprint-1/implement/mean.test.js index 8eb65faa6..c391cefda 100644 --- a/Sprint-1/implement/mean.test.js +++ b/Sprint-1/implement/mean.test.js @@ -30,12 +30,18 @@ describe("calculateMean", () => { it("given [1, 2, 6], returns 3", () => { expect(calculateMean([1, 2, 6])).toEqual(3); }); -}) // Given an array with a single number // When passed to calculateMean // Then it should return that number +it("given an array with a single number, returns that number", () => { + expect(calculateMean([10])).toEqual(10); + }); +}) + + + // Given an array containing negative or decimal numbers // When passed to calculateMean // Then it should return the correct mean From b1069feceae5275f026f8bb86d3a0ef0328c38b2 Mon Sep 17 00:00:00 2001 From: Rahana Suleiman Date: Mon, 2 Nov 2026 21:35:16 +0000 Subject: [PATCH 17/24] Add test for calculateMean function to validate mean calculation for negative and decimal numbers --- Sprint-1/implement/mean.test.js | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/Sprint-1/implement/mean.test.js b/Sprint-1/implement/mean.test.js index c391cefda..4e966e476 100644 --- a/Sprint-1/implement/mean.test.js +++ b/Sprint-1/implement/mean.test.js @@ -38,14 +38,21 @@ describe("calculateMean", () => { it("given an array with a single number, returns that number", () => { expect(calculateMean([10])).toEqual(10); }); -}) - - // Given an array containing negative or decimal numbers // When passed to calculateMean // Then it should return the correct mean +it("given an array containing negative or decimal numbers, returns the correct mean", () => { + expect(calculateMean([10, -2, 6])).toEqual(4); + expect(calculateMean([1.5, 2.5])).toEqual(2); + }); +}) + + + + + // Given an empty array // When passed to calculateMean // Then it should throw Error("calculateMean requires a non-empty array") From 4faa8075b6283b2dc83d57bbe6628fc055d83671 Mon Sep 17 00:00:00 2001 From: Rahana Suleiman Date: Mon, 2 Nov 2026 21:36:47 +0000 Subject: [PATCH 18/24] Add tests for calculateMean function to verify error handling for empty arrays and non-array inputs --- Sprint-1/implement/mean.test.js | 35 ++++++++++++++++++++++++++++----- 1 file changed, 30 insertions(+), 5 deletions(-) diff --git a/Sprint-1/implement/mean.test.js b/Sprint-1/implement/mean.test.js index 4e966e476..4104f379e 100644 --- a/Sprint-1/implement/mean.test.js +++ b/Sprint-1/implement/mean.test.js @@ -47,20 +47,45 @@ it("given an array containing negative or decimal numbers, returns the correct m expect(calculateMean([10, -2, 6])).toEqual(4); expect(calculateMean([1.5, 2.5])).toEqual(2); }); -}) - - - - // Given an empty array // When passed to calculateMean // Then it should throw Error("calculateMean requires a non-empty array") +it("given an empty array, throws the correct error", () => { + expect(() => calculateMean([])).toThrow( + new Error("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") +it("given a value that isn't an array (string, number, null, object), throws the correct error", () => { + expect(() => calculateMean("banana")).toThrow( + new Error("calculateMean requires an array of numbers") + ); + expect(() => calculateMean(42)).toThrow( + new Error("calculateMean requires an array of numbers") + ); + expect(() => calculateMean(null)).toThrow( + new Error("calculateMean requires an array of numbers") + ); + expect(() => calculateMean({})).toThrow( + new Error("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") From 35df2b2cd7b5c4368287aea77cc31ea9e394ec8c Mon Sep 17 00:00:00 2001 From: Rahana Suleiman Date: Mon, 2 Nov 2026 21:37:24 +0000 Subject: [PATCH 19/24] Add test for calculateMean function to verify error handling for no arguments --- Sprint-1/implement/mean.test.js | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/Sprint-1/implement/mean.test.js b/Sprint-1/implement/mean.test.js index 4104f379e..4cef205ca 100644 --- a/Sprint-1/implement/mean.test.js +++ b/Sprint-1/implement/mean.test.js @@ -76,6 +76,16 @@ it("given a value that isn't an array (string, number, null, object), throws the new Error("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") + +it("given no argument at all, throws the correct error", () => { + expect(() => calculateMean()).toThrow( + new Error("calculateMean requires an array of numbers") + ); + }); }) @@ -86,9 +96,7 @@ it("given a value that isn't an array (string, number, null, object), throws the -// Given no argument at all -// When passed to calculateMean -// Then it should throw Error("calculateMean requires an array of numbers") + // Given an array containing a non-number value, e.g. [1, "2", 3] // When passed to calculateMean From 9d10901f13cef7532a81eee347b8abdd0b7d016f Mon Sep 17 00:00:00 2001 From: Rahana Suleiman Date: Mon, 2 Nov 2026 21:37:59 +0000 Subject: [PATCH 20/24] Add tests for calculateMean function to validate error handling for non-number values and no arguments --- Sprint-1/implement/mean.test.js | 68 +++++++++++++++------------------ 1 file changed, 31 insertions(+), 37 deletions(-) diff --git a/Sprint-1/implement/mean.test.js b/Sprint-1/implement/mean.test.js index 4cef205ca..f4a54f181 100644 --- a/Sprint-1/implement/mean.test.js +++ b/Sprint-1/implement/mean.test.js @@ -19,50 +19,48 @@ const calculateMean = require("./mean.js"); // Acceptance criteria: - // Delete this test.todo and replace it with a test. describe("calculateMean", () => { - // Given an array of numbers -// When passed to calculateMean -// Then it should return their mean + // When passed to calculateMean + // Then it should return their mean it("given [1, 2, 6], returns 3", () => { expect(calculateMean([1, 2, 6])).toEqual(3); }); -// Given an array with a single number -// When passed to calculateMean -// Then it should return that number + // Given an array with a single number + // When passed to calculateMean + // Then it should return that number -it("given an array with a single number, returns that number", () => { + it("given an array with a single number, returns that number", () => { expect(calculateMean([10])).toEqual(10); }); -// Given an array containing negative or decimal numbers -// When passed to calculateMean -// Then it should return the correct mean + // Given an array containing negative or decimal numbers + // When passed to calculateMean + // Then it should return the correct mean -it("given an array containing negative or decimal numbers, returns the correct mean", () => { + it("given an array containing negative or decimal numbers, returns the correct mean", () => { expect(calculateMean([10, -2, 6])).toEqual(4); expect(calculateMean([1.5, 2.5])).toEqual(2); }); -// Given an empty array -// When passed to calculateMean -// Then it should throw Error("calculateMean requires a non-empty array") + // Given an empty array + // When passed to calculateMean + // Then it should throw Error("calculateMean requires a non-empty array") -it("given an empty array, throws the correct error", () => { + it("given an empty array, throws the correct error", () => { expect(() => calculateMean([])).toThrow( new Error("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") + // 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") -it("given a value that isn't an array (string, number, null, object), throws the correct error", () => { + it("given a value that isn't an array (string, number, null, object), throws the correct error", () => { expect(() => calculateMean("banana")).toThrow( new Error("calculateMean requires an array of numbers") ); @@ -77,27 +75,23 @@ it("given a value that isn't an array (string, number, null, object), throws the ); }); -// Given no argument at all -// When passed to calculateMean -// Then it should throw Error("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") -it("given no argument at all, throws the correct error", () => { + it("given no argument at all, throws the correct error", () => { expect(() => calculateMean()).toThrow( new Error("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") - - - - -// 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") + it("given an array containing a non-number value, throws the correct error", () => { + expect(() => calculateMean([1, "2", 3])).toThrow( + new Error("calculateMean requires an array of numbers") + ); + }); +}); From 292c6ed21cf22168c4f008f6629ba9a71bfbe2f9 Mon Sep 17 00:00:00 2001 From: Rahana Suleiman Date: Mon, 2 Nov 2026 21:40:13 +0000 Subject: [PATCH 21/24] Update test for calculateMean function to use different input values for mean calculation --- Sprint-1/implement/mean.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sprint-1/implement/mean.test.js b/Sprint-1/implement/mean.test.js index f4a54f181..8fd9b4bff 100644 --- a/Sprint-1/implement/mean.test.js +++ b/Sprint-1/implement/mean.test.js @@ -42,7 +42,7 @@ describe("calculateMean", () => { // Then it should return the correct mean it("given an array containing negative or decimal numbers, returns the correct mean", () => { - expect(calculateMean([10, -2, 6])).toEqual(4); + expect(calculateMean([6, -2, 8])).toEqual(4); // 6 + (-2) + 8 = 12 / 3 = 4 expect(calculateMean([1.5, 2.5])).toEqual(2); }); From 59d9646e809d9f7813f63f8fc0c068e8ccc8edf3 Mon Sep 17 00:00:00 2001 From: Rahana Suleiman Date: Mon, 2 Nov 2026 22:00:49 +0000 Subject: [PATCH 22/24] Add sum function to calculate the total of an array, including input validation --- Sprint-1/implement/sum.js | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/Sprint-1/implement/sum.js b/Sprint-1/implement/sum.js index 9062aafe3..b263048d7 100644 --- a/Sprint-1/implement/sum.js +++ b/Sprint-1/implement/sum.js @@ -1,4 +1,25 @@ function sum(elements) { + // 1. Check if input is a valid array + if (!Array.isArray(elements)) { + throw new Error("sum requires an array of numbers"); + } + + // 2. Handle empty array edge case (sum of empty array is 0) + if (elements.length === 0) { + return 0; + } + + // 3. Validate that every item in the array is strictly a number + let total = 0; + for (const item of elements) { + if (typeof item !== "number" || Number.isNaN(item)) { + throw new Error("sum requires an array of numbers"); + } + total += item; + } + + // 4. Return the calculated sum + return total; } -module.exports = sum; +module.exports = sum; \ No newline at end of file From 55d75287f62baa3c3ac5f00ebe9b0146c753fd05 Mon Sep 17 00:00:00 2001 From: Rahana Suleiman Date: Mon, 2 Nov 2026 22:04:25 +0000 Subject: [PATCH 23/24] Add tests for sum function to validate various input scenarios and error handling --- Sprint-1/implement/sum.test.js | 50 ++++++++++++++++++++++++++++++++-- 1 file changed, 48 insertions(+), 2 deletions(-) diff --git a/Sprint-1/implement/sum.test.js b/Sprint-1/implement/sum.test.js index a288e0f0a..6725eac57 100644 --- a/Sprint-1/implement/sum.test.js +++ b/Sprint-1/implement/sum.test.js @@ -10,27 +10,73 @@ const sum = require("./sum.js"); // Acceptance Criteria: + +describe("sum", () => { // Given an empty array // When passed to the sum function // Then it should return 0 -test.todo("given an empty array, returns 0") + +it("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 +it("given an array with just 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 +it("given an array containing negative numbers, returns the correct total sum", () => { + expect(sum([10, -20, 30])).toEqual(20); + }); + // Given an array with decimal/float numbers // When passed to the sum function // Then it should return the correct total sum -// Given an array containing a value that isn't a number +it("given an array with decimal/float numbers, returns the correct total sum", () => { + expect(sum([1.5, 2.5, 3.0])).toEqual(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") +it("given an array containing a value that isn't a number, throws an error", () => { + expect(() => sum(["hey", 10, "hi", 60, 10])).toThrow( + new Error("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") + +it("given something that isn't an array at all, throws an error", () => { + expect(() => sum("hey")).toThrow( + new Error("sum requires an array of numbers") + ); + expect(() => sum(42)).toThrow( + new Error("sum requires an array of numbers") + ); + expect(() => sum()).toThrow( + new Error("sum requires an array of numbers") + ); +}) +}) + + + + + + + + + + From 02e7fdcac7373d9e50539793e4a3e8b29dbd698d Mon Sep 17 00:00:00 2001 From: Rahana Suleiman Date: Mon, 2 Nov 2026 22:10:16 +0000 Subject: [PATCH 24/24] Refactor includes function to use for...of loop for improved readability --- Sprint-1/refactor/includes.js | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/Sprint-1/refactor/includes.js b/Sprint-1/refactor/includes.js index 29dad81f0..8474eb211 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 +// Refactored implementation of includes using 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 +9,4 @@ function includes(list, target) { return false; } -module.exports = includes; +module.exports = includes; \ No newline at end of file