From 91cb53cf281460010dc289d6b2cd058a693a52f0 Mon Sep 17 00:00:00 2001 From: Shirin Panahian Date: Thu, 5 Nov 2026 11:00:42 +0000 Subject: [PATCH 01/14] Fix calculateMedian function and pass tests --- Sprint-1/fix/median.js | 27 ++++++++++++++++++++++++--- package.json | 24 ++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 3 deletions(-) create mode 100644 package.json diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index 5c5b796e1..111028162 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -6,9 +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]; - return median; + if (!Array.isArray(list)) { + throw new Error("calculateMedian requires an array of numbers"); + } + + if (list.length === 0) { + throw new Error("calculateMedian requires a non-empty array"); + } + + for (const item of list) { + if (typeof item !== "number" || !Number.isFinite(item)) { + throw new Error("calculateMedian requires an array of numbers"); + } + } + + const listLength = list.length; + const copy = [...list]; + const middleIndex = Math.floor(listLength / 2); + copy.sort((a, b) => a - b); + if (listLength % 2 === 0) { + const median = (copy[middleIndex - 1] + copy[middleIndex]) / 2; + return median; + } else { + return copy[middleIndex]; + } } module.exports = calculateMedian; diff --git a/package.json b/package.json new file mode 100644 index 000000000..a142438be --- /dev/null +++ b/package.json @@ -0,0 +1,24 @@ +{ + "name": "module-data-groups", + "version": "1.0.0", + "description": "Like learning a musical instrument, programming requires daily practice.", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/shirinpanahian/Module-Data-Groups.git" + }, + "keywords": [], + "author": "", + "license": "ISC", + "type": "commonjs", + "bugs": { + "url": "https://github.com/shirinpanahian/Module-Data-Groups/issues" + }, + "homepage": "https://github.com/shirinpanahian/Module-Data-Groups#readme", + "devDependencies": { + "jest": "^30.5.2" + } +} From 84b3b381df9521196a21d05d0650e10cd502004a Mon Sep 17 00:00:00 2001 From: Shirin Panahian Date: Thu, 5 Nov 2026 12:04:45 +0000 Subject: [PATCH 02/14] Handle empty array in dedupe function --- Sprint-1/implement/dedupe.js | 17 ++++++++++++++++- Sprint-1/implement/dedupe.test.js | 13 ++++++++++--- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/Sprint-1/implement/dedupe.js b/Sprint-1/implement/dedupe.js index 781e8718a..49b1696f3 100644 --- a/Sprint-1/implement/dedupe.js +++ b/Sprint-1/implement/dedupe.js @@ -1 +1,16 @@ -function dedupe() {} +function dedupe(list) { + if (list.length === 0) { + return []; + } + const newList = []; + for (const item of list) { + if (newList.includes(item)) { + continue; + } else { + newList.push(item); + } + } + return newList; +} +console.log(dedupe(["a", "a", "a", "b", "b", "c"])); +module.exports = dedupe; diff --git a/Sprint-1/implement/dedupe.test.js b/Sprint-1/implement/dedupe.test.js index d7c8e3d8e..e8da3982b 100644 --- a/Sprint-1/implement/dedupe.test.js +++ b/Sprint-1/implement/dedupe.test.js @@ -4,7 +4,7 @@ Dedupe Array 📖 Dedupe means **deduplicate** -In this kata, you will need to deduplicate the elements of an array +In this Kata, you will need to deduplicate the elements of an array E.g. dedupe(['a','a','a','b','b','c']) returns ['a','b','c'] E.g. dedupe([5, 1, 1, 2, 3, 2, 5, 8]) returns [5, 1, 2, 3, 8] @@ -16,13 +16,20 @@ 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"); +describe("dedupe", () => { + it("given an empty array, it returns an empty array", () => { + expect(dedupe([])).toEqual([]); + }); + it("given an array, it returns an deduplicate array", () => { + expect(dedupe(["a", "a", "a", "b", "b", "c"])).toEqual(["a", "b", "c"]); + }); +}); // Given an array with no duplicates // When passed to the dedupe function // Then it should return a copy of the original array // 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. From ee07d793d00da7bb80a4423e4fed998c19ee3276 Mon Sep 17 00:00:00 2001 From: Shirin Panahian Date: Thu, 5 Nov 2026 12:16:19 +0000 Subject: [PATCH 03/14] code work for no duplicate array --- Sprint-1/implement/dedupe.js | 2 +- Sprint-1/implement/dedupe.test.js | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/Sprint-1/implement/dedupe.js b/Sprint-1/implement/dedupe.js index 49b1696f3..8c675c8f3 100644 --- a/Sprint-1/implement/dedupe.js +++ b/Sprint-1/implement/dedupe.js @@ -12,5 +12,5 @@ function dedupe(list) { } return newList; } -console.log(dedupe(["a", "a", "a", "b", "b", "c"])); +//console.log(dedupe([5, 1, 1, 2, 3, 2, 5, 8])); module.exports = dedupe; diff --git a/Sprint-1/implement/dedupe.test.js b/Sprint-1/implement/dedupe.test.js index e8da3982b..9ac51b1fd 100644 --- a/Sprint-1/implement/dedupe.test.js +++ b/Sprint-1/implement/dedupe.test.js @@ -23,6 +23,17 @@ describe("dedupe", () => { it("given an array, it returns an deduplicate array", () => { 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]); + }); + + // Given an array with no duplicates + // When passed to the dedupe function + // Then it should return a copy of the original array + it("Given an array with no duplicates, it returns a copy of the original array", () => { + expect(dedupe(["d", "s", "t", "c"])).toEqual(["d", "s", "t", "c"]); + expect(dedupe([5, 4, 3, 2, 1])).toEqual([5, 4, 3, 2, 1]); + expect(dedupe([7, 8, 9])).toEqual([7, 8, 9]); }); }); // Given an array with no duplicates From 0bbf1830b4bc034366df28885dadc77493c633df Mon Sep 17 00:00:00 2001 From: Shirin Panahian Date: Thu, 5 Nov 2026 12:26:35 +0000 Subject: [PATCH 04/14] "Handle arrays without duplicates" --- Sprint-1/implement/dedupe.js | 1 - Sprint-1/implement/dedupe.test.js | 23 +++++++++++++++-------- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/Sprint-1/implement/dedupe.js b/Sprint-1/implement/dedupe.js index 8c675c8f3..82e9f59ae 100644 --- a/Sprint-1/implement/dedupe.js +++ b/Sprint-1/implement/dedupe.js @@ -12,5 +12,4 @@ function dedupe(list) { } return newList; } -//console.log(dedupe([5, 1, 1, 2, 3, 2, 5, 8])); module.exports = dedupe; diff --git a/Sprint-1/implement/dedupe.test.js b/Sprint-1/implement/dedupe.test.js index 9ac51b1fd..f1b5fa37f 100644 --- a/Sprint-1/implement/dedupe.test.js +++ b/Sprint-1/implement/dedupe.test.js @@ -35,12 +35,19 @@ describe("dedupe", () => { expect(dedupe([5, 4, 3, 2, 1])).toEqual([5, 4, 3, 2, 1]); expect(dedupe([7, 8, 9])).toEqual([7, 8, 9]); }); -}); -// Given an array with no duplicates -// When passed to the dedupe function -// Then it should return a copy of the original array -// 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. + // 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. + it("Given an array without duplicates, it returns a copy of the original array", () => { + expect(dedupe(["d", "s", "s", "t", "c", "d", "s", "t", "c", "d"])).toEqual([ + "d", + "s", + "t", + "c", + ]); + expect(dedupe([5, 4, 5, 4, 2, 1, 5, 3, 4])).toEqual([5, 4, 2, 1, 3]); + expect(dedupe([7, 2, 7, 8, 9])).toEqual([7, 2, 8, 9]); + }); +}); From 81b6d82217640391652573cdbda95617cd037afb Mon Sep 17 00:00:00 2001 From: Shirin Panahian Date: Thu, 5 Nov 2026 12:28:53 +0000 Subject: [PATCH 05/14] complete dedupe function --- Sprint-1/implement/dedupe.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sprint-1/implement/dedupe.test.js b/Sprint-1/implement/dedupe.test.js index f1b5fa37f..20dcea6dd 100644 --- a/Sprint-1/implement/dedupe.test.js +++ b/Sprint-1/implement/dedupe.test.js @@ -40,7 +40,7 @@ describe("dedupe", () => { // 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. - it("Given an array without duplicates, it returns a copy of the original array", () => { + it("Given an array with duplicates, it removes duplicates while preserving the first occurrence", () => { expect(dedupe(["d", "s", "s", "t", "c", "d", "s", "t", "c", "d"])).toEqual([ "d", "s", From 0aeba8fc906bfe3107c2796dc190e05a504e91f4 Mon Sep 17 00:00:00 2001 From: Shirin Panahian Date: Thu, 5 Nov 2026 13:42:24 +0000 Subject: [PATCH 06/14] Test findMax with empty and single-element arrays --- Sprint-1/implement/max.js | 9 ++++++++- Sprint-1/implement/max.test.js | 13 ++++++++++++- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/Sprint-1/implement/max.js b/Sprint-1/implement/max.js index 6dd76378e..1cc2c14d4 100644 --- a/Sprint-1/implement/max.js +++ b/Sprint-1/implement/max.js @@ -1,4 +1,11 @@ function findMax(elements) { + const arrLength = elements.length; + if (arrLength === 0) { + return [-Infinity]; + } + if (arrLength === 1) { + return elements[0]; + } } - +//console.log(findMax([5])); module.exports = findMax; diff --git a/Sprint-1/implement/max.test.js b/Sprint-1/implement/max.test.js index ff3fbb5e5..4ef019729 100644 --- a/Sprint-1/implement/max.test.js +++ b/Sprint-1/implement/max.test.js @@ -16,7 +16,18 @@ 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("dedupe", () => { + 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 + it("given an array with one number,returns that number", () => { + expect(findMax([5])).toEqual(5); + }); +}); // Given an array with one number // When passed to the max function From 0f21d3dc6b8c0a52fc207341135ebb6ad2c89bcf Mon Sep 17 00:00:00 2001 From: Shirin Panahian Date: Fri, 6 Nov 2026 01:04:12 +0000 Subject: [PATCH 07/14] complete function and test --- Sprint-1/implement/max.js | 17 +++++++- Sprint-1/implement/max.test.js | 76 +++++++++++++++++++++++++--------- Sprint-1/implement/sum.js | 17 ++++++++ Sprint-1/implement/sum.test.js | 66 ++++++++++++++++++++++------- 4 files changed, 139 insertions(+), 37 deletions(-) diff --git a/Sprint-1/implement/max.js b/Sprint-1/implement/max.js index 1cc2c14d4..9dcec8883 100644 --- a/Sprint-1/implement/max.js +++ b/Sprint-1/implement/max.js @@ -1,11 +1,26 @@ function findMax(elements) { + if (!Array.isArray(elements)) { + throw new Error("findMax requires an array of numbers"); + } + for (const value of elements) { + if (typeof value !== "number") { + throw new Error("findMax requires an array of numbers"); + } + } + const arrLength = elements.length; + let maxNum = elements[0]; if (arrLength === 0) { return [-Infinity]; } if (arrLength === 1) { return elements[0]; } + for (const item of elements) { + if (maxNum < item) { + maxNum = item; + } + } + return maxNum; } -//console.log(findMax([5])); module.exports = findMax; diff --git a/Sprint-1/implement/max.test.js b/Sprint-1/implement/max.test.js index 4ef019729..2853b72ce 100644 --- a/Sprint-1/implement/max.test.js +++ b/Sprint-1/implement/max.test.js @@ -16,7 +16,7 @@ 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. -describe("dedupe", () => { +describe("findMax", () => { it("given an empty array,returns -Infinity", () => { expect(findMax([])).toEqual([-Infinity]); }); @@ -26,29 +26,65 @@ describe("dedupe", () => { // Then it should return that number it("given an array with one number,returns that number", () => { expect(findMax([5])).toEqual(5); + expect(findMax([20])).toEqual(20); }); -}); -// 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 both positive and negative numbers, return the largest number overall", () => { + expect(findMax([5, 8, 3, 0, -1, 2, 16, -8, -16])).toEqual(16); + expect(findMax([-10, -5, -2, -20, 3, -1])).toEqual(3); + expect(findMax([-100, 50, -30, 25, 0, 75, -5])).toEqual(75); + }); -// 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 + it("given an array with just negative numbers, return the closest one to zero", () => { + expect(findMax([-5, -8, -3, -76, -2, -16, -4])).toEqual(-2); + expect(findMax([-1, -2, -9, -100])).toEqual(-1); + }); -// 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 + it("given an array with decimal numbers, returns the largest decimal number", () => { + expect(findMax([1.5, 3.7, 2.4, 5.9, 4.2])).toEqual(5.9); + expect(findMax([0.1, 0.9, 0.5])).toEqual(0.9); + expect(findMax([-1.5, -3.2, -0.5])).toEqual(-0.5); + }); -// 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") + it("throws when given an array containing a value that isn't a number", () => { + expect(() => findMax(["banana"])).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("throws when given something that isn't an array", () => { + expect(() => findMax("hey")).toThrow( + new 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") + expect(() => findMax(42)).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") + expect(() => findMax()).toThrow( + new Error("findMax requires an array of numbers") + ); + + expect(() => findMax(null)).toThrow( + new Error("findMax requires an array of numbers") + ); + + expect(() => findMax({})).toThrow( + new Error("findMax requires an array of numbers") + ); + }); +}); diff --git a/Sprint-1/implement/sum.js b/Sprint-1/implement/sum.js index 9062aafe3..979a0401c 100644 --- a/Sprint-1/implement/sum.js +++ b/Sprint-1/implement/sum.js @@ -1,4 +1,21 @@ function sum(elements) { + if (!Array.isArray(elements)) { + throw new Error("sum requires an array of numbers"); + } + for (const item of elements) { + if (typeof item !== "number") { + throw new Error("sum requires an array of numbers"); + } + } + const listLength = elements.length; + let total = 0; + if (listLength === 0) { + return 0; + } + elements.forEach((item) => { + total = total + item; + }); + return Number(total.toFixed(2)); } module.exports = sum; diff --git a/Sprint-1/implement/sum.test.js b/Sprint-1/implement/sum.test.js index a288e0f0a..52b22f1d2 100644 --- a/Sprint-1/implement/sum.test.js +++ b/Sprint-1/implement/sum.test.js @@ -13,24 +13,58 @@ 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") +describe("sum", () => { + 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 + // Given an array with just one number + // When passed to the sum function + // Then it should return that number + it("Given an array one number, return that number", () => { + expect(sum([45])).toEqual(45); + }); -// Given an array containing negative numbers -// When passed to the sum function -// Then it should still return the correct total sum + // 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, return the correct total sum", () => { + expect(sum([-5, -6, -12])).toEqual(-23); + expect(sum([-22, 17, -9])).toEqual(-14); + expect(sum([-99, 0, -1])).toEqual(-100); + }); -// Given an array with decimal/float numbers -// When passed to the sum function -// Then it should 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 + it("Given an array with decimal/float numbers, return the correct total sum", () => { + expect(sum([1.25, 2.75, 7.12])).toEqual(11.12); + expect(sum([4.12, 7.36, -1.52])).toEqual(9.96); + }); -// 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 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("throws when given something that isn't an number", () => { + expect(() => sum(["a", "b", "c"])).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") + // 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("throws when given something that isn't an array", () => { + expect(() => sum("hey")).toThrow( + new Error("sum requires an array of numbers") + ); + + expect(() => sum(null)).toThrow( + new Error("sum requires an array of numbers") + ); + + expect(() => sum(42)).toThrow( + new Error("sum requires an array of numbers") + ); + }); +}); From 133a2592de10ccf7e9f6b03b9e4156f1fa5abfb8 Mon Sep 17 00:00:00 2001 From: Shirin Panahian Date: Fri, 6 Nov 2026 10:07:01 +0000 Subject: [PATCH 08/14] mean function and acceptance criteria --- Sprint-1/implement/mean.js | 11 +++++++++-- Sprint-1/implement/mean.test.js | 29 ++++++++++++++++++++--------- 2 files changed, 29 insertions(+), 11 deletions(-) diff --git a/Sprint-1/implement/mean.js b/Sprint-1/implement/mean.js index 36909ea1f..6f8a117e8 100644 --- a/Sprint-1/implement/mean.js +++ b/Sprint-1/implement/mean.js @@ -1,3 +1,10 @@ -function calculateMean(list) {} - +function calculateMean(list) { + let sum = 0; + let mean = 0; + list.forEach((item) => { + sum = sum + item; + }); + mean = sum / list.length; + return Number(mean.toFixed(2)); +} module.exports = calculateMean; diff --git a/Sprint-1/implement/mean.test.js b/Sprint-1/implement/mean.test.js index 847b88676..ca3d7ba1c 100644 --- a/Sprint-1/implement/mean.test.js +++ b/Sprint-1/implement/mean.test.js @@ -23,15 +23,26 @@ 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"); - -// 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 +describe("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 + it("given an array with a single number, returns that number", () => { + expect(calculateMean([5])).toEqual(5); + }); + // Given an array containing negative or decimal numbers + // When passed to calculateMean + // Then it should return the correct mean + it("given an array with negative or decimal numbers, returns the correct mean", () => { + expect(calculateMean([-2, -6, -76])).toEqual(-28); + expect(calculateMean([10, 0, -11])).toEqual(-0.33); + expect(calculateMean([6.87, 3.12, -11.25])).toEqual(-0.42); + }); +}); // Given an empty array // When passed to calculateMean From 0a9d415c61583719cae2e1d65c2e2e10ecef26d6 Mon Sep 17 00:00:00 2001 From: Shirin Panahian Date: Fri, 6 Nov 2026 10:26:52 +0000 Subject: [PATCH 09/14] code work for throw error --- Sprint-1/implement/mean.js | 10 +++++++ Sprint-1/implement/mean.test.js | 47 +++++++++++++++++++++++---------- 2 files changed, 43 insertions(+), 14 deletions(-) diff --git a/Sprint-1/implement/mean.js b/Sprint-1/implement/mean.js index 6f8a117e8..6be0b434a 100644 --- a/Sprint-1/implement/mean.js +++ b/Sprint-1/implement/mean.js @@ -1,4 +1,14 @@ function calculateMean(list) { + if (!Array.isArray(list)) { + throw new Error("calculateMean requires an array of numbers"); + } + if (list.length === 0) { + throw new Error("calculateMean requires a non-empty array"); + } + for (const element of list) { + if (typeof element !== "number") + throw new Error("calculateMean requires an array of numbers"); + } let sum = 0; let mean = 0; list.forEach((item) => { diff --git a/Sprint-1/implement/mean.test.js b/Sprint-1/implement/mean.test.js index ca3d7ba1c..823e78c5f 100644 --- a/Sprint-1/implement/mean.test.js +++ b/Sprint-1/implement/mean.test.js @@ -42,20 +42,39 @@ describe("mean", () => { expect(calculateMean([10, 0, -11])).toEqual(-0.33); expect(calculateMean([6.87, 3.12, -11.25])).toEqual(-0.42); }); -}); - -// 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 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 no argument at all -// 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("throws a value that isn't an array", () => { + expect(() => calculateMean(["banana"])).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 no argument at all + // When passed to calculateMean + // Then it should throw Error("calculateMean requires an array of numbers") + it("throws when given no argument at all", () => { + 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([1, "2", 3])).toThrow( + new Error("calculateMean requires an array of numbers") + ); + }); +}); From 1842096453e7301ebee293ae6d126022461fb41d Mon Sep 17 00:00:00 2001 From: Shirin Panahian Date: Fri, 6 Nov 2026 11:26:10 +0000 Subject: [PATCH 10/14] Add median description and error handling tests --- Sprint-1/implement/describe-median.js | 9 +++++- Sprint-1/implement/describe-median.test.js | 33 ++++++++++++++++++---- 2 files changed, 36 insertions(+), 6 deletions(-) diff --git a/Sprint-1/implement/describe-median.js b/Sprint-1/implement/describe-median.js index 3381cfc0a..c2a20efbf 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 result = `The median is ${calculateMedian(list)}`; + return result; + } 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..93a35f0aa 100644 --- a/Sprint-1/implement/describe-median.test.js +++ b/Sprint-1/implement/describe-median.test.js @@ -21,11 +21,34 @@ 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"'); - -// Given an empty array -// When passed to describeMedian -// Then it should return "Could not calculate a median: calculateMedian requires a non-empty array" +describe("describeMedian", () => { + it("Given array of positive numbers, it returns the correct message", () => { + expect(describeMedian([1, 2, 6])).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("returns an error message when given an 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" + it("returns an error message when given an empty array", () => { + expect(describeMedian("banana")).toEqual( + "Could not calculate a median: calculateMedian requires an array of numbers" + ); + expect(describeMedian([3, "5", 8])).toEqual( + "Could not calculate a median: calculateMedian requires an array of numbers" + ); + expect(describeMedian(42)).toEqual( + "Could not calculate a median: calculateMedian requires an array of numbers" + ); + }); +}); // Given something that isn't an array of numbers, e.g. "banana" // When passed to describeMedian From 8210bdd8c41a1c3931fa065c0807ab824fcd7062 Mon Sep 17 00:00:00 2001 From: Shirin Panahian Date: Fri, 6 Nov 2026 11:36:04 +0000 Subject: [PATCH 11/14] Refactor includes 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..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; } From bae20584ea1ef59220fd41d2af4648eb001acbe1 Mon Sep 17 00:00:00 2001 From: Shirin Panahian Date: Fri, 6 Nov 2026 11:45:34 +0000 Subject: [PATCH 12/14] delete file --- package.json | 24 ------------------------ 1 file changed, 24 deletions(-) delete mode 100644 package.json diff --git a/package.json b/package.json deleted file mode 100644 index a142438be..000000000 --- a/package.json +++ /dev/null @@ -1,24 +0,0 @@ -{ - "name": "module-data-groups", - "version": "1.0.0", - "description": "Like learning a musical instrument, programming requires daily practice.", - "main": "index.js", - "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" - }, - "repository": { - "type": "git", - "url": "git+https://github.com/shirinpanahian/Module-Data-Groups.git" - }, - "keywords": [], - "author": "", - "license": "ISC", - "type": "commonjs", - "bugs": { - "url": "https://github.com/shirinpanahian/Module-Data-Groups/issues" - }, - "homepage": "https://github.com/shirinpanahian/Module-Data-Groups#readme", - "devDependencies": { - "jest": "^30.5.2" - } -} From 68fe295befd53bc4d6b1fc8723a486726c48df48 Mon Sep 17 00:00:00 2001 From: Shirin Panahian Date: Fri, 6 Nov 2026 23:45:46 +0000 Subject: [PATCH 13/14] Improve number validation and floating-point tests --- Sprint-1/implement/dedupe.js | 3 +-- Sprint-1/implement/dedupe.test.js | 7 ++++++- Sprint-1/implement/max.js | 8 +++----- Sprint-1/implement/max.test.js | 14 +++++++++++++- Sprint-1/implement/mean.js | 17 ++++++++++++----- Sprint-1/implement/mean.test.js | 6 +++--- Sprint-1/implement/sum.js | 14 +++++++++----- 7 files changed, 47 insertions(+), 22 deletions(-) diff --git a/Sprint-1/implement/dedupe.js b/Sprint-1/implement/dedupe.js index 82e9f59ae..f4a04d8ba 100644 --- a/Sprint-1/implement/dedupe.js +++ b/Sprint-1/implement/dedupe.js @@ -6,9 +6,8 @@ function dedupe(list) { for (const item of list) { if (newList.includes(item)) { continue; - } else { - newList.push(item); } + newList.push(item); } return newList; } diff --git a/Sprint-1/implement/dedupe.test.js b/Sprint-1/implement/dedupe.test.js index 20dcea6dd..319646cc8 100644 --- a/Sprint-1/implement/dedupe.test.js +++ b/Sprint-1/implement/dedupe.test.js @@ -31,7 +31,12 @@ describe("dedupe", () => { // When passed to the dedupe function // Then it should return a copy of the original array it("Given an array with no duplicates, it returns a copy of the original array", () => { - expect(dedupe(["d", "s", "t", "c"])).toEqual(["d", "s", "t", "c"]); + const originalArray = ["d", "s", "t", "c"]; + const result = dedupe(originalArray); + + expect(result).toEqual(originalArray); + expect(result).not.toBe(originalArray); + expect(dedupe([5, 4, 3, 2, 1])).toEqual([5, 4, 3, 2, 1]); expect(dedupe([7, 8, 9])).toEqual([7, 8, 9]); }); diff --git a/Sprint-1/implement/max.js b/Sprint-1/implement/max.js index 9dcec8883..99b1e3e58 100644 --- a/Sprint-1/implement/max.js +++ b/Sprint-1/implement/max.js @@ -3,19 +3,17 @@ function findMax(elements) { throw new Error("findMax requires an array of numbers"); } for (const value of elements) { - if (typeof value !== "number") { + if (!Number.isFinite(value)) { throw new Error("findMax requires an array of numbers"); } } const arrLength = elements.length; - let maxNum = elements[0]; + let maxNum = -Infinity; if (arrLength === 0) { return [-Infinity]; } - if (arrLength === 1) { - return elements[0]; - } + for (const item of elements) { if (maxNum < item) { maxNum = item; diff --git a/Sprint-1/implement/max.test.js b/Sprint-1/implement/max.test.js index 2853b72ce..43264a7c4 100644 --- a/Sprint-1/implement/max.test.js +++ b/Sprint-1/implement/max.test.js @@ -18,7 +18,7 @@ const findMax = require("./max.js"); // Delete this test.todo and replace it with a test. describe("findMax", () => { it("given an empty array,returns -Infinity", () => { - expect(findMax([])).toEqual([-Infinity]); + expect(findMax([])).toEqual(-Infinity); }); // Given an array with one number @@ -62,6 +62,18 @@ describe("findMax", () => { expect(() => findMax(["banana"])).toThrow( new Error("findMax requires an array of numbers") ); + + expect(() => findMax([10, "hello", 20])).toThrow( + new Error("findMax requires an array of numbers") + ); + + expect(() => findMax([10, null, 20])).toThrow( + new Error("findMax requires an array of numbers") + ); + + expect(() => findMax([10, NaN, 20])).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 diff --git a/Sprint-1/implement/mean.js b/Sprint-1/implement/mean.js index 6be0b434a..e4dd19110 100644 --- a/Sprint-1/implement/mean.js +++ b/Sprint-1/implement/mean.js @@ -2,19 +2,26 @@ function calculateMean(list) { if (!Array.isArray(list)) { throw new Error("calculateMean requires an array of numbers"); } + if (list.length === 0) { throw new Error("calculateMean requires a non-empty array"); } + for (const element of list) { - if (typeof element !== "number") + if (!Number.isFinite(element)) { throw new Error("calculateMean requires an array of numbers"); + } } + let sum = 0; - let mean = 0; + list.forEach((item) => { - sum = sum + item; + sum += item; }); - mean = sum / list.length; - return Number(mean.toFixed(2)); + + const mean = sum / list.length; + + return mean; } + module.exports = calculateMean; diff --git a/Sprint-1/implement/mean.test.js b/Sprint-1/implement/mean.test.js index 823e78c5f..ad374c553 100644 --- a/Sprint-1/implement/mean.test.js +++ b/Sprint-1/implement/mean.test.js @@ -38,9 +38,9 @@ describe("mean", () => { // When passed to calculateMean // Then it should return the correct mean it("given an array with negative or decimal numbers, returns the correct mean", () => { - expect(calculateMean([-2, -6, -76])).toEqual(-28); - expect(calculateMean([10, 0, -11])).toEqual(-0.33); - expect(calculateMean([6.87, 3.12, -11.25])).toEqual(-0.42); + expect(calculateMean([-2, -6, -76])).toBeCloseTo(-28, 2); + expect(calculateMean([10, 0, -11])).toBeCloseTo(-0.33, 2); + expect(calculateMean([6.87, 3.12, -11.25])).toBeCloseTo(-0.42, 2); }); // Given an empty array diff --git a/Sprint-1/implement/sum.js b/Sprint-1/implement/sum.js index 979a0401c..af9741b65 100644 --- a/Sprint-1/implement/sum.js +++ b/Sprint-1/implement/sum.js @@ -2,19 +2,23 @@ function sum(elements) { if (!Array.isArray(elements)) { throw new Error("sum requires an array of numbers"); } + for (const item of elements) { - if (typeof item !== "number") { + if (!Number.isFinite(item)) { throw new Error("sum requires an array of numbers"); } } - const listLength = elements.length; - let total = 0; - if (listLength === 0) { + + if (elements.length === 0) { return 0; } + + let total = 0; + elements.forEach((item) => { - total = total + item; + total += item; }); + return Number(total.toFixed(2)); } From 92d12fe8395bdae6ca912b2ae1598687960ca661 Mon Sep 17 00:00:00 2001 From: Shirin Panahian Date: Fri, 6 Nov 2026 23:47:02 +0000 Subject: [PATCH 14/14] Improve code --- Sprint-1/fix/median.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index 111028162..4d7cf972b 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -15,7 +15,7 @@ function calculateMedian(list) { } for (const item of list) { - if (typeof item !== "number" || !Number.isFinite(item)) { + if (!Number.isFinite(item)) { throw new Error("calculateMedian requires an array of numbers"); } }