From 953a7acde601b540b38e722bac9a699b848e77df Mon Sep 17 00:00:00 2001 From: Diana <40689028+ausiejute@users.noreply.github.com> Date: Sun, 1 Nov 2026 18:48:06 +0000 Subject: [PATCH 01/13] Write required tests --- Sprint-1/fix/median.js | 12 ++++++++++-- Sprint-1/implement/max.test.js | 34 ++++++++++++++++++++++++++++++++-- 2 files changed, 42 insertions(+), 4 deletions(-) diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index 5c5b796e1..f97d2e8ac 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -6,8 +6,16 @@ // 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]; + list.forEach((item) => { + if (typeof item !== "number") + throw new Error("input should be a number value"); + }); + + const sortedList = list.slice().sort((a, b) => a - b); + + // implement median part + const middleIndex = Math.floor(sortedList.length / 2); + const median = sortedList[middleIndex]; return median; } diff --git a/Sprint-1/implement/max.test.js b/Sprint-1/implement/max.test.js index ff3fbb5e5..c727883fa 100644 --- a/Sprint-1/implement/max.test.js +++ b/Sprint-1/implement/max.test.js @@ -15,29 +15,59 @@ 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("should return -Infinity if given an empty array", () => { + expect(findMax([]).toEqual(-Infinity)); +}); // Given an array with one number // When passed to the max function // Then it should return that number +test("should return that number if given an array with one number", () => { + expect(findMax([2]).toEqual(2)); +}); + // Given an array with both positive and negative numbers // When passed to the max function // Then it should return the largest number overall +test("should return the largest number overall if given positive & negative numbers", () => { + expect(findMax([-2, 4, -7, 9]).toEqual(9)); +}); + // Given an array with just negative numbers // When passed to the max function // Then it should return the closest one to zero +test("should return the one closest to zero if given only negative numbers", () => { + expect(findMax([-2, -4, -7, -9]).toEqual(-2)); +}); + // Given an array with decimal numbers // When passed to the max function // Then it should return the largest decimal number +test("should return the largest decimal number when only decimals were given", () => { + expect(findMax([-2.5, 4.1, 3.2, 9.3]).toEqual(9.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") +test("should throw an error when at least one value is not a number", () => { + expect(() => findMax([-2, "9", "sweet", 9])).toThrow( + new Error("findMax requires for all items to be 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("should throw an error when the data type isn't an array", () => { + expect(() => findMax(["ten", 1, "thirty"])).toThrow( + new Error("findMax requires for input to be a list of items") + ); +}); From 646befa539c9db64eec5d5a8d09850fef686ad49 Mon Sep 17 00:00:00 2001 From: Diana <40689028+ausiejute@users.noreply.github.com> Date: Mon, 2 Nov 2026 00:17:22 +0000 Subject: [PATCH 02/13] Implement the function & correct some test cases --- Sprint-1/implement/max.js | 13 +++++++++++++ Sprint-1/implement/max.test.js | 12 ++++++------ 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/Sprint-1/implement/max.js b/Sprint-1/implement/max.js index 6dd76378e..16ec000dc 100644 --- a/Sprint-1/implement/max.js +++ b/Sprint-1/implement/max.js @@ -1,4 +1,17 @@ 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((element) => typeof element === "number")) { + throw new Error("findMax requires for all items to be numbers"); + } + + return Math.max(...elements); } module.exports = findMax; diff --git a/Sprint-1/implement/max.test.js b/Sprint-1/implement/max.test.js index c727883fa..bb9e39a0e 100644 --- a/Sprint-1/implement/max.test.js +++ b/Sprint-1/implement/max.test.js @@ -17,7 +17,7 @@ const findMax = require("./max.js"); // Then it should return -Infinity test("should return -Infinity if given an empty array", () => { - expect(findMax([]).toEqual(-Infinity)); + expect(findMax([])).toEqual(-Infinity); }); // Given an array with one number @@ -25,7 +25,7 @@ test("should return -Infinity if given an empty array", () => { // Then it should return that number test("should return that number if given an array with one number", () => { - expect(findMax([2]).toEqual(2)); + expect(findMax([2])).toEqual(2); }); // Given an array with both positive and negative numbers @@ -33,7 +33,7 @@ test("should return that number if given an array with one number", () => { // Then it should return the largest number overall test("should return the largest number overall if given positive & negative numbers", () => { - expect(findMax([-2, 4, -7, 9]).toEqual(9)); + expect(findMax([-2, 4, -7, 9])).toEqual(9); }); // Given an array with just negative numbers @@ -41,7 +41,7 @@ test("should return the largest number overall if given positive & negative numb // Then it should return the closest one to zero test("should return the one closest to zero if given only negative numbers", () => { - expect(findMax([-2, -4, -7, -9]).toEqual(-2)); + expect(findMax([-2, -4, -7, -9])).toEqual(-2); }); // Given an array with decimal numbers @@ -49,7 +49,7 @@ test("should return the one closest to zero if given only negative numbers", () // Then it should return the largest decimal number test("should return the largest decimal number when only decimals were given", () => { - expect(findMax([-2.5, 4.1, 3.2, 9.3]).toEqual(9.3)); + expect(findMax([-2.5, 4.1, 3.2, 9.3])).toEqual(9.3); }); // Given an array containing a value that isn't a number @@ -68,6 +68,6 @@ test("should throw an error when at least one value is not a number", () => { test("should throw an error when the data type isn't an array", () => { expect(() => findMax(["ten", 1, "thirty"])).toThrow( - new Error("findMax requires for input to be a list of items") + new Error("findMax requires for all items to be numbers") ); }); From 755f69f9cd85949fb316ef647be853fa4ed0a611 Mon Sep 17 00:00:00 2001 From: Diana <40689028+ausiejute@users.noreply.github.com> Date: Mon, 2 Nov 2026 14:21:45 +0000 Subject: [PATCH 03/13] fix the implementation of calculateMedian --- Sprint-1/fix/median.js | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index f97d2e8ac..6357c63ac 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -6,16 +6,22 @@ // or contains values that aren't numbers (the function is expected to throw - see the tests). function calculateMedian(list) { - list.forEach((item) => { - if (typeof item !== "number") - throw new Error("input should be a number value"); - }); + if (!Array.isArray(list) || !list.every((item) => typeof item === "number")) { + throw new Error("calculateMedian requires an array of numbers"); + } + + if (list.length === 0) { + throw new Error("calculateMedian requires a non-empty array"); + } const sortedList = list.slice().sort((a, b) => a - b); - // implement median part - const middleIndex = Math.floor(sortedList.length / 2); - const median = sortedList[middleIndex]; + const mid = Math.floor(sortedList.length / 2); + const median = + sortedList.length % 2 !== 0 + ? sortedList[mid] + : (sortedList[mid - 1] + sortedList[mid]) / 2; + return median; } From 391d0e5de17feecee2878d1ac1a5cb0f5369c94a Mon Sep 17 00:00:00 2001 From: Diana <40689028+ausiejute@users.noreply.github.com> Date: Tue, 3 Nov 2026 00:36:55 +0000 Subject: [PATCH 04/13] Fix the calculateMedian implementation --- Sprint-1/fix/median.js | 1 + 1 file changed, 1 insertion(+) diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index 6357c63ac..2756596b4 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -9,6 +9,7 @@ function calculateMedian(list) { if (!Array.isArray(list) || !list.every((item) => typeof item === "number")) { throw new Error("calculateMedian requires an array of numbers"); } + // test test test if (list.length === 0) { throw new Error("calculateMedian requires a non-empty array"); From a2b4428e883ffdeddd3ea884960aa576412f9316 Mon Sep 17 00:00:00 2001 From: Diana <40689028+ausiejute@users.noreply.github.com> Date: Tue, 3 Nov 2026 00:38:12 +0000 Subject: [PATCH 05/13] Write the tests and implementation for Sum --- Sprint-1/implement/sum.js | 16 ++++++++++++++++ Sprint-1/implement/sum.test.js | 23 ++++++++++++++++++++++- 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/Sprint-1/implement/sum.js b/Sprint-1/implement/sum.js index 9062aafe3..f404246c2 100644 --- a/Sprint-1/implement/sum.js +++ b/Sprint-1/implement/sum.js @@ -1,4 +1,20 @@ function sum(elements) { + if ( + !Array.isArray(elements) || + !elements.every((item) => typeof item === "number") + ) { + throw new Error("sum requires an array of numbers"); + } + + if (elements.length === 0) { + return 0; + } + + let total = 0; + for (let i = 0; i < elements.length; i++) { + 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..de5238d7c 100644 --- a/Sprint-1/implement/sum.test.js +++ b/Sprint-1/implement/sum.test.js @@ -13,24 +13,45 @@ 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("should return the same number when given one number", () => { + expect(sum([3])).toEqual(3); +}); // Given an array containing negative numbers // When passed to the sum function // Then it should still return the correct total sum +test("should return correct sum when given negative numbers", () => { + expect(sum([5, -4, 1, 9])).toEqual(11); +}); // Given an array with decimal/float numbers // When passed to the sum function // Then it should return the correct total sum +test("should return a correct total sum when given decimal/float numbers", () => { + expect(sum([2.2, 1, 4.1])).toEqual(7.3); +}); // 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("should throw an error when passing non-number values", () => { + expect(() => sum([8, -3, ""])).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") +test("should throw an error when passing non-array data types", () => { + expect(() => sum("three", {})).toThrow( + new Error("sum requires an array of numbers") + ); +}); From 6d3d23f38116d1bf5362038d974cf99dc299f709 Mon Sep 17 00:00:00 2001 From: Diana <40689028+ausiejute@users.noreply.github.com> Date: Tue, 3 Nov 2026 01:07:19 +0000 Subject: [PATCH 06/13] Write required tests for Dedupe --- Sprint-1/fix/median.js | 1 - Sprint-1/implement/dedupe.test.js | 17 +++++++++++++++-- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index 2756596b4..6357c63ac 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -9,7 +9,6 @@ function calculateMedian(list) { if (!Array.isArray(list) || !list.every((item) => typeof item === "number")) { throw new Error("calculateMedian requires an array of numbers"); } - // test test test if (list.length === 0) { throw new Error("calculateMedian requires a non-empty array"); diff --git a/Sprint-1/implement/dedupe.test.js b/Sprint-1/implement/dedupe.test.js index d7c8e3d8e..655b8a5e5 100644 --- a/Sprint-1/implement/dedupe.test.js +++ b/Sprint-1/implement/dedupe.test.js @@ -16,13 +16,26 @@ 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("should return a copy of the original when the given array has no duplicates", () => { + expect(dedupe([1, 3, 8])).toEqual([1, 3, 8]); +}); // 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("should return a new array with duplicates removed and 1st occurence of each element preserved", () => { + expect(dedupe(["Mon", "wed", "wed", "Sun", "Tue", "Sun"])).toEqual([ + "Mon", + "Wed", + "Sun", + "Tue", + ]); +}); From 2f157319c7aa1b1b5046556d832ea81c3bb661d6 Mon Sep 17 00:00:00 2001 From: Diana <40689028+ausiejute@users.noreply.github.com> Date: Tue, 3 Nov 2026 03:01:35 +0000 Subject: [PATCH 07/13] Write the implementation for dedublicate function --- Sprint-1/implement/dedupe.js | 15 ++++++++++++++- Sprint-1/implement/dedupe.test.js | 2 +- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/Sprint-1/implement/dedupe.js b/Sprint-1/implement/dedupe.js index 781e8718a..0c72150e7 100644 --- a/Sprint-1/implement/dedupe.js +++ b/Sprint-1/implement/dedupe.js @@ -1 +1,14 @@ -function dedupe() {} +function dedupe(items) { + if (items.length === 0) { + return []; + } + + let singleOccur = []; + items.forEach((i) => { + if (!singleOccur.includes(i)) { + singleOccur.push(i); + } + }); + return singleOccur; +} +module.exports = dedupe; diff --git a/Sprint-1/implement/dedupe.test.js b/Sprint-1/implement/dedupe.test.js index 655b8a5e5..0e80143e8 100644 --- a/Sprint-1/implement/dedupe.test.js +++ b/Sprint-1/implement/dedupe.test.js @@ -32,7 +32,7 @@ test("should return a copy of the original when the given array has no duplicate // Then it should return a new array with duplicates removed while preserving the // first occurrence of each element from the original array. test("should return a new array with duplicates removed and 1st occurence of each element preserved", () => { - expect(dedupe(["Mon", "wed", "wed", "Sun", "Tue", "Sun"])).toEqual([ + expect(dedupe(["Mon", "Wed", "Wed", "Sun", "Tue", "Sun"])).toEqual([ "Mon", "Wed", "Sun", From 7c2c9cd1605973202a6d89cda39a089af69c9e86 Mon Sep 17 00:00:00 2001 From: Diana <40689028+ausiejute@users.noreply.github.com> Date: Tue, 3 Nov 2026 12:34:53 +0000 Subject: [PATCH 08/13] Write required describe-median tests --- Sprint-1/implement/describe-median.test.js | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/Sprint-1/implement/describe-median.test.js b/Sprint-1/implement/describe-median.test.js index e7d18a264..58ebb43d4 100644 --- a/Sprint-1/implement/describe-median.test.js +++ b/Sprint-1/implement/describe-median.test.js @@ -21,12 +21,28 @@ 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"'); +describe("calculateMedian", () => { + test('returns "The median is " followed by the median', () => { + expect(calculateMedian([3, 1, 4, 1, 5])).toBe(3); + }); +}); // Given an empty array // When passed to describeMedian // Then it should return "Could not calculate a median: calculateMedian requires a non-empty array" +test("returns error message when given an empty array", () => { + const result = calculateMedian([]); + expect(result.error).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("returns error message when given a non-array data", () => { + const result = calculateMedian("five seven nine"); + expect(result.error).toBe( + "Could not calculate a median: calculateMedian requires an array of numbers" + ); +}); From 2c6d2d0b8fbcc1b8efb2fc3837c4bb89834a085a Mon Sep 17 00:00:00 2001 From: Diana <40689028+ausiejute@users.noreply.github.com> Date: Tue, 3 Nov 2026 12:54:22 +0000 Subject: [PATCH 09/13] Implement describe-median 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..ebdf1cfe9 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 (err) { + return err.message; + } +} module.exports = describeMedian; From eef2d6bcbfc17d85f07266ad643dafb92f56a431 Mon Sep 17 00:00:00 2001 From: Diana <40689028+ausiejute@users.noreply.github.com> Date: Tue, 3 Nov 2026 13:06:46 +0000 Subject: [PATCH 10/13] Fix naming and final checkups --- Sprint-1/implement/describe-median.js | 2 +- Sprint-1/implement/describe-median.test.js | 14 +++++++------- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/Sprint-1/implement/describe-median.js b/Sprint-1/implement/describe-median.js index ebdf1cfe9..488bffa1c 100644 --- a/Sprint-1/implement/describe-median.js +++ b/Sprint-1/implement/describe-median.js @@ -25,7 +25,7 @@ function describeMedian(list) { const median = calculateMedian(list); return `The median is ${median}`; } catch (err) { - return err.message; + return `Could not calculate a median: ${err.message}`; } } diff --git a/Sprint-1/implement/describe-median.test.js b/Sprint-1/implement/describe-median.test.js index 58ebb43d4..7225f1b38 100644 --- a/Sprint-1/implement/describe-median.test.js +++ b/Sprint-1/implement/describe-median.test.js @@ -21,9 +21,9 @@ 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. -describe("calculateMedian", () => { +describe("describeMedian", () => { test('returns "The median is " followed by the median', () => { - expect(calculateMedian([3, 1, 4, 1, 5])).toBe(3); + expect(describeMedian([3, 1, 4, 1, 5])).toBe("The median is 3"); }); }); @@ -31,8 +31,8 @@ describe("calculateMedian", () => { // When passed to describeMedian // Then it should return "Could not calculate a median: calculateMedian requires a non-empty array" test("returns error message when given an empty array", () => { - const result = calculateMedian([]); - expect(result.error).toBe( + const result = describeMedian([]); + expect(result).toBe( "Could not calculate a median: calculateMedian requires a non-empty array" ); }); @@ -40,9 +40,9 @@ test("returns error message when given an 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("returns error message when given a non-array data", () => { - const result = calculateMedian("five seven nine"); - expect(result.error).toBe( +test("returns error message when given a non-array", () => { + const result = describeMedian("five seven nine"); + expect(result).toBe( "Could not calculate a median: calculateMedian requires an array of numbers" ); }); From bea2f8dc81afcf51d195816a74cd8bac08109155 Mon Sep 17 00:00:00 2001 From: Diana <40689028+ausiejute@users.noreply.github.com> Date: Tue, 3 Nov 2026 17:56:42 +0000 Subject: [PATCH 11/13] Write required tests and implementation of calculateMean --- Sprint-1/implement/mean.js | 15 ++++++++++++++- Sprint-1/implement/mean.test.js | 32 +++++++++++++++++++++++++++++++- 2 files changed, 45 insertions(+), 2 deletions(-) diff --git a/Sprint-1/implement/mean.js b/Sprint-1/implement/mean.js index 36909ea1f..979f588df 100644 --- a/Sprint-1/implement/mean.js +++ b/Sprint-1/implement/mean.js @@ -1,3 +1,16 @@ -function calculateMean(list) {} +function calculateMean(list) { + if (list.length === 0) { + throw new Error("calculateMean requires a non-empty array"); + } + + if (!Array.isArray(list) || !list.every((item) => typeof item === "number")) { + throw new Error("calculateMean requires an array of numbers"); + } + + let sum = list.reduce((a, b) => a + b, 0); + + let meanResult = sum / list.length; + return meanResult; +} module.exports = calculateMean; diff --git a/Sprint-1/implement/mean.test.js b/Sprint-1/implement/mean.test.js index 847b88676..e06bf8375 100644 --- a/Sprint-1/implement/mean.test.js +++ b/Sprint-1/implement/mean.test.js @@ -23,28 +23,58 @@ 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"); +describe("calculatemean", () => { + it("returns the mean for [1, 2, 6]", () => { + expect(calculateMean([])).toEqual(); + }); +}); // Given an array with a single number // When passed to calculateMean // Then it should return that number +it("returns the mean for [1, 2, 6]", () => { + expect(calculateMean([1, 2, 6])).toEqual(3); +}); // Given an array containing negative or decimal numbers // When passed to calculateMean // Then it should return the correct mean +it("returns the mean for [-1, 2.5, 6]", () => { + expect(calculateMean([])).toEqual(2.5); +}); // Given an empty array // When passed to calculateMean // Then it should throw Error("calculateMean requires a non-empty array") +it("throws when given an empty array", () => { + 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("throws when given a non-array", () => { + expect(() => calculateMean("luck")).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") +it("throws when not given any argument", () => { + 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") +it("throws when given an array containing a non-number value", () => { + expect(() => calculateMean(["two", five, "8"])).toThrow( + new Error("calculateMean requires an array of numbers") + ); +}); From fdb26e8f56908d94a969a5d9eebb8cd278cf0e57 Mon Sep 17 00:00:00 2001 From: Diana <40689028+ausiejute@users.noreply.github.com> Date: Tue, 3 Nov 2026 23:20:43 +0000 Subject: [PATCH 12/13] Fix and adjust implementation and tests of calculateMean --- .gitignore | 2 ++ Sprint-1/implement/mean.js | 8 ++++++-- Sprint-1/implement/mean.test.js | 12 ++++++------ 3 files changed, 14 insertions(+), 8 deletions(-) diff --git a/.gitignore b/.gitignore index 8ee70353f..e1ce8e68a 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,5 @@ node_modules **/.DS_Store .idea package-lock.json +mean.js +mean.test.js diff --git a/Sprint-1/implement/mean.js b/Sprint-1/implement/mean.js index 979f588df..e89828027 100644 --- a/Sprint-1/implement/mean.js +++ b/Sprint-1/implement/mean.js @@ -1,9 +1,13 @@ function calculateMean(list) { - if (list.length === 0) { + 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 (!Array.isArray(list) || !list.every((item) => typeof item === "number")) { + if (!list.every((item) => typeof item === "number")) { throw new Error("calculateMean requires an array of numbers"); } diff --git a/Sprint-1/implement/mean.test.js b/Sprint-1/implement/mean.test.js index e06bf8375..8e9b24f73 100644 --- a/Sprint-1/implement/mean.test.js +++ b/Sprint-1/implement/mean.test.js @@ -25,22 +25,22 @@ const calculateMean = require("./mean.js"); // Delete this test.todo and replace it with a test. describe("calculatemean", () => { it("returns the mean for [1, 2, 6]", () => { - expect(calculateMean([])).toEqual(); + 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("returns the mean for [1, 2, 6]", () => { - expect(calculateMean([1, 2, 6])).toEqual(3); +it("returns the mean for 3", () => { + expect(calculateMean([3])).toEqual(3); }); // Given an array containing negative or decimal numbers // When passed to calculateMean // Then it should return the correct mean it("returns the mean for [-1, 2.5, 6]", () => { - expect(calculateMean([])).toEqual(2.5); + expect(calculateMean([-1, 2.5, 6])).toEqual(2.5); }); // Given an empty array @@ -65,7 +65,7 @@ it("throws when given a non-array", () => { // When passed to calculateMean // Then it should throw Error("calculateMean requires an array of numbers") it("throws when not given any argument", () => { - expect(() => calculateMean([])).toThrow( + expect(() => calculateMean()).toThrow( new Error("calculateMean requires an array of numbers") ); }); @@ -74,7 +74,7 @@ it("throws when not given any argument", () => { // When passed to calculateMean // Then it should throw Error("calculateMean requires an array of numbers") it("throws when given an array containing a non-number value", () => { - expect(() => calculateMean(["two", five, "8"])).toThrow( + expect(() => calculateMean(["two", 5, "8"])).toThrow( new Error("calculateMean requires an array of numbers") ); }); From bdf37ad5e1d0757e616ded341bfcc65b40e4125a Mon Sep 17 00:00:00 2001 From: Diana <40689028+ausiejute@users.noreply.github.com> Date: Wed, 4 Nov 2026 01:11:24 +0000 Subject: [PATCH 13/13] Refactor the implementation of Includes function --- 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; }