From 42e667009ad034b2733e183840a84006007df225 Mon Sep 17 00:00:00 2001 From: Mahir Shah Date: Wed, 28 Oct 2026 17:44:08 +0000 Subject: [PATCH 01/14] Fix calculateMedian function, Implement error checking --- Sprint-1/fix/median.js | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index 5c5b796e1..7928ccc95 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -6,8 +6,30 @@ // 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]; + 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( + (currentValue) => + typeof currentValue === "number" && !Number.isNaN(currentValue) + ) + ) { + throw new Error("calculateMedian requires an array of numbers"); + } + + const sortedList = list.toSorted((a, b) => a - b); + const middleIndex = Math.floor(sortedList.length / 2); + let median; + if (sortedList.length % 2 === 0) { + median = (sortedList[middleIndex - 1] + sortedList[middleIndex]) / 2; + } else { + median = sortedList[middleIndex]; + } return median; } From b20550fb29ded61a3decd817ee18078a9d9e6c41 Mon Sep 17 00:00:00 2001 From: Mahir Shah Date: Thu, 29 Oct 2026 15:33:34 +0000 Subject: [PATCH 02/14] Implement tests --- Sprint-1/implement/max.test.js | 60 ++++++++++++++++++---------------- 1 file changed, 31 insertions(+), 29 deletions(-) diff --git a/Sprint-1/implement/max.test.js b/Sprint-1/implement/max.test.js index ff3fbb5e5..0049de42d 100644 --- a/Sprint-1/implement/max.test.js +++ b/Sprint-1/implement/max.test.js @@ -12,32 +12,34 @@ We have set things up already so that this file can see your function from the o 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"); - -// 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 - -// 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 decimal numbers -// When passed to the max function -// Then it should return the largest decimal number - -// 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 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 an empty array, returns -Infinity", () => { + expect(findMax([])).toEqual(-Infinity); +}); + +test("given an array with one number, returns that number", () => { + expect(findMax([10])).toEqual(10); +}); + +test("given an array with positive and negative numbers, returns largest number overall", () => { + expect(findMax([10, 58, -74, 984, -87, -995, 47])).toEqual(984); +}); + +test("given an array with only negative numbers, returns number closest to zero", () => { + expect(findMax([-58, -65, -7, -2, -96, -753])).toEqual(-2); +}); + +test("given an array with decimal numbers, returns largest decimal number", () => { + expect(findMax([10, 9.5, 65.6, 41, 36.8])).toEqual(65.6); +}); + +test("given an array with with value that isn't a number, throws error", () => { + expect(() => findMax([10, 23, 18, "hello", 89, 2])).toThrow( + new Error("findMax requires an array of numbers") + ); +}); + +test("given something that isn't an array, throws error", () => { + expect(() => + findMax("Hello").toThrow(new Error("findMax requires an array of numbers")) + ); +}); From 6af26a25c4f36dbfb5c4dfa2ccb47c3f6b4072f6 Mon Sep 17 00:00:00 2001 From: Mahir Shah Date: Thu, 29 Oct 2026 15:41:42 +0000 Subject: [PATCH 03/14] Implement findMax function --- Sprint-1/implement/max.js | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/Sprint-1/implement/max.js b/Sprint-1/implement/max.js index 6dd76378e..886687cf6 100644 --- a/Sprint-1/implement/max.js +++ b/Sprint-1/implement/max.js @@ -1,4 +1,24 @@ function findMax(elements) { + if (!Array.isArray(elements)) { + throw new Error("findMax requires an array of numbers"); + } + + if (elements.length === 0) { + return -Infinity; + } + + if ( + !elements.every( + (currentValue) => + typeof currentValue === "number" && !Number.isNaN(currentValue) + ) + ) { + throw new Error("findMax requires an array of numbers"); + } + + sortedElements = elements.toSorted((a, b) => a - b); + + return sortedElements[sortedElements.length - 1]; } module.exports = findMax; From 7a3e4e510600cb701fa96fd575ec805178303f8a Mon Sep 17 00:00:00 2001 From: Mahir Shah Date: Thu, 29 Oct 2026 17:14:33 +0000 Subject: [PATCH 04/14] Add tests --- Sprint-1/implement/sum.test.js | 53 +++++++++++++++++++--------------- 1 file changed, 29 insertions(+), 24 deletions(-) diff --git a/Sprint-1/implement/sum.test.js b/Sprint-1/implement/sum.test.js index a288e0f0a..ea02ff440 100644 --- a/Sprint-1/implement/sum.test.js +++ b/Sprint-1/implement/sum.test.js @@ -10,27 +10,32 @@ 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") - -// Given an array with just one number -// When passed to the sum function -// Then it should return that number - -// Given an array containing negative numbers -// When passed to the sum function -// Then it should still return the correct total sum - -// 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 -// When passed to the sum function -// Then it should throw 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") +test("given an empty array, returns 0", () => { + expect(sum([])).toEqual(0); +}); + +test("given an array with one number, return that number", () => { + expect(sum([5])).toEqual(5); +}); + +test("given an array with negative numbers, correctly return sum of numbers", () => { + expect(sum[(-10, -45, -6, -74)]).toEqual(-135); +}); + +test("given an array with decimal numbers, correctly return total sum", () => { + expect(sum([12.7, 3.6, 32.48, 8, 32.53])).toEqual(89.31); +}); + +test("given an array with some values that aren't numbers, throw an Error", () => { + expect(() => { + sum([10, 8, 32, "hello", 2, 53, 32]).toThrow( + new Error("sum requires an array of numbers") + ); + }); +}); + +test("given something that isn't an array, throw an Error", () => { + expect(() => { + sum("hello").toThrow(new Error("sum requires an array of numbers")); + }); +}); From 36a519da0d1a3dffad7377b8cc8b706f9f572b2c Mon Sep 17 00:00:00 2001 From: Mahir Shah Date: Thu, 29 Oct 2026 17:28:19 +0000 Subject: [PATCH 05/14] Fix error tests --- Sprint-1/implement/max.test.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Sprint-1/implement/max.test.js b/Sprint-1/implement/max.test.js index 0049de42d..ec46b1a71 100644 --- a/Sprint-1/implement/max.test.js +++ b/Sprint-1/implement/max.test.js @@ -39,7 +39,7 @@ test("given an array with with value that isn't a number, throws error", () => { }); test("given something that isn't an array, throws error", () => { - expect(() => - findMax("Hello").toThrow(new Error("findMax requires an array of numbers")) + expect(() => findMax("Hello")).toThrow( + new Error("findMax requires an array of numbers") ); }); From e97ee714124bad8c7dc809faba1001275925bbdc Mon Sep 17 00:00:00 2001 From: Mahir Shah Date: Thu, 29 Oct 2026 17:31:01 +0000 Subject: [PATCH 06/14] Fix error tests --- Sprint-1/implement/sum.test.js | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/Sprint-1/implement/sum.test.js b/Sprint-1/implement/sum.test.js index ea02ff440..a2cfb3389 100644 --- a/Sprint-1/implement/sum.test.js +++ b/Sprint-1/implement/sum.test.js @@ -19,7 +19,7 @@ test("given an array with one number, return that number", () => { }); test("given an array with negative numbers, correctly return sum of numbers", () => { - expect(sum[(-10, -45, -6, -74)]).toEqual(-135); + expect(sum([-10, -45, -6, -74])).toEqual(-135); }); test("given an array with decimal numbers, correctly return total sum", () => { @@ -28,14 +28,12 @@ test("given an array with decimal numbers, correctly return total sum", () => { test("given an array with some values that aren't numbers, throw an Error", () => { expect(() => { - sum([10, 8, 32, "hello", 2, 53, 32]).toThrow( - new Error("sum requires an array of numbers") - ); - }); + sum([10, 8, 32, "hello", 2, 53, 32]); + }).toThrow(new Error("sum requires an array of numbers")); }); test("given something that isn't an array, throw an Error", () => { expect(() => { - sum("hello").toThrow(new Error("sum requires an array of numbers")); - }); + sum("hello"); + }).toThrow(new Error("sum requires an array of numbers")); }); From 57f287552f9d66b031be94b3a21d264f389c4fed Mon Sep 17 00:00:00 2001 From: Mahir Shah Date: Thu, 29 Oct 2026 17:42:42 +0000 Subject: [PATCH 07/14] Implement sum function --- Sprint-1/implement/sum.js | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/Sprint-1/implement/sum.js b/Sprint-1/implement/sum.js index 9062aafe3..b7c76e9e3 100644 --- a/Sprint-1/implement/sum.js +++ b/Sprint-1/implement/sum.js @@ -1,4 +1,22 @@ function sum(elements) { + if (!Array.isArray(elements)) { + throw new Error("sum requires an array of numbers"); + } + + if ( + !elements.every( + (currentValue) => + typeof currentValue === "number" && !Number.isNaN(currentValue) + ) + ) { + throw new Error("sum requires an array of numbers"); + } + + let total = 0; + for (number of elements) { + total += number; + } + return total; } module.exports = sum; From 1b7b599fef950972bc3041eb6ec56d6b7ab66610 Mon Sep 17 00:00:00 2001 From: Mahir Shah Date: Thu, 29 Oct 2026 18:10:48 +0000 Subject: [PATCH 08/14] Add tests --- Sprint-1/implement/dedupe.test.js | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/Sprint-1/implement/dedupe.test.js b/Sprint-1/implement/dedupe.test.js index d7c8e3d8e..dec42086e 100644 --- a/Sprint-1/implement/dedupe.test.js +++ b/Sprint-1/implement/dedupe.test.js @@ -13,16 +13,20 @@ E.g. dedupe([1, 2, 1]) returns [1, 2] // Acceptance Criteria: -// 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, should return a copy of the same array", () => { + expect(dedupe([1, 5, 9, 23])).toEqual([1, 5, 9, 23]); +}); -// 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("given an array with strings or numbers, should return an array with duplicates removed and first occurrence preserved", () => { + expect(dedupe([1, 6, 1, "a", 5, 6, "a", "b", "b"])).toEqual([ + 1, + 6, + "a", + 5, + "b", + ]); +}); From 479a30c81f99188996ab5e3bd6158f2b20886b49 Mon Sep 17 00:00:00 2001 From: Mahir Shah Date: Thu, 29 Oct 2026 18:11:07 +0000 Subject: [PATCH 09/14] Implement dedupe function --- Sprint-1/implement/dedupe.js | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/Sprint-1/implement/dedupe.js b/Sprint-1/implement/dedupe.js index 781e8718a..5631aceef 100644 --- a/Sprint-1/implement/dedupe.js +++ b/Sprint-1/implement/dedupe.js @@ -1 +1,12 @@ -function dedupe() {} +function dedupe(elements) { + const dedupedArray = []; + + for (value of elements) { + if (!dedupedArray.includes(value)) { + dedupedArray.push(value); + } + } + return dedupedArray; +} + +module.exports = dedupe; From 42bdd8dbff12f76a0e2ae2377d2e53643aafc22c Mon Sep 17 00:00:00 2001 From: Mahir Shah Date: Fri, 30 Oct 2026 18:36:44 +0000 Subject: [PATCH 10/14] Add tests --- Sprint-1/implement/mean.test.js | 64 ++++++++++++++++++--------------- 1 file changed, 35 insertions(+), 29 deletions(-) diff --git a/Sprint-1/implement/mean.test.js b/Sprint-1/implement/mean.test.js index 847b88676..3cbf56a44 100644 --- a/Sprint-1/implement/mean.test.js +++ b/Sprint-1/implement/mean.test.js @@ -19,32 +19,38 @@ const calculateMean = require("./mean.js"); // Acceptance criteria: -// 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"); - -// Given an array with a single number -// When passed to calculateMean -// Then it should return that number - -// Given an array containing negative or decimal numbers -// When passed to calculateMean -// Then it should return the correct mean - -// Given an empty array -// When passed to calculateMean -// Then it should throw 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 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 -// Then it should throw Error("calculateMean requires an array of numbers") +test("given [1, 2, 6], returns 3", () => { + expect(calculateMean([1, 2, 6])).toEqual(3); +}); + +test("given array with single number, return that number", () => { + expect(calculateMean([5])).toEqual(5); +}); + +test("given an array with positive and negative numbers, return the correct mean", () => { + expect(calculateMean([5, 8, -2, 7, -9])).toBeCloseTo(1.8); +}); + +test("given an empty array, throw an error", () => { + expect(() => calculateMean([])).toThrow( + "calculateMean requires a non-empty array" + ); +}); + +test("given something that isn't an array, throw an error", () => { + expect(() => calculateMean("banana")).toThrow( + "calculateMean requires an array of numbers" + ); +}); + +test("given no argument, throw an error", () => { + expect(() => calculateMean()).toThrow( + "calculateMean requires an array of numbers" + ); +}); + +test("given an array with non-number values, throw an error", () => { + expect(() => calculateMean([1, 5, 9, "hello", 4])).toThrow( + "calculateMean requires an array of numbers" + ); +}); From 03f21b02915d9ffc3e4fefe41f39bcfef2b89cc0 Mon Sep 17 00:00:00 2001 From: Mahir Shah Date: Fri, 30 Oct 2026 18:48:17 +0000 Subject: [PATCH 11/14] Implement calculateMean function --- Sprint-1/implement/mean.js | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/Sprint-1/implement/mean.js b/Sprint-1/implement/mean.js index 36909ea1f..039c3507a 100644 --- a/Sprint-1/implement/mean.js +++ b/Sprint-1/implement/mean.js @@ -1,3 +1,29 @@ -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"); + } + + if ( + !list.every( + (currentValue) => + typeof currentValue === "number" && !Number.isNaN(currentValue) + ) + ) { + throw new Error("calculateMean requires an array of numbers"); + } + + let total = 0; + + for (number of list) { + total += number; + } + const mean = total / list.length; + + return mean; +} module.exports = calculateMean; From 4d19b025e423f7335cb3d21728927f58af5c81e8 Mon Sep 17 00:00:00 2001 From: Mahir Shah Date: Fri, 30 Oct 2026 19:09:56 +0000 Subject: [PATCH 12/14] Add tests --- Sprint-1/implement/describe-median.test.js | 28 ++++++++++++---------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/Sprint-1/implement/describe-median.test.js b/Sprint-1/implement/describe-median.test.js index e7d18a264..e12a9cf7c 100644 --- a/Sprint-1/implement/describe-median.test.js +++ b/Sprint-1/implement/describe-median.test.js @@ -17,16 +17,18 @@ const describeMedian = require("./describe-median.js"); // Acceptance criteria: -// Given an array of numbers -// 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"'); - -// 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('given [1, 2, 3], returns "The median is 2"', () => { + expect(describeMedian([1, 2, 3])).toEqual("The median is 2"); +}); + +test('given empty array, returns "Could not calculate a median: calculateMedian requires a non-empty array"', () => { + expect(describeMedian([])).toEqual( + "Could not calculate a median: calculateMedian requires a non-empty array" + ); +}); + +test('given something that isnt an array of numbers, returns "Could not calculate a median: calculateMedian requires an array of numbers"', () => { + expect(describeMedian("banana")).toEqual( + "Could not calculate a median: calculateMedian requires an array of numbers" + ); +}); From c3a4ffa67000a475cf4cc3a32cba6c86b85cc531 Mon Sep 17 00:00:00 2001 From: Mahir Shah Date: Fri, 30 Oct 2026 19:10:22 +0000 Subject: [PATCH 13/14] Implement describeMedian function --- Sprint-1/implement/describe-median.js | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/Sprint-1/implement/describe-median.js b/Sprint-1/implement/describe-median.js index 3381cfc0a..551c245f1 100644 --- a/Sprint-1/implement/describe-median.js +++ b/Sprint-1/implement/describe-median.js @@ -20,6 +20,18 @@ function calculateMedian(list) { } // Implement this function. See describe-median.test.js for the acceptance criteria. -function describeMedian(list) {} +function describeMedian(list) { + try { + return "The median is " + calculateMedian(list); + } catch { + if (list.length === 0) { + return "Could not calculate a median: calculateMedian requires a non-empty array"; + } + + if (!Array.isArray(list)) { + return "Could not calculate a median: calculateMedian requires an array of numbers"; + } + } +} module.exports = describeMedian; From 8133c48a77519cff160096bcaa0a6b137c2b05a6 Mon Sep 17 00:00:00 2001 From: Mahir Shah Date: Fri, 30 Oct 2026 19:13:40 +0000 Subject: [PATCH 14/14] Refactor includes function to use for...of loop --- Sprint-1/refactor/includes.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Sprint-1/refactor/includes.js b/Sprint-1/refactor/includes.js index 29dad81f0..916bcae13 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 (element of list) { if (element === target) { return true; }