From ca8c1ce950f1796765d42be80e0a65bee0d57e87 Mon Sep 17 00:00:00 2001 From: Roger Lee Date: Mon, 2 Nov 2026 22:18:19 +0000 Subject: [PATCH 01/13] completed implementation of mean.js --- Sprint-1/fix/median.js | 39 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 36 insertions(+), 3 deletions(-) diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index 5c5b796e1..bb02af44f 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -6,9 +6,42 @@ // 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) === false) { + throw new Error("calculateMedian requires an array of numbers"); + } + + if (list === null || list === undefined) { + throw new Error("calculateMedian requires an array of numbers"); + } + + if (typeof list === "string" || typeof list === "number") { + throw new Error("calculateMedian requires an array of numbers"); + } + + if (list.length === 0) { + throw new Error("calculateMedian requires a non-empty array"); + } + + if (list.every((item) => typeof item === "string")) { + throw new Error("calculateMedian requires an array of numbers"); + } + + if (list.some((item) => typeof item === "string" || item === null || item === undefined)) { + throw new Error("calculateMedian requires an array of numbers"); + } + + const sortedList = [...list].sort((a, b) => a - b); + + if (sortedList.length % 2 === 0) { + const evenMiddleIndex = Math.floor(sortedList.length / 2); + const sumOfEvenMiddleIndex = + sortedList.splice(evenMiddleIndex, 1)[0] + + sortedList.splice(evenMiddleIndex - 1, 1)[0]; + return sumOfEvenMiddleIndex / 2; + } else { + const oddMiddleIndex = Math.ceil(sortedList.length / 2); + return sortedList.splice(oddMiddleIndex - 1, 1)[0]; + } } module.exports = calculateMedian; From 1d434da96c96da34e90b9e5ce4189906acb52201 Mon Sep 17 00:00:00 2001 From: Roger Lee Date: Tue, 3 Nov 2026 11:35:58 +0000 Subject: [PATCH 02/13] complete implementation of dedupe.js and test --- Sprint-1/implement/dedupe.js | 6 ++- Sprint-1/implement/dedupe.test.js | 66 +++++++++++++++++++++---------- 2 files changed, 51 insertions(+), 21 deletions(-) diff --git a/Sprint-1/implement/dedupe.js b/Sprint-1/implement/dedupe.js index 781e8718a..7abb5fb70 100644 --- a/Sprint-1/implement/dedupe.js +++ b/Sprint-1/implement/dedupe.js @@ -1 +1,5 @@ -function dedupe() {} +function dedupe(list) { + return [...new Set(list)]; +} + +module.exports = dedupe; diff --git a/Sprint-1/implement/dedupe.test.js b/Sprint-1/implement/dedupe.test.js index d7c8e3d8e..35f4676c6 100644 --- a/Sprint-1/implement/dedupe.test.js +++ b/Sprint-1/implement/dedupe.test.js @@ -1,28 +1,54 @@ const dedupe = require("./dedupe.js"); -/* -Dedupe Array -📖 Dedupe means **deduplicate** +//Dedupe Array -In this kata, you will need to deduplicate the elements of an array +//📖 Dedupe means **deduplicate** -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] -E.g. dedupe([1, 2, 1]) returns [1, 2] -*/ +//In this kata, you will need to deduplicate the elements of an array -// Acceptance Criteria: +describe("dedupe", () => { + //E.g. dedupe(['a','a','a','b','b','c']) returns ['a','b','c'] + it("returns for ['a','a','a','b','b','c']", () => { + expect(dedupe(["a", "a", "a", "b", "b", "c"])).toEqual(["a", "b", "c"]); + }); -// 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"); + //E.g. dedupe([5, 1, 1, 2, 3, 2, 5, 8]) returns [5, 1, 2, 3, 8] + it("returns for [5, 1, 1, 2, 3, 2, 5, 8]", () => { + expect(dedupe([5, 1, 1, 2, 3, 2, 5, 8])).toEqual([5, 1, 2, 3, 8]); + }); + //E.g. dedupe([1, 2, 1]) returns [1, 2] + //*/ + it("returns for [1, 2, 1]", () => { + 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 + // Acceptance Criteria: -// 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 empty array + // When passed to the dedupe function + // Then it should return an empty array + + it("returns for []", () => { + 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 + + it("returns for [1, 2, 3, 4]", () => { + expect(dedupe([1, 2, 3, 4])).toEqual([1, 2, 3, 4]); + }); + + // 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("returns for ['apple', 'orange', 'banana', 'banana']", () => { + expect(dedupe(["apple", "orange", "banana", "banana"])).toEqual([ + "apple", + "orange", + "banana", + ]); + }); +}); From e2480dee14539563f7b803623fc6b27899dc1373 Mon Sep 17 00:00:00 2001 From: Roger Lee Date: Wed, 4 Nov 2026 10:02:40 +0000 Subject: [PATCH 03/13] completed test and implementation for describe-median --- Sprint-1/implement/describe-median.js | 5 ++++- Sprint-1/implement/describe-median.test.js | 16 +++++++++++++++- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/Sprint-1/implement/describe-median.js b/Sprint-1/implement/describe-median.js index 3381cfc0a..1659cb048 100644 --- a/Sprint-1/implement/describe-median.js +++ b/Sprint-1/implement/describe-median.js @@ -20,6 +20,9 @@ function calculateMedian(list) { } // Implement this function. See describe-median.test.js for the acceptance criteria. -function describeMedian(list) {} +function describeMedian(list) { + const result = calculateMedian(list); + return `The median is ${result}` +} module.exports = describeMedian; diff --git a/Sprint-1/implement/describe-median.test.js b/Sprint-1/implement/describe-median.test.js index e7d18a264..bb537cd52 100644 --- a/Sprint-1/implement/describe-median.test.js +++ b/Sprint-1/implement/describe-median.test.js @@ -21,12 +21,26 @@ 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("describeMedian", () => { +it("returns for [1, 2, 3, 4]", () => { +const array = [1, 2, 3, 4] +const result = describeMedian(array); +expect(result).toBe('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 ("returns for []", () => { + expect(() => describeMedian([])).toThrow( + new Error("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 for ['banana', 'apple', 'orange']", () => { + expect(() => describeMedian(['banana', 'apple', 'orange'])).toThrow( + new Error("calculateMedian requires an array of numbers")) +}) +}) \ No newline at end of file From 676c4faa85b90b5acd3e96eae2982ee72a90d45f Mon Sep 17 00:00:00 2001 From: Roger Lee Date: Wed, 4 Nov 2026 13:41:56 +0000 Subject: [PATCH 04/13] completed test and implementation for max. --- Sprint-1/implement/max.js | 8 +++- Sprint-1/implement/max.test.js | 82 ++++++++++++++++++++++++++-------- 2 files changed, 70 insertions(+), 20 deletions(-) diff --git a/Sprint-1/implement/max.js b/Sprint-1/implement/max.js index 6dd76378e..05562c338 100644 --- a/Sprint-1/implement/max.js +++ b/Sprint-1/implement/max.js @@ -1,4 +1,10 @@ function findMax(elements) { + if (!Array.isArray(elements)) { + throw new Error("findMax requires an array of numbers"); + } + if (elements.some((element) => typeof element === "string")) { + throw new Error("findMax requires an array of 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 ff3fbb5e5..a03fed5da 100644 --- a/Sprint-1/implement/max.test.js +++ b/Sprint-1/implement/max.test.js @@ -16,28 +16,72 @@ 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("findMax", () => { + it("returns for []", () => { + expect(findMax([])).toEqual(-Infinity); + }); -// Given an array with one number -// When passed to the max function -// Then it should return that number + // Given an array with one number + // When passed to the max function + // Then it should return that number + it("returns for [17]", () => { + expect(findMax([17])).toBe(17); + }); -// 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 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 for [-6, 15, 30, 42, -90]", () => { + expect(findMax([-6, 15, 30, 42, -90])).toBe(42); + }); -// Given an array with decimal numbers -// When passed to the max function -// Then it should return the largest decimal number + // 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 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("returns for [-6, 15, 30, 42, -90]", () => { + expect(findMax([-6, 15, 30, 42, -90])).toBe(42); + }); -// 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") + // Given an array with decimal numbers + // When passed to the max function + // Then it should return the largest decimal number + + it("returns for [0.5, 1.5, 3.5, 5.5, 7.5]", () => { + expect(findMax([0.5, 1.5, 3.5, 5.5, 7.5])).toBe(7.5); + }); + + // 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("returns for [2, 4, 'hello', 6, 8, 10]", () => { + () => + expect(findMax([2, 4, "hello", 6, 8, 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("returns for 'hey'", () => { + expect(() => findMax("hey")).toThrow( + new Error("findMax requires an array of numbers") + ); + }); + + it("returns for 42", () => { + expect(() => findMax(42)).toThrow( + new Error("findMax requires an array of numbers") + ); + }); + + it("returns for ''", () => { + expect(() => findMax()).toThrow( + new Error("findMax requires an array of numbers") + ); + }); +}); From 2479c5303e5c7efe3fa0b1f5d3f4a0bbe09241b2 Mon Sep 17 00:00:00 2001 From: Roger Lee Date: Wed, 4 Nov 2026 22:03:57 +0000 Subject: [PATCH 05/13] completed test and implementation for mean --- Sprint-1/implement/max.test.js | 8 +-- Sprint-1/implement/mean.js | 23 +++++++- Sprint-1/implement/mean.test.js | 96 ++++++++++++++++++++++++++------- 3 files changed, 103 insertions(+), 24 deletions(-) diff --git a/Sprint-1/implement/max.test.js b/Sprint-1/implement/max.test.js index a03fed5da..0623badf0 100644 --- a/Sprint-1/implement/max.test.js +++ b/Sprint-1/implement/max.test.js @@ -25,7 +25,7 @@ describe("findMax", () => { // When passed to the max function // Then it should return that number it("returns for [17]", () => { - expect(findMax([17])).toBe(17); + expect(findMax([17])).toEqual(17); }); // Given an array with both positive and negative numbers @@ -33,7 +33,7 @@ describe("findMax", () => { // Then it should return the largest number overall it("returns for [-6, 15, 30, 42, -90]", () => { - expect(findMax([-6, 15, 30, 42, -90])).toBe(42); + expect(findMax([-6, 15, 30, 42, -90])).toEqual(42); }); // Given an array with just negative numbers @@ -41,7 +41,7 @@ describe("findMax", () => { // Then it should return the closest one to zero it("returns for [-6, 15, 30, 42, -90]", () => { - expect(findMax([-6, 15, 30, 42, -90])).toBe(42); + expect(findMax([-6, 15, 30, 42, -90])).toEqual(42); }); // Given an array with decimal numbers @@ -49,7 +49,7 @@ describe("findMax", () => { // Then it should return the largest decimal number it("returns for [0.5, 1.5, 3.5, 5.5, 7.5]", () => { - expect(findMax([0.5, 1.5, 3.5, 5.5, 7.5])).toBe(7.5); + expect(findMax([0.5, 1.5, 3.5, 5.5, 7.5])).toEqual(7.5); }); // Given an array containing a value that isn't a number diff --git a/Sprint-1/implement/mean.js b/Sprint-1/implement/mean.js index 36909ea1f..e7291f577 100644 --- a/Sprint-1/implement/mean.js +++ b/Sprint-1/implement/mean.js @@ -1,3 +1,24 @@ -function calculateMean(list) {} +function calculateMean(list) { + if (!Array.isArray(list) || 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.some((item) => typeof item === "string")) { + throw new Error("calculateMean requires an array of numbers"); + } + + let sum = 0; + for (const item of list) { + sum += item; + } + + 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 847b88676..19e0cd1c6 100644 --- a/Sprint-1/implement/mean.test.js +++ b/Sprint-1/implement/mean.test.js @@ -23,28 +23,86 @@ 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("return [1, 2, 6]", () => { + expect(calculateMean([1, 2, 6])).toEqual(3); + }); -// Given an array with a single number -// When passed to calculateMean -// Then it should return that number + // Given an array with a single number + // When passed to calculateMean + // Then it should return that number + it("returns for [6]", () => { + expect(calculateMean([6])).toEqual(6); + }); -// Given an array containing negative or decimal numbers -// When passed to calculateMean -// Then it should return the correct mean + // Given an array containing negative or decimal numbers + // When passed to calculateMean + // Then it should return the correct mean + it("returns for 10, 20.5, -10, 30.5]", () => { + expect(calculateMean([10, 20.5, -10, 30.5])).toEqual(12.75); + }); -// Given an empty array -// When passed to calculateMean -// Then it should throw Error("calculateMean requires a non-empty array") + // 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") + it("returns for []", () => { + () => + 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") -// 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("returns for 'banana'", () => { + () => + expect(calculateMean("banana")).toThrow( + new Error("calculateMean requires an array of numbers") + ); + }); + + it("returns for 42", () => { + () => + expect(calculateMean(42)).toThrow( + new Error("calculateMean requires an array of numbers") + ); + }); + + it("returns for null", () => { + () => + expect(calculateMean(null)).toThrow( + new Error("calculateMean requires an array of numbers") + ); + }); + + it("returns for {}", () => { + () => + expect(calculateMean({})).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("returns for ''", () => { + () => + 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("returns for [1, '2', 3]", () => { + () => + expect(calculateMean([1, "2", 3])).toThrow( + new Error("calculateMean requires an array of numbers") + ); + }); +}); From 0bc4a3af52dfc9fc681d09ebd84337d4a883e324 Mon Sep 17 00:00:00 2001 From: Roger Lee Date: Wed, 4 Nov 2026 22:40:17 +0000 Subject: [PATCH 06/13] completed test and implementation for sum --- Sprint-1/implement/sum.js | 13 +++++++ Sprint-1/implement/sum.test.js | 65 +++++++++++++++++++++++++--------- 2 files changed, 62 insertions(+), 16 deletions(-) diff --git a/Sprint-1/implement/sum.js b/Sprint-1/implement/sum.js index 9062aafe3..03c5718dc 100644 --- a/Sprint-1/implement/sum.js +++ b/Sprint-1/implement/sum.js @@ -1,4 +1,17 @@ function sum(elements) { + if (!Array.isArray(elements) || elements === "") { + throw new Error("sum requires an array of numbers"); + } + + if (elements.some((element) => typeof element === "string")) { + throw new Error("sum requires an array of numbers"); + } + let result = 0; + for (const element of elements) { + result += element; + } + + return result; } module.exports = sum; diff --git a/Sprint-1/implement/sum.test.js b/Sprint-1/implement/sum.test.js index a288e0f0a..3298d4896 100644 --- a/Sprint-1/implement/sum.test.js +++ b/Sprint-1/implement/sum.test.js @@ -13,24 +13,57 @@ 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("returns 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 for [8]", () => { + expect(sum([8])).toEqual(8); + }); + // Given an array containing negative numbers + // When passed to the sum function + // Then it should still return the correct total sum + it("returns for [8, -5, 10]", () => { + expect(sum([8, -5, 10])).toEqual(13); + }); -// Given an array with just one number -// When passed to the sum function -// Then it should return that number + // Given an array with decimal/float numbers + // When passed to the sum function + // Then it should return the correct total sum + it("returns for [10, 3.75, 2.25]", () => { + expect(sum([10, 3.75, 2.25])).toEqual(16); + }); + // 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 negative numbers -// When passed to the sum function -// Then it should still return the correct total sum + it("returns for [10, 20, '30']", () => { + () => + expect( + sum([10, 20, "30"]).toThrow( + new Error("sum requires an array of numbers") + ) + ); + }); -// Given an array with decimal/float numbers -// When passed to the sum function -// Then it should return the correct total sum + // 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("returns for 'hey'", () => { + () => + expect(sum("hey")).toThrow(new 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("returns for (42)", () => { + () => + expect(sum(42)).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("returns for ('')", () => { + () => + expect(sum("")).toThrow(new Error("sum requires an array of numbers")); + }); +}); From fe7131b666616064e32221a2b463c3f7c932fd9f Mon Sep 17 00:00:00 2001 From: Roger Lee Date: Thu, 5 Nov 2026 16:08:17 +0000 Subject: [PATCH 07/13] completed implementation of includes. --- Sprint-1/refactor/includes.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/Sprint-1/refactor/includes.js b/Sprint-1/refactor/includes.js index 29dad81f0..6aeac0b5d 100644 --- a/Sprint-1/refactor/includes.js +++ b/Sprint-1/refactor/includes.js @@ -1,9 +1,8 @@ // 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]; - if (element === target) { + for (const item of list) { + if (item === target) { return true; } } From 97e2315c2c8134c6a5873748a3c2d3aa70a04f63 Mon Sep 17 00:00:00 2001 From: Roger Lee Date: Fri, 6 Nov 2026 20:14:37 +0000 Subject: [PATCH 08/13] modified the function of median. --- Sprint-1/fix/median.js | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index bb02af44f..cfcbf88f0 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -10,22 +10,10 @@ function calculateMedian(list) { throw new Error("calculateMedian requires an array of numbers"); } - if (list === null || list === undefined) { - throw new Error("calculateMedian requires an array of numbers"); - } - - if (typeof list === "string" || typeof list === "number") { - throw new Error("calculateMedian requires an array of numbers"); - } - if (list.length === 0) { throw new Error("calculateMedian requires a non-empty array"); } - if (list.every((item) => typeof item === "string")) { - throw new Error("calculateMedian requires an array of numbers"); - } - if (list.some((item) => typeof item === "string" || item === null || item === undefined)) { throw new Error("calculateMedian requires an array of numbers"); } From 7b99611548b8389117ee1263cf3bd4a818049e12 Mon Sep 17 00:00:00 2001 From: Roger Lee Date: Fri, 6 Nov 2026 21:01:54 +0000 Subject: [PATCH 09/13] modified the function of median --- Sprint-1/fix/median.js | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index cfcbf88f0..390414624 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -21,14 +21,13 @@ function calculateMedian(list) { const sortedList = [...list].sort((a, b) => a - b); if (sortedList.length % 2 === 0) { - const evenMiddleIndex = Math.floor(sortedList.length / 2); + const evenMiddleIndex = sortedList.length / 2; const sumOfEvenMiddleIndex = - sortedList.splice(evenMiddleIndex, 1)[0] + - sortedList.splice(evenMiddleIndex - 1, 1)[0]; + sortedList[evenMiddleIndex - 1] + sortedList[evenMiddleIndex] return sumOfEvenMiddleIndex / 2; } else { - const oddMiddleIndex = Math.ceil(sortedList.length / 2); - return sortedList.splice(oddMiddleIndex - 1, 1)[0]; + const oddMiddleIndex = Math.floor(sortedList.length / 2); + return sortedList[oddMiddleIndex]; } } From 601eba7de1801c69c4e0bd9db70a229a0777c864 Mon Sep 17 00:00:00 2001 From: Roger Lee Date: Fri, 6 Nov 2026 21:16:24 +0000 Subject: [PATCH 10/13] modified the test of dedupe --- Sprint-1/implement/dedupe.test.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Sprint-1/implement/dedupe.test.js b/Sprint-1/implement/dedupe.test.js index 35f4676c6..a0dc1533e 100644 --- a/Sprint-1/implement/dedupe.test.js +++ b/Sprint-1/implement/dedupe.test.js @@ -37,7 +37,9 @@ describe("dedupe", () => { // Then it should return a copy of the original array it("returns for [1, 2, 3, 4]", () => { - expect(dedupe([1, 2, 3, 4])).toEqual([1, 2, 3, 4]); + const original = [1, 2, 3, 4]; + const result = dedupe(original); + expect(result)not.toBe(original); }); // Given an array of strings or numbers From a0de9194f90438b79851dbac4d6e5838c6208711 Mon Sep 17 00:00:00 2001 From: Roger Lee Date: Fri, 6 Nov 2026 21:23:02 +0000 Subject: [PATCH 11/13] modified the dedupe test and describe-median --- Sprint-1/implement/dedupe.test.js | 2 +- Sprint-1/implement/describe-median.js | 6 +++--- Sprint-1/implement/describe-median.test.js | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/Sprint-1/implement/dedupe.test.js b/Sprint-1/implement/dedupe.test.js index a0dc1533e..2e226dd99 100644 --- a/Sprint-1/implement/dedupe.test.js +++ b/Sprint-1/implement/dedupe.test.js @@ -39,7 +39,7 @@ describe("dedupe", () => { it("returns for [1, 2, 3, 4]", () => { const original = [1, 2, 3, 4]; const result = dedupe(original); - expect(result)not.toBe(original); + expect(result).not.toBe(original); }); // Given an array of strings or numbers diff --git a/Sprint-1/implement/describe-median.js b/Sprint-1/implement/describe-median.js index 1659cb048..ea3f8321d 100644 --- a/Sprint-1/implement/describe-median.js +++ b/Sprint-1/implement/describe-median.js @@ -1,14 +1,14 @@ // Don't change this function. function calculateMedian(list) { if (!Array.isArray(list)) { - throw new Error("calculateMedian requires an array of numbers"); + throw new Error("Could not calculate a median: calculateMedian requires an array of numbers"); } if (list.length === 0) { - throw new Error("calculateMedian requires a non-empty array"); + throw new Error("Could not calculate a median: calculateMedian requires a non-empty array"); } for (const item of list) { if (typeof item !== "number") { - throw new Error("calculateMedian requires an array of numbers"); + throw new Error("Could not calculate a median: calculateMedian requires an array of numbers"); } } const middleIndex = Math.floor(list.length / 2); diff --git a/Sprint-1/implement/describe-median.test.js b/Sprint-1/implement/describe-median.test.js index bb537cd52..a838d40ad 100644 --- a/Sprint-1/implement/describe-median.test.js +++ b/Sprint-1/implement/describe-median.test.js @@ -33,7 +33,7 @@ expect(result).toBe('The median is 2.5') // Then it should return "Could not calculate a median: calculateMedian requires a non-empty array" it ("returns for []", () => { expect(() => describeMedian([])).toThrow( - new Error("calculateMedian requires a non-empty array")) + new Error("Could not calculate a median: calculateMedian requires a non-empty array")) }) // Given something that isn't an array of numbers, e.g. "banana" @@ -41,6 +41,6 @@ it ("returns for []", () => { // Then it should return "Could not calculate a median: calculateMedian requires an array of numbers" it ("returns for ['banana', 'apple', 'orange']", () => { expect(() => describeMedian(['banana', 'apple', 'orange'])).toThrow( - new Error("calculateMedian requires an array of numbers")) + new Error("Could not calculate a median: calculateMedian requires an array of numbers")) }) }) \ No newline at end of file From f5247f1cfeeb9323d07cc7d91c66293c81374b61 Mon Sep 17 00:00:00 2001 From: Roger Lee Date: Fri, 6 Nov 2026 21:26:57 +0000 Subject: [PATCH 12/13] format document for describe-median test --- Sprint-1/implement/describe-median.test.js | 48 ++++++++++++---------- 1 file changed, 27 insertions(+), 21 deletions(-) diff --git a/Sprint-1/implement/describe-median.test.js b/Sprint-1/implement/describe-median.test.js index a838d40ad..59dcf2b57 100644 --- a/Sprint-1/implement/describe-median.test.js +++ b/Sprint-1/implement/describe-median.test.js @@ -22,25 +22,31 @@ const describeMedian = require("./describe-median.js"); // Then it should return "The median is " followed by the median // Delete this test.todo and replace it with a test. describe("describeMedian", () => { -it("returns for [1, 2, 3, 4]", () => { -const array = [1, 2, 3, 4] -const result = describeMedian(array); -expect(result).toBe('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 ("returns for []", () => { + it("returns for [1, 2, 3, 4]", () => { + const array = [1, 2, 3, 4]; + const result = describeMedian(array); + expect(result).toBe("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("returns for []", () => { expect(() => describeMedian([])).toThrow( - new Error("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 for ['banana', 'apple', 'orange']", () => { - expect(() => describeMedian(['banana', 'apple', 'orange'])).toThrow( - new Error("Could not calculate a median: calculateMedian requires an array of numbers")) -}) -}) \ No newline at end of file + new Error( + "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 for ['banana', 'apple', 'orange']", () => { + expect(() => describeMedian(["banana", "apple", "orange"])).toThrow( + new Error( + "Could not calculate a median: calculateMedian requires an array of numbers" + ) + ); + }); +}); From f64fe714f39e41ba34df713fb3a43eec1c4e8ebf Mon Sep 17 00:00:00 2001 From: Roger Lee Date: Fri, 6 Nov 2026 21:48:37 +0000 Subject: [PATCH 13/13] removed redundant check for mean. added test for floating number. --- Sprint-1/implement/mean.js | 2 +- Sprint-1/implement/mean.test.js | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/Sprint-1/implement/mean.js b/Sprint-1/implement/mean.js index e7291f577..0e984b9ef 100644 --- a/Sprint-1/implement/mean.js +++ b/Sprint-1/implement/mean.js @@ -1,5 +1,5 @@ function calculateMean(list) { - if (!Array.isArray(list) || list === "") { + if (!Array.isArray(list)) { 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 19e0cd1c6..06f572107 100644 --- a/Sprint-1/implement/mean.test.js +++ b/Sprint-1/implement/mean.test.js @@ -42,6 +42,11 @@ describe("calculateMean", () => { expect(calculateMean([10, 20.5, -10, 30.5])).toEqual(12.75); }); + it ("returns for [1.2, 0.6, 0.005]", () => { + expect(calculateMean([1.2, 0.6, 0.005])).toBeCloseTo(0.6017); + + }); + // Given an empty array // When passed to calculateMean // Then it should throw Error("calculateMean requires a non-empty array")