From 48ce8d78c4ef237928a435ef891ae068cbe45e68 Mon Sep 17 00:00:00 2001 From: gaireprakash20-ops Date: Thu, 5 Nov 2026 10:18:44 +0000 Subject: [PATCH 01/12] pass all the tests for calculateMedian function --- Sprint-1/fix/median.js | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index 5c5b796e1..3adc95936 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -6,9 +6,23 @@ // 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 middleIndex = Math.floor(list.length / 2); - const median = list.splice(middleIndex, 1)[0]; - return median; + const sorted = [...list].sort((a, b) => a - b); + if (sorted.length % 2 === 0) { + return (sorted[middleIndex - 1] + sorted[middleIndex]) / 2; + } + return sorted[middleIndex]; } module.exports = calculateMedian; From 2b6e82254d0aa68fd92fad4b38be50399cae70bf Mon Sep 17 00:00:00 2001 From: gaireprakash20-ops Date: Thu, 5 Nov 2026 11:02:57 +0000 Subject: [PATCH 02/12] make the function throw an error if the input is not an array of numbers --- Sprint-1/implement/max.js | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/Sprint-1/implement/max.js b/Sprint-1/implement/max.js index 6dd76378e..a061a69fa 100644 --- a/Sprint-1/implement/max.js +++ b/Sprint-1/implement/max.js @@ -1,4 +1,15 @@ function findMax(elements) { + if (!Array.isArray(elements)) { + throw new Error("findMax requires an array of numbers"); + } + if (!elements.every((v) => typeof v === "number" && !Number.isNaN(v))) { + throw new Error("findMax requires an array of numbers"); + } + let max = -Infinity; + for (const value of elements) { + if (value > max) max = value; + } + return max; } module.exports = findMax; From 8ccb8b0fedcc687232bc766d3091ffb6a966e24b Mon Sep 17 00:00:00 2001 From: gaireprakash20-ops Date: Thu, 5 Nov 2026 11:03:24 +0000 Subject: [PATCH 03/12] create the test edges and all the test case passed --- Sprint-1/implement/max.test.js | 82 +++++++++++++++++++++++----------- 1 file changed, 57 insertions(+), 25 deletions(-) diff --git a/Sprint-1/implement/max.test.js b/Sprint-1/implement/max.test.js index ff3fbb5e5..f525f2ba5 100644 --- a/Sprint-1/implement/max.test.js +++ b/Sprint-1/implement/max.test.js @@ -11,33 +11,65 @@ We have set things up already so that this file can see your function from the o */ const findMax = require("./max.js"); +describe("findMax", () => { + // 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. + it("returns -Infinity for []", () => { + expect(findMax([])).toEqual(-Infinity); + }); -// 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 + it("returns the only number for [1]", () => { + expect(findMax([1])).toEqual(1); + }); -// 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("returns the largest of positive and negative numbers", () => { + expect(findMax([5, -7, 3, -5])).toEqual(5); + }); -// 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("returns the negative number closest to zero", () => { + expect(findMax([-10, -3, -20, -5])).toEqual(-3); + }); -// 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("returns the largest decimal number", () => { + expect(findMax([1.5, 2.25, 2.2, -0.5])).toEqual(2.25); + }); + // 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 the array contains a non-number", () => { + expect(() => findMax(["hey", 10, "hi", 60, 10])).toThrow( + new Error("findMax requires an array of numbers") + ); + }); + // Given something that isn't an array at all, such as "hey", 42 or no argument + // When passed to the max function + // Then it should throw Error("findMax requires an array of numbers") + it.each([["hey"], [42], [null], [{}]])( + "throws when given %p instead of an array", + (input) => { + expect(() => findMax(input)).toThrow( + new Error("findMax requires an array of numbers") + ); + } + ); -// Given an array with decimal numbers -// When passed to the max function -// Then it should return the largest decimal number - -// 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") + it("throws when called with no argument", () => { + expect(() => findMax()).toThrow( + new Error("findMax requires an array of numbers") + ); + }); +}); From 381ec0d53f9162246822c1b7937313e61676e44b Mon Sep 17 00:00:00 2001 From: gaireprakash20-ops Date: Thu, 5 Nov 2026 11:20:07 +0000 Subject: [PATCH 04/12] create a function to sum an array of numbers with input validation --- Sprint-1/implement/sum.js | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/Sprint-1/implement/sum.js b/Sprint-1/implement/sum.js index 9062aafe3..7a49fd1f6 100644 --- a/Sprint-1/implement/sum.js +++ b/Sprint-1/implement/sum.js @@ -1,4 +1,15 @@ function sum(elements) { + if (!Array.isArray(elements)) { + throw new Error("sum requires an array of numbers"); + } + if (!elements.every((a) => typeof a === "number" && !Number.isNaN(a))) { + throw new Error("sum requires an array of numbers"); + } + let Total = 0; + for (const value of elements) { + Total += value; + } + return Total; } module.exports = sum; From 1e00cf05a43333f10f500e4d4629ab5eee092954 Mon Sep 17 00:00:00 2001 From: gaireprakash20-ops Date: Thu, 5 Nov 2026 11:20:21 +0000 Subject: [PATCH 05/12] all tests for sum function with various inputs and edge cases --- Sprint-1/implement/sum.test.js | 77 +++++++++++++++++++++++----------- 1 file changed, 52 insertions(+), 25 deletions(-) diff --git a/Sprint-1/implement/sum.test.js b/Sprint-1/implement/sum.test.js index a288e0f0a..6c0cc307e 100644 --- a/Sprint-1/implement/sum.test.js +++ b/Sprint-1/implement/sum.test.js @@ -9,28 +9,55 @@ E.g. sum(['hey', 10, 'hi', 60, 10]) throws Error("sum requires an array of numbe 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") +describe("findMax", () => { + // Given an empty array + // When passed to the sum function + // Then it should return 0 + it("returns 0 for []", () => { + expect(sum([])).toEqual(0); + }); + + // Given an array with just one number + // When passed to the sum function + // Then it should return that number + it("returns the sum of number for [7]", () => { + expect(sum([7])).toEqual(7); + }); + // Given an array containing negative numbers + // When passed to the sum function + // Then it should still return the correct total sum + it("returns the correct sum with negative numbers", () => { + expect(sum([10, -4, -8, 5])).toEqual(3); + }); + // Given an array with decimal/float numbers + // When passed to the sum function + // Then it should return the correct total sum + it("returns the correct sum with decimal numbers", () => { + expect(sum([1.5, 2.25, 0.25])).toBeCloseTo(4); + expect(sum([0.1, 0.2])).toBeCloseTo(0.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") + it("throws when the array contains a non-number", () => { + expect(() => sum(["hey", 10, "hi", 60, 10])).toThrow( + new Error("sum requires an array of numbers") + ); + }); + // Given something that isn't an array at all, such as "hey", 42 or no argument + // When passed to the sum function + // Then it should throw Error("sum requires an array of numbers") + it.each([["hey"], [42], [null], [{}]])( + "throws when given %p instead of an array", + (input) => { + expect(() => sum(input)).toThrow( + new Error("sum requires an array of numbers") + ); + } + ); + + it("throws when called with no argument", () => { + expect(() => sum()).toThrow(new Error("sum requires an array of numbers")); + }); +}); From c85bbffaf12407aadf1146bf25e39a5888f5914f Mon Sep 17 00:00:00 2001 From: gaireprakash20-ops Date: Thu, 5 Nov 2026 11:26:52 +0000 Subject: [PATCH 06/12] correct the small typo in the test description for sum function --- Sprint-1/implement/sum.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sprint-1/implement/sum.test.js b/Sprint-1/implement/sum.test.js index 6c0cc307e..fff315115 100644 --- a/Sprint-1/implement/sum.test.js +++ b/Sprint-1/implement/sum.test.js @@ -9,7 +9,7 @@ E.g. sum(['hey', 10, 'hi', 60, 10]) throws Error("sum requires an array of numbe const sum = require("./sum.js"); // Acceptance Criteria: -describe("findMax", () => { +describe("sum", () => { // Given an empty array // When passed to the sum function // Then it should return 0 From 533cce3a37d31902473a071726257247eb696938 Mon Sep 17 00:00:00 2001 From: gaireprakash20-ops Date: Thu, 5 Nov 2026 11:41:16 +0000 Subject: [PATCH 07/12] write the code for the dedupe function. --- Sprint-1/implement/dedupe.js | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/Sprint-1/implement/dedupe.js b/Sprint-1/implement/dedupe.js index 781e8718a..b54117bd9 100644 --- a/Sprint-1/implement/dedupe.js +++ b/Sprint-1/implement/dedupe.js @@ -1 +1,11 @@ -function dedupe() {} +function dedupe(list) { + const result = []; + for (const item of list) { + if (!result.includes(item)) { + result.push(item); + } + } + return result; +} + +module.exports = dedupe; From 8b0d12902170232af93a291d4aaf4e488bf48e79 Mon Sep 17 00:00:00 2001 From: gaireprakash20-ops Date: Thu, 5 Nov 2026 11:41:33 +0000 Subject: [PATCH 08/12] make all the edges cases and acceptance criteria for the dedupe function --- Sprint-1/implement/dedupe.test.js | 45 +++++++++++++++++++++++-------- 1 file changed, 34 insertions(+), 11 deletions(-) diff --git a/Sprint-1/implement/dedupe.test.js b/Sprint-1/implement/dedupe.test.js index d7c8e3d8e..3cef7e23e 100644 --- a/Sprint-1/implement/dedupe.test.js +++ b/Sprint-1/implement/dedupe.test.js @@ -12,17 +12,40 @@ E.g. dedupe([1, 2, 1]) returns [1, 2] */ // Acceptance Criteria: +describe("dedupe", () => { + // Given an empty array + // When passed to the dedupe function + // Then it should return an empty array + it("returns [] for []", () => { + expect(dedupe([])).toEqual([]); + }); -// 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"); + // Given an array with no duplicates + // When passed to the dedupe function + // Then it should return a copy of the original array + it("returns a copy of the array when there are no duplicates", () => { + // const list = [1, 2, 3]; + // const result = dedupe(list); + // expect(result).toEqual([1, 2, 3]); + // expect(result).not.toBe(list); + expect(dedupe([1, "1", 2])).toEqual([1, "1", 2]); + }); + // 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("removes duplicate numbers, keeping the first occurrence order", () => { + 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("treats 1 and '1' as different values", () => { + expect(dedupe([1, "1", 1, "1"])).toEqual([1, "1"]); + }); -// 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("doesn't modify the input array", () => { + const list = [1, 1, 2]; + dedupe(list); + expect(list).toEqual([1, 1, 2]); + }); +}); From a77f4b8d1954b20a632722f02113f70644238780 Mon Sep 17 00:00:00 2001 From: gaireprakash20-ops Date: Thu, 5 Nov 2026 11:59:41 +0000 Subject: [PATCH 09/12] make a mean function that calculates the mean of an array of numbers --- Sprint-1/implement/mean.js | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/Sprint-1/implement/mean.js b/Sprint-1/implement/mean.js index 36909ea1f..c7748da1d 100644 --- a/Sprint-1/implement/mean.js +++ b/Sprint-1/implement/mean.js @@ -1,3 +1,21 @@ -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((v) => typeof v === "number" && !Number.isNaN(v))) { + throw new Error("calculateMean requires an array of numbers"); + } + + let total = 0; + for (const value of list) { + total += value; + } + return total / list.length; +} module.exports = calculateMean; From 43846933dd4dd328482fd02116f43eae6c4120bc Mon Sep 17 00:00:00 2001 From: gaireprakash20-ops Date: Thu, 5 Nov 2026 12:00:08 +0000 Subject: [PATCH 10/12] make all the edges cases for the calculateMean function --- Sprint-1/implement/mean.test.js | 89 ++++++++++++++++++++++----------- 1 file changed, 60 insertions(+), 29 deletions(-) diff --git a/Sprint-1/implement/mean.test.js b/Sprint-1/implement/mean.test.js index 847b88676..cf1656b72 100644 --- a/Sprint-1/implement/mean.test.js +++ b/Sprint-1/implement/mean.test.js @@ -18,33 +18,64 @@ We have set things up already so that this file can see your function from the o const calculateMean = require("./mean.js"); // Acceptance criteria: +describe("calculatedMean", () => { + // 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. + it("returns 3 for [1, 2, 6]", () => { + expect(calculateMean([1, 2, 6])).toEqual(3); + }); -// 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") + // Given an array with a single number + // When passed to calculateMean + // Then it should return that number + it("returns the number itself for [7]", () => { + expect(calculateMean([7])).toEqual(7); + }); + // Given an array containing negative or decimal numbers + // When passed to calculateMean + // Then it should return the correct mean + it("returns the correct mean with negative numbers", () => { + expect(calculateMean([-2, 4, -6, 8])).toEqual(1); + }); + it("returns the correct mean with decimal numbers", () => { + expect(calculateMean([1.5, 2.5, 3.5])).toBeCloseTo(2.5); + expect(calculateMean([0.1, 0.2])).toBeCloseTo(0.15); + }); + // 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.each([["banana"], [42], [null], [{}]])( + "throws when given %p instead of an array", + (input) => { + expect(() => calculateMean(input)).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 called with no 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 the array contains a non-number", () => { + expect(() => calculateMean([1, "2", 3])).toThrow( + new Error("calculateMean requires an array of numbers") + ); + }); +}); From c6c2e2f04b620de161de7c0c57f1e310b68127a2 Mon Sep 17 00:00:00 2001 From: gaireprakash20-ops Date: Thu, 5 Nov 2026 12:11:30 +0000 Subject: [PATCH 11/12] in describeMedian function i add the try catch block to handle errors when calculating the median --- 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..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; From d139abcefebb167f8f8ead5d234273b6fa40d3b0 Mon Sep 17 00:00:00 2001 From: gaireprakash20-ops Date: Thu, 5 Nov 2026 12:12:06 +0000 Subject: [PATCH 12/12] MAKE A text edges that can be used to describe the median of a list of numbers --- Sprint-1/implement/describe-median.test.js | 65 ++++++++++++++++++---- 1 file changed, 54 insertions(+), 11 deletions(-) diff --git a/Sprint-1/implement/describe-median.test.js b/Sprint-1/implement/describe-median.test.js index e7d18a264..7aaf09ba4 100644 --- a/Sprint-1/implement/describe-median.test.js +++ b/Sprint-1/implement/describe-median.test.js @@ -16,17 +16,60 @@ message, so the caller can see what went wrong. const describeMedian = require("./describe-median.js"); // Acceptance criteria: +describe("describeMedian", () => { + // 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. + it('returns "The median is 2" for [1, 2, 3]', () => { + expect(describeMedian([1, 2, 3])).toEqual("The median is 2"); + }); -// 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"'); + it("works for an even-length array", () => { + expect(describeMedian([1, 2, 3, 4])).toEqual("The median is 2.5"); + }); -// Given an empty array -// When passed to describeMedian -// Then it should return "Could not calculate a median: calculateMedian requires a non-empty array" + it("works for an unsorted array", () => { + expect(describeMedian([3, 1, 2])).toEqual("The median is 2"); + }); -// 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" + // Given an empty array + // When passed to describeMedian + // Then it should return "Could not calculate a median: calculateMedian requires a non-empty array" + it("explains the problem for 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("explains the problem for 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, explain why + it.each([["banana"], [42], [null], [{}]])( + "explains the problem when given %p instead of an array", + (input) => { + expect(describeMedian(input)).toEqual( + "Could not calculate a median: calculateMedian requires an array of numbers" + ); + } + ); + + it("explains the problem when called with no argument", () => { + expect(describeMedian()).toEqual( + "Could not calculate a median: calculateMedian requires an array of numbers" + ); + }); + + it("explains the problem for an array containing a non-number", () => { + expect(describeMedian([1, "2", 3])).toEqual( + "Could not calculate a median: calculateMedian requires an array of numbers" + ); + }); +});