From d7a511f631c96820c7feebdf9eea66dff8184fd0 Mon Sep 17 00:00:00 2001 From: abmhts Date: Wed, 4 Nov 2026 16:50:42 +0000 Subject: [PATCH 1/7] Fix- median implementation --- Sprint-1/fix/median.js | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index 5c5b796e1..488a03b45 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -6,8 +6,27 @@ // or contains values that aren't numbers (the function is expected to throw - see the tests). function calculateMedian(list) { + 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"); + } + + for (const item of list) { + if (typeof item !== "number") { + throw new Error("calculateMedian requires an array of numbers"); + } + } + + const sortedList = [...list].sort((a, b) => a - b); const middleIndex = Math.floor(list.length / 2); - const median = list.splice(middleIndex, 1)[0]; + + if (sortedList.length % 2 === 0) { + return (sortedList[middleIndex - 1] + sortedList[middleIndex]) / 2; + } + const median = sortedList.splice(middleIndex, 1)[0]; + return median; } From afa77c2beec1a7a11758de4e49e45eaaad19058e Mon Sep 17 00:00:00 2001 From: abmhts Date: Wed, 4 Nov 2026 16:52:22 +0000 Subject: [PATCH 2/7] Fix- dedupe --- Sprint-1/implement/dedupe.js | 5 ++++- Sprint-1/implement/dedupe.test.js | 21 +++++++++++++++++++-- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/Sprint-1/implement/dedupe.js b/Sprint-1/implement/dedupe.js index 781e8718a..3fbff3e73 100644 --- a/Sprint-1/implement/dedupe.js +++ b/Sprint-1/implement/dedupe.js @@ -1 +1,4 @@ -function dedupe() {} +function dedupe(num) { + return [...new Set(num)]; +} +module.exports = dedupe; diff --git a/Sprint-1/implement/dedupe.test.js b/Sprint-1/implement/dedupe.test.js index d7c8e3d8e..e624807f8 100644 --- a/Sprint-1/implement/dedupe.test.js +++ b/Sprint-1/implement/dedupe.test.js @@ -1,4 +1,5 @@ const dedupe = require("./dedupe.js"); + /* Dedupe Array @@ -16,13 +17,29 @@ 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.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 list = [1, 2, 3]; + + const result = dedupe(list); + expect(result).toEqual([1, 2, 3]); + expect(result).not.toBe(list); +}); // 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 of strings or numbers, it removes duplicates while preserving the first occurrence", () => { + expect(dedupe(["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]); +}); From 12b2ecb578da9524f5d816e41ddfc99a267104db Mon Sep 17 00:00:00 2001 From: abmhts Date: Wed, 4 Nov 2026 16:54:40 +0000 Subject: [PATCH 3/7] Fix - describe median --- Sprint-1/implement/describe-median.js | 9 ++++++++- Sprint-1/implement/describe-median.test.js | 17 ++++++++++++++++- 2 files changed, 24 insertions(+), 2 deletions(-) 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; diff --git a/Sprint-1/implement/describe-median.test.js b/Sprint-1/implement/describe-median.test.js index e7d18a264..001bee838 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.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])).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" +test(`given an 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" + ); +}); // 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, e.g. "banana", 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 c1bfe1afc64936a9473a6cde816eb7a1cf7afe47 Mon Sep 17 00:00:00 2001 From: abmhts Date: Wed, 4 Nov 2026 16:55:36 +0000 Subject: [PATCH 4/7] Fix - max --- Sprint-1/implement/max.js | 19 +++++++++++++++++++ Sprint-1/implement/max.test.js | 30 ++++++++++++++++++++++++++++-- 2 files changed, 47 insertions(+), 2 deletions(-) diff --git a/Sprint-1/implement/max.js b/Sprint-1/implement/max.js index 6dd76378e..5849427ae 100644 --- a/Sprint-1/implement/max.js +++ b/Sprint-1/implement/max.js @@ -1,4 +1,23 @@ function findMax(elements) { + if (!Array.isArray(elements)) { + throw new Error("findMax requires an array of numbers"); + } + + for (const item of elements) { + if (typeof item !== "number") { + throw new Error("findMax requires an array of numbers"); + } + } + + let max = -Infinity; + + for (const item of elements) { + if (item > max) { + max = item; + } + } + + return max; } module.exports = findMax; diff --git a/Sprint-1/implement/max.test.js b/Sprint-1/implement/max.test.js index ff3fbb5e5..0b80ccaca 100644 --- a/Sprint-1/implement/max.test.js +++ b/Sprint-1/implement/max.test.js @@ -16,28 +16,54 @@ 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([7])).toBe(7); +}); // 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 arry with both positive and negative numbers, returns the largest number overall", () => { + expect(findMax([-30, 50, -10, -40])).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 an array with just negative numbers, returns the closest one to zero", () => { + expect(findMax([-30, -50, -10, -40])).toBe(-10); +}); // 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 number", () => { + expect(findMax([1.5, 2.6, 3.7, 4.8])).toBe(4.8); +}); // 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 an array containing a value that isn't a number, throws an error", () => { + expect(() => findMax(["hey", 10, "hi", 60, 10])).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 at all,such as "hey", 42 or no argument, 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 971139c8d58405bfe31c3df3c4d996371e0d016b Mon Sep 17 00:00:00 2001 From: abmhts Date: Wed, 4 Nov 2026 16:56:37 +0000 Subject: [PATCH 5/7] Fix - Calculate the mean --- Sprint-1/implement/mean.js | 22 ++++++++++++++++- Sprint-1/implement/mean.test.js | 44 ++++++++++++++++++++++++++++++--- 2 files changed, 62 insertions(+), 4 deletions(-) diff --git a/Sprint-1/implement/mean.js b/Sprint-1/implement/mean.js index 36909ea1f..831175075 100644 --- a/Sprint-1/implement/mean.js +++ b/Sprint-1/implement/mean.js @@ -1,3 +1,23 @@ -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"); + } + + let sum = 0; + + for (let i = 0; i < list.length; i++) { + if (typeof list[i] !== "number") { + throw new Error("calculateMean requires an array of numbers"); + } + + sum += list[i]; + } + + return sum / list.length; +} module.exports = calculateMean; diff --git a/Sprint-1/implement/mean.test.js b/Sprint-1/implement/mean.test.js index 847b88676..5a2d611ef 100644 --- a/Sprint-1/implement/mean.test.js +++ b/Sprint-1/implement/mean.test.js @@ -23,28 +23,66 @@ 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])).toEqual(3); +}); // Given an array with a single number // When passed to calculateMean // Then it should return that number +test("given an array with a single number, returns that number", () => { + expect(calculateMean([1])).toEqual(1); +}); // Given an array containing negative or decimal numbers // When passed to calculateMean // Then it should return the correct mean +test("given an array containing negative or decimal numbers, returns the correct mean", () => { + expect(calculateMean([10, 2.2, 3.8, -6])).toEqual(2.5); +}); // 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 a value that isn't an array, e.g. "banana", 42, null or {}, 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 at all, 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 value, e.g. [1, "2", 3], throws an error`, () => { + expect(() => calculateMean([1, "2", 3])).toThrow( + "calculateMean requires an array of numbers" + ); +}); From a0847a2e7c158a9454ba435ec7eb7487d850f282 Mon Sep 17 00:00:00 2001 From: abmhts Date: Wed, 4 Nov 2026 16:57:31 +0000 Subject: [PATCH 6/7] Fix - Sum the numbers --- Sprint-1/implement/sum.js | 14 ++++++++++++++ Sprint-1/implement/sum.test.js | 28 +++++++++++++++++++++++----- 2 files changed, 37 insertions(+), 5 deletions(-) diff --git a/Sprint-1/implement/sum.js b/Sprint-1/implement/sum.js index 9062aafe3..f6891adfc 100644 --- a/Sprint-1/implement/sum.js +++ b/Sprint-1/implement/sum.js @@ -1,4 +1,18 @@ function sum(elements) { + if (!Array.isArray(elements)) { + throw new Error("sum requires an array of numbers"); + } + + let total = 0; + + for (let i = 0; i < elements.length; i++) { + if (typeof elements[i] !== "number") { + throw new Error("sum requires an array of numbers"); + } + + total += elements[i]; + } + return total; } module.exports = sum; diff --git a/Sprint-1/implement/sum.test.js b/Sprint-1/implement/sum.test.js index a288e0f0a..af1c0766a 100644 --- a/Sprint-1/implement/sum.test.js +++ b/Sprint-1/implement/sum.test.js @@ -13,24 +13,42 @@ 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([])).toEqual(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 just one number, returns that number", () => { + expect(sum([10])).toEqual(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 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 - +test("given an array with decimal/float numbers, returns the correct total sum", () => { + expect(sum([10, 20.5, 30])).toEqual(60.5); +}); // 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 value that isn't a number", () => { + expect(() => sum(["hey", 10, "hi", 60, 10])).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 at all, such as "hey", 42 or no argument`, () => { + 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 f5c0c0c80b7dd7111116dc62ee9855d312e0caf7 Mon Sep 17 00:00:00 2001 From: abmhts Date: Wed, 4 Nov 2026 16:58:20 +0000 Subject: [PATCH 7/7] Refactor - includes --- 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..8c9ae2e66 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 element of list) { if (element === target) { return true; }