From 69c0396021537f67aee487d30c7cfbe9d6d60a94 Mon Sep 17 00:00:00 2001 From: HM-127BTY Date: Sat, 31 Oct 2026 16:28:47 +0000 Subject: [PATCH 01/32] Adjusted code for medium with even and odd numbers. --- Sprint-1/fix/median.js | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index 5c5b796e1..1a9cfdcfc 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -5,10 +5,22 @@ // Hint: Please consider scenarios when 'list' isn't an array, is empty, // 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; +// } + +// module.exports = calculateMedian; + function calculateMedian(list) { - const middleIndex = Math.floor(list.length / 2); - const median = list.splice(middleIndex, 1)[0]; - return median; + if (list.length % 2 === 1) { + const middleIndex = Math.floor(list.length / 2); + return list[middleIndex]; + } + const rightMiddleIndex = Math.floor(list.length / 2 - 1); + const leftMiddleIndex = Math.floor(list.length / 2); + return (list[rightMiddleIndex] + list[leftMiddleIndex]) / 2; } module.exports = calculateMedian; From 6c18a294a852d20cd60cbe432b5bc36741ec895b Mon Sep 17 00:00:00 2001 From: HM-127BTY Date: Sat, 31 Oct 2026 16:29:38 +0000 Subject: [PATCH 02/32] Shortened the tests by using // to make it less overwhelming --- Sprint-1/fix/median.test.js | 184 ++++++++++++++++++------------------ 1 file changed, 92 insertions(+), 92 deletions(-) diff --git a/Sprint-1/fix/median.test.js b/Sprint-1/fix/median.test.js index 51d61cc86..7b12a9be5 100644 --- a/Sprint-1/fix/median.test.js +++ b/Sprint-1/fix/median.test.js @@ -23,96 +23,96 @@ describe("calculateMedian", () => { expect(calculateMedian([1, 2, 3, 4, 5, 6])).toEqual(3.5); }); - it("returns the correct median for unsorted array [3, 1, 2]", () => { - expect(calculateMedian([3, 1, 2])).toEqual(2); - }); - - it("returns the correct median for unsorted array [5, 1, 3, 4, 2]", () => { - expect(calculateMedian([5, 1, 3, 4, 2])).toEqual(3); - }); - - it("returns the correct median for unsorted array [4, 2, 1, 3]", () => { - expect(calculateMedian([4, 2, 1, 3])).toEqual(2.5); - }); - - it("returns the correct median for unsorted array [6, 1, 5, 3, 2, 4]", () => { - expect(calculateMedian([6, 1, 5, 3, 2, 4])).toEqual(3.5); - }); - - it("returns the correct median for unsorted array [110, 20, 0]", () => { - expect(calculateMedian([110, 20, 0])).toEqual(20); - }); - - it("returns the correct median for unsorted array [6, -2, 2, 12, 14]", () => { - expect(calculateMedian([6, -2, 2, 12, 14])).toEqual(6); - }); - - it("doesn't modify the input array [3, 1, 2]", () => { - const list = [3, 1, 2]; - calculateMedian(list); - expect(list).toEqual([3, 1, 2]); - }); - - // There is no median of an empty array, so calculateMedian should throw - it("throws when given an empty array", () => { - expect(() => calculateMedian([])).toThrow( - new Error("calculateMedian requires a non-empty array") - ); - }); - - // Input that isn't an array should throw - it("throws when given a string", () => { - expect(() => calculateMedian("banana")).toThrow( - new Error("calculateMedian requires an array of numbers") - ); - }); - - it("throws when given a number", () => { - expect(() => calculateMedian(123)).toThrow( - new Error("calculateMedian requires an array of numbers") - ); - }); - - it("throws when given null", () => { - expect(() => calculateMedian(null)).toThrow( - new Error("calculateMedian requires an array of numbers") - ); - }); - - it("throws when given an object", () => { - expect(() => calculateMedian({})).toThrow( - new Error("calculateMedian requires an array of numbers") - ); - }); - - it("throws when called with no argument", () => { - expect(() => calculateMedian()).toThrow( - new Error("calculateMedian requires an array of numbers") - ); - }); - - // Arrays containing any non-number value should throw, rather than filtering them out - it("throws for an array of strings", () => { - expect(() => calculateMedian(["ten", "twenty", "thirty"])).toThrow( - new Error("calculateMedian requires an array of numbers") - ); - }); - - it("throws for an array mixing numbers and strings", () => { - expect(() => calculateMedian([1, "2", 3, "4", 5])).toThrow( - new Error("calculateMedian requires an array of numbers") - ); - }); - - it("throws for an array containing null", () => { - expect(() => calculateMedian([1, 2, null, 4])).toThrow( - new Error("calculateMedian requires an array of numbers") - ); - }); - - it("throws for an array containing undefined", () => { - expect(() => calculateMedian([3, 1, undefined, 2])).toThrow( - new Error("calculateMedian requires an array of numbers") - ); - }); + // it("returns the correct median for unsorted array [3, 1, 2]", () => { + // expect(calculateMedian([3, 1, 2])).toEqual(2); + // }); + + // it("returns the correct median for unsorted array [5, 1, 3, 4, 2]", () => { + // expect(calculateMedian([5, 1, 3, 4, 2])).toEqual(3); + // }); + + // it("returns the correct median for unsorted array [4, 2, 1, 3]", () => { + // expect(calculateMedian([4, 2, 1, 3])).toEqual(2.5); + // }); + + // it("returns the correct median for unsorted array [6, 1, 5, 3, 2, 4]", () => { + // expect(calculateMedian([6, 1, 5, 3, 2, 4])).toEqual(3.5); + // }); + + // it("returns the correct median for unsorted array [110, 20, 0]", () => { + // expect(calculateMedian([110, 20, 0])).toEqual(20); + // }); + + // it("returns the correct median for unsorted array [6, -2, 2, 12, 14]", () => { + // expect(calculateMedian([6, -2, 2, 12, 14])).toEqual(6); + // }); + + // it("doesn't modify the input array [3, 1, 2]", () => { + // const list = [3, 1, 2]; + // calculateMedian(list); + // expect(list).toEqual([3, 1, 2]); + // }); + + // // There is no median of an empty array, so calculateMedian should throw + // it("throws when given an empty array", () => { + // expect(() => calculateMedian([])).toThrow( + // new Error("calculateMedian requires a non-empty array") + // ); + // }); + + // // Input that isn't an array should throw + // it("throws when given a string", () => { + // expect(() => calculateMedian("banana")).toThrow( + // new Error("calculateMedian requires an array of numbers") + // ); + // }); + + // it("throws when given a number", () => { + // expect(() => calculateMedian(123)).toThrow( + // new Error("calculateMedian requires an array of numbers") + // ); + // }); + + // it("throws when given null", () => { + // expect(() => calculateMedian(null)).toThrow( + // new Error("calculateMedian requires an array of numbers") + // ); + // }); + + // it("throws when given an object", () => { + // expect(() => calculateMedian({})).toThrow( + // new Error("calculateMedian requires an array of numbers") + // ); + // }); + + // it("throws when called with no argument", () => { + // expect(() => calculateMedian()).toThrow( + // new Error("calculateMedian requires an array of numbers") + // ); + // }); + + // // Arrays containing any non-number value should throw, rather than filtering them out + // it("throws for an array of strings", () => { + // expect(() => calculateMedian(["ten", "twenty", "thirty"])).toThrow( + // new Error("calculateMedian requires an array of numbers") + // ); + // }); + + // it("throws for an array mixing numbers and strings", () => { + // expect(() => calculateMedian([1, "2", 3, "4", 5])).toThrow( + // new Error("calculateMedian requires an array of numbers") + // ); + // }); + + // it("throws for an array containing null", () => { + // expect(() => calculateMedian([1, 2, null, 4])).toThrow( + // new Error("calculateMedian requires an array of numbers") + // ); + // }); + + // it("throws for an array containing undefined", () => { + // expect(() => calculateMedian([3, 1, undefined, 2])).toThrow( + // new Error("calculateMedian requires an array of numbers") + // ); + // }); }); From a9924372d83437b00c2850b6b78e69923a7e53dc Mon Sep 17 00:00:00 2001 From: HM-127BTY Date: Mon, 2 Nov 2026 13:26:10 +0000 Subject: [PATCH 03/32] removed // from more tests and see those tests failed, now working on code. --- Sprint-1/fix/median.test.js | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/Sprint-1/fix/median.test.js b/Sprint-1/fix/median.test.js index 7b12a9be5..f028f22ad 100644 --- a/Sprint-1/fix/median.test.js +++ b/Sprint-1/fix/median.test.js @@ -23,25 +23,25 @@ describe("calculateMedian", () => { expect(calculateMedian([1, 2, 3, 4, 5, 6])).toEqual(3.5); }); - // it("returns the correct median for unsorted array [3, 1, 2]", () => { - // expect(calculateMedian([3, 1, 2])).toEqual(2); - // }); + it("returns the correct median for unsorted array [3, 1, 2]", () => { + expect(calculateMedian([3, 1, 2])).toEqual(2); + }); - // it("returns the correct median for unsorted array [5, 1, 3, 4, 2]", () => { - // expect(calculateMedian([5, 1, 3, 4, 2])).toEqual(3); - // }); + it("returns the correct median for unsorted array [5, 1, 3, 4, 2]", () => { + expect(calculateMedian([5, 1, 3, 4, 2])).toEqual(3); + }); - // it("returns the correct median for unsorted array [4, 2, 1, 3]", () => { - // expect(calculateMedian([4, 2, 1, 3])).toEqual(2.5); - // }); + it("returns the correct median for unsorted array [4, 2, 1, 3]", () => { + expect(calculateMedian([4, 2, 1, 3])).toEqual(2.5); + }); - // it("returns the correct median for unsorted array [6, 1, 5, 3, 2, 4]", () => { - // expect(calculateMedian([6, 1, 5, 3, 2, 4])).toEqual(3.5); - // }); + it("returns the correct median for unsorted array [6, 1, 5, 3, 2, 4]", () => { + expect(calculateMedian([6, 1, 5, 3, 2, 4])).toEqual(3.5); + }); - // it("returns the correct median for unsorted array [110, 20, 0]", () => { - // expect(calculateMedian([110, 20, 0])).toEqual(20); - // }); + it("returns the correct median for unsorted array [110, 20, 0]", () => { + expect(calculateMedian([110, 20, 0])).toEqual(20); + }); // it("returns the correct median for unsorted array [6, -2, 2, 12, 14]", () => { // expect(calculateMedian([6, -2, 2, 12, 14])).toEqual(6); From 328458feca8c0f58f6be219a45336cc9ce0fe043 Mon Sep 17 00:00:00 2001 From: HM-127BTY Date: Mon, 2 Nov 2026 13:34:35 +0000 Subject: [PATCH 04/32] added a sort for the array with : `list.sort((a, b) => a - b)` and changed other `list` to `sortList` --- Sprint-1/fix/median.js | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index 1a9cfdcfc..5f564494b 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -14,13 +14,14 @@ // module.exports = calculateMedian; function calculateMedian(list) { - if (list.length % 2 === 1) { - const middleIndex = Math.floor(list.length / 2); - return list[middleIndex]; + let sortList = list.sort((a, b) => a - b); + if (sortList.length % 2 === 1) { + const middleIndex = Math.floor(sortList.length / 2); + return sortList[middleIndex]; } - const rightMiddleIndex = Math.floor(list.length / 2 - 1); - const leftMiddleIndex = Math.floor(list.length / 2); - return (list[rightMiddleIndex] + list[leftMiddleIndex]) / 2; + const rightMiddleIndex = Math.floor(sortList.length / 2 - 1); + const leftMiddleIndex = Math.floor(sortList.length / 2); + return (sortList[rightMiddleIndex] + sortList[leftMiddleIndex]) / 2; } module.exports = calculateMedian; From fcd612f7cbe6c57504a7a74c9af3faeffa5a64b1 Mon Sep 17 00:00:00 2001 From: HM-127BTY Date: Mon, 2 Nov 2026 13:37:04 +0000 Subject: [PATCH 05/32] removed // from more tests and ran into fail with "doesn't modify the input array [3, 1, 2]" working on this test now. --- Sprint-1/fix/median.test.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/Sprint-1/fix/median.test.js b/Sprint-1/fix/median.test.js index f028f22ad..c9384c6c7 100644 --- a/Sprint-1/fix/median.test.js +++ b/Sprint-1/fix/median.test.js @@ -43,15 +43,15 @@ describe("calculateMedian", () => { expect(calculateMedian([110, 20, 0])).toEqual(20); }); - // it("returns the correct median for unsorted array [6, -2, 2, 12, 14]", () => { - // expect(calculateMedian([6, -2, 2, 12, 14])).toEqual(6); - // }); + it("returns the correct median for unsorted array [6, -2, 2, 12, 14]", () => { + expect(calculateMedian([6, -2, 2, 12, 14])).toEqual(6); + }); - // it("doesn't modify the input array [3, 1, 2]", () => { - // const list = [3, 1, 2]; - // calculateMedian(list); - // expect(list).toEqual([3, 1, 2]); - // }); + it("doesn't modify the input array [3, 1, 2]", () => { + const list = [3, 1, 2]; + calculateMedian(list); + expect(list).toEqual([3, 1, 2]); + }); // // There is no median of an empty array, so calculateMedian should throw // it("throws when given an empty array", () => { From 28f23bb7fc9333c4e989401b90ba48e2a2f747e3 Mon Sep 17 00:00:00 2001 From: HM-127BTY Date: Mon, 2 Nov 2026 13:45:59 +0000 Subject: [PATCH 06/32] Added in "copyList" so that I could make a copy of original Array without making changed to it using "Array.from(list)" --- Sprint-1/fix/median.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index 5f564494b..1c16b6ec3 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -14,7 +14,8 @@ // module.exports = calculateMedian; function calculateMedian(list) { - let sortList = list.sort((a, b) => a - b); + const copyList = Array.from(list); + let sortList = copyList.sort((a, b) => a - b); if (sortList.length % 2 === 1) { const middleIndex = Math.floor(sortList.length / 2); return sortList[middleIndex]; From e3a93cc8fb3a55156287a0c95c34bdf2f9471e22 Mon Sep 17 00:00:00 2001 From: HM-127BTY Date: Mon, 2 Nov 2026 13:47:14 +0000 Subject: [PATCH 07/32] starting on next test for throwing error on empty array --- Sprint-1/fix/median.test.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/Sprint-1/fix/median.test.js b/Sprint-1/fix/median.test.js index c9384c6c7..0c646d22f 100644 --- a/Sprint-1/fix/median.test.js +++ b/Sprint-1/fix/median.test.js @@ -53,12 +53,12 @@ describe("calculateMedian", () => { expect(list).toEqual([3, 1, 2]); }); - // // There is no median of an empty array, so calculateMedian should throw - // it("throws when given an empty array", () => { - // expect(() => calculateMedian([])).toThrow( - // new Error("calculateMedian requires a non-empty array") - // ); - // }); + // There is no median of an empty array, so calculateMedian should throw + it("throws when given an empty array", () => { + expect(() => calculateMedian([])).toThrow( + new Error("calculateMedian requires a non-empty array") + ); + }); // // Input that isn't an array should throw // it("throws when given a string", () => { From 3e41283d4ee0de7f3be86a9414b6bdd435879d14 Mon Sep 17 00:00:00 2001 From: HM-127BTY Date: Mon, 2 Nov 2026 13:56:27 +0000 Subject: [PATCH 08/32] Added a throw new Error when the array is empty. --- Sprint-1/fix/median.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index 1c16b6ec3..c35feb4e0 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -14,6 +14,9 @@ // module.exports = calculateMedian; function calculateMedian(list) { + if (list.length === 0) { + throw new Error("calculateMedian requires a non-empty array"); + } const copyList = Array.from(list); let sortList = copyList.sort((a, b) => a - b); if (sortList.length % 2 === 1) { From 9590c714fe1ededcbbbe143c3981229c06911958 Mon Sep 17 00:00:00 2001 From: HM-127BTY Date: Mon, 2 Nov 2026 13:58:35 +0000 Subject: [PATCH 09/32] remove // from more tests and now working on the new errors t obe thrown --- Sprint-1/fix/median.test.js | 64 ++++++++++++++++++------------------- 1 file changed, 32 insertions(+), 32 deletions(-) diff --git a/Sprint-1/fix/median.test.js b/Sprint-1/fix/median.test.js index 0c646d22f..290ed4238 100644 --- a/Sprint-1/fix/median.test.js +++ b/Sprint-1/fix/median.test.js @@ -60,43 +60,43 @@ describe("calculateMedian", () => { ); }); - // // Input that isn't an array should throw - // it("throws when given a string", () => { - // expect(() => calculateMedian("banana")).toThrow( - // new Error("calculateMedian requires an array of numbers") - // ); - // }); + // Input that isn't an array should throw + it("throws when given a string", () => { + expect(() => calculateMedian("banana")).toThrow( + new Error("calculateMedian requires an array of numbers") + ); + }); - // it("throws when given a number", () => { - // expect(() => calculateMedian(123)).toThrow( - // new Error("calculateMedian requires an array of numbers") - // ); - // }); + it("throws when given a number", () => { + expect(() => calculateMedian(123)).toThrow( + new Error("calculateMedian requires an array of numbers") + ); + }); - // it("throws when given null", () => { - // expect(() => calculateMedian(null)).toThrow( - // new Error("calculateMedian requires an array of numbers") - // ); - // }); + it("throws when given null", () => { + expect(() => calculateMedian(null)).toThrow( + new Error("calculateMedian requires an array of numbers") + ); + }); - // it("throws when given an object", () => { - // expect(() => calculateMedian({})).toThrow( - // new Error("calculateMedian requires an array of numbers") - // ); - // }); + it("throws when given an object", () => { + expect(() => calculateMedian({})).toThrow( + new Error("calculateMedian requires an array of numbers") + ); + }); - // it("throws when called with no argument", () => { - // expect(() => calculateMedian()).toThrow( - // new Error("calculateMedian requires an array of numbers") - // ); - // }); + it("throws when called with no argument", () => { + expect(() => calculateMedian()).toThrow( + new Error("calculateMedian requires an array of numbers") + ); + }); - // // Arrays containing any non-number value should throw, rather than filtering them out - // it("throws for an array of strings", () => { - // expect(() => calculateMedian(["ten", "twenty", "thirty"])).toThrow( - // new Error("calculateMedian requires an array of numbers") - // ); - // }); + // Arrays containing any non-number value should throw, rather than filtering them out + it("throws for an array of strings", () => { + expect(() => calculateMedian(["ten", "twenty", "thirty"])).toThrow( + new Error("calculateMedian requires an array of numbers") + ); + }); // it("throws for an array mixing numbers and strings", () => { // expect(() => calculateMedian([1, "2", 3, "4", 5])).toThrow( From 8fc182a826aa600641a92f46c31e71b7fc7377db Mon Sep 17 00:00:00 2001 From: HM-127BTY Date: Mon, 2 Nov 2026 15:02:05 +0000 Subject: [PATCH 10/32] adding in .some to check for anything that isnt a number with .some and added in check for .isArray to ensure its an array --- Sprint-1/fix/median.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index c35feb4e0..968154022 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -14,7 +14,11 @@ // module.exports = calculateMedian; function calculateMedian(list) { - if (list.length === 0) { + if (!Array.isArray(list)) { + throw new Error("calculateMedian requires an array of numbers"); + } else if (list.some((item) => typeof item !== "number")) { + throw new Error("calculateMedian requires an array of numbers"); + } else if (list.length === 0) { throw new Error("calculateMedian requires a non-empty array"); } const copyList = Array.from(list); From 1f84354a7d98aab83cad2cb22f667631f8ee16ae Mon Sep 17 00:00:00 2001 From: HM-127BTY Date: Mon, 2 Nov 2026 15:03:11 +0000 Subject: [PATCH 11/32] Removed last // and run tests to see that all tests are working. --- Sprint-1/fix/median.test.js | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/Sprint-1/fix/median.test.js b/Sprint-1/fix/median.test.js index 290ed4238..51d61cc86 100644 --- a/Sprint-1/fix/median.test.js +++ b/Sprint-1/fix/median.test.js @@ -98,21 +98,21 @@ describe("calculateMedian", () => { ); }); - // it("throws for an array mixing numbers and strings", () => { - // expect(() => calculateMedian([1, "2", 3, "4", 5])).toThrow( - // new Error("calculateMedian requires an array of numbers") - // ); - // }); - - // it("throws for an array containing null", () => { - // expect(() => calculateMedian([1, 2, null, 4])).toThrow( - // new Error("calculateMedian requires an array of numbers") - // ); - // }); - - // it("throws for an array containing undefined", () => { - // expect(() => calculateMedian([3, 1, undefined, 2])).toThrow( - // new Error("calculateMedian requires an array of numbers") - // ); - // }); + it("throws for an array mixing numbers and strings", () => { + expect(() => calculateMedian([1, "2", 3, "4", 5])).toThrow( + new Error("calculateMedian requires an array of numbers") + ); + }); + + it("throws for an array containing null", () => { + expect(() => calculateMedian([1, 2, null, 4])).toThrow( + new Error("calculateMedian requires an array of numbers") + ); + }); + + it("throws for an array containing undefined", () => { + expect(() => calculateMedian([3, 1, undefined, 2])).toThrow( + new Error("calculateMedian requires an array of numbers") + ); + }); }); From a8d6728253c43693d10ae74bc6631de95979163c Mon Sep 17 00:00:00 2001 From: HM-127BTY Date: Mon, 2 Nov 2026 18:11:38 +0000 Subject: [PATCH 12/32] Making the code to deduplicate, found out about ...Set and attempting to use this. --- Sprint-1/implement/dedupe.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Sprint-1/implement/dedupe.js b/Sprint-1/implement/dedupe.js index 781e8718a..75f7bef6b 100644 --- a/Sprint-1/implement/dedupe.js +++ b/Sprint-1/implement/dedupe.js @@ -1 +1,3 @@ -function dedupe() {} +function dedupe(array) { + return [...Set(array)]; +} From 473b428f84525fa21b9a12fbb958f32d2d29208a Mon Sep 17 00:00:00 2001 From: HM-127BTY Date: Mon, 2 Nov 2026 18:30:35 +0000 Subject: [PATCH 13/32] got my code to work for the first test, better understanding of ...new Set() in the process. --- Sprint-1/implement/dedupe.js | 4 +++- Sprint-1/implement/dedupe.test.js | 11 ++++++++--- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/Sprint-1/implement/dedupe.js b/Sprint-1/implement/dedupe.js index 75f7bef6b..298230541 100644 --- a/Sprint-1/implement/dedupe.js +++ b/Sprint-1/implement/dedupe.js @@ -1,3 +1,5 @@ function dedupe(array) { - return [...Set(array)]; + return [...new Set(array)]; } + +module.exports = dedupe; diff --git a/Sprint-1/implement/dedupe.test.js b/Sprint-1/implement/dedupe.test.js index d7c8e3d8e..223111a9a 100644 --- a/Sprint-1/implement/dedupe.test.js +++ b/Sprint-1/implement/dedupe.test.js @@ -1,9 +1,14 @@ const dedupe = require("./dedupe.js"); /* Dedupe Array - 📖 Dedupe means **deduplicate** - +*/ +describe("deduplicate", () => { + it(`returns the deduplicate for ["a", "b", "c"]`, () => { + expect(dedupe(["a", "a", "a", "b", "b", "c"])).toEqual(["a", "b", "c"]); + }); +}); +/* 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'] @@ -24,5 +29,5 @@ test.todo("given an empty array, it returns an empty 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 fed43f7eba60094961fad20a5721b0902483d2a1 Mon Sep 17 00:00:00 2001 From: HM-127BTY Date: Mon, 2 Nov 2026 18:43:24 +0000 Subject: [PATCH 14/32] got into the flow of adding tests and ran into format bug, from lines 20-26 keeps making it look like that --- Sprint-1/implement/dedupe.test.js | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/Sprint-1/implement/dedupe.test.js b/Sprint-1/implement/dedupe.test.js index 223111a9a..f207826e1 100644 --- a/Sprint-1/implement/dedupe.test.js +++ b/Sprint-1/implement/dedupe.test.js @@ -4,9 +4,32 @@ Dedupe Array 📖 Dedupe means **deduplicate** */ describe("deduplicate", () => { - it(`returns the deduplicate for ["a", "b", "c"]`, () => { + it(`returns the deduplicate for string letters`, () => { expect(dedupe(["a", "a", "a", "b", "b", "c"])).toEqual(["a", "b", "c"]); }); + + it(`returns the deduplicate for numbers`, () => { + expect(dedupe([5, 1, 1, 2, 3, 2, 5, 8])).toEqual([5, 1, 2, 3, 8]); + }); + + it(`returns the deduplicate while not numerical in order`, () => { + expect(dedupe([1, 2, 1])).toEqual([1, 2]); + }); + + it(`returns the deduplicate for string and numbers `, () => { + expect(dedupe([5, 1, 1, 2, 3, "a", "a", "a", "c"])).toEqual([ + 5, + 1, + 2, + 3, + "a", + "c", + ]); + }); + + it(`returns the empty array when given an empty array`, () => { + expect(dedupe([])).toEqual([]); + }); }); /* In this kata, you will need to deduplicate the elements of an array From e6a2a7f40c777498e8397d0a907c0edb64c7fbd5 Mon Sep 17 00:00:00 2001 From: HM-127BTY Date: Mon, 2 Nov 2026 18:48:43 +0000 Subject: [PATCH 15/32] Added in tests and finished of task. --- Sprint-1/implement/dedupe.test.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Sprint-1/implement/dedupe.test.js b/Sprint-1/implement/dedupe.test.js index f207826e1..0b4852bd6 100644 --- a/Sprint-1/implement/dedupe.test.js +++ b/Sprint-1/implement/dedupe.test.js @@ -27,6 +27,11 @@ describe("deduplicate", () => { ]); }); + it("doesn't modify the input array [3, 1, 1, 2]", () => { + const array = [3, 1, 1, 2]; + dedupe(array); + expect(array).toEqual([3, 1, 1, 2]); + }); it(`returns the empty array when given an empty array`, () => { expect(dedupe([])).toEqual([]); }); From bf24000d1f213dfd49d35665d6de5d0d243e2281 Mon Sep 17 00:00:00 2001 From: HM-127BTY Date: Tue, 3 Nov 2026 11:05:46 +0000 Subject: [PATCH 16/32] Made the code required and done 2 tests sofar, one with single number and another with negative and positive numbers --- Sprint-1/implement/max.js | 3 +++ Sprint-1/implement/max.test.js | 6 ++++++ 2 files changed, 9 insertions(+) diff --git a/Sprint-1/implement/max.js b/Sprint-1/implement/max.js index 6dd76378e..a9108b411 100644 --- a/Sprint-1/implement/max.js +++ b/Sprint-1/implement/max.js @@ -1,4 +1,7 @@ function findMax(elements) { + const copyElements = Array.from(elements); + let sortElements = copyElements.sort((a, b) => a - b); + return sortElements.at(-1); } module.exports = findMax; diff --git a/Sprint-1/implement/max.test.js b/Sprint-1/implement/max.test.js index ff3fbb5e5..787859050 100644 --- a/Sprint-1/implement/max.test.js +++ b/Sprint-1/implement/max.test.js @@ -21,10 +21,16 @@ 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 median for [40]", () => { + expect(findMax([40])).toEqual(40); +}); // 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 median for [40]", () => { + expect(findMax([-40, 20, 30, -10, 40])).toEqual(40); +}); // Given an array with just negative numbers // When passed to the max function From b6fc9e2685c2bd489856abba2525293053b26799 Mon Sep 17 00:00:00 2001 From: HM-127BTY Date: Tue, 3 Nov 2026 11:18:40 +0000 Subject: [PATCH 17/32] Added more tests to look for median. --- Sprint-1/implement/max.test.js | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/Sprint-1/implement/max.test.js b/Sprint-1/implement/max.test.js index 787859050..a23caa3c8 100644 --- a/Sprint-1/implement/max.test.js +++ b/Sprint-1/implement/max.test.js @@ -28,17 +28,23 @@ it("returns the median for [40]", () => { // 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 median for [40]", () => { - expect(findMax([-40, 20, 30, -10, 40])).toEqual(40); +it("returns the median for [20, 30, 10, 40]", () => { + expect(findMax([20, 30, 10, 40])).toEqual(40); }); // 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 median for [-40, -20, -30, -70]", () => { + expect(findMax([-40, -20, -30, -70])).toEqual(-20); +}); // Given an array with decimal numbers // When passed to the max function // Then it should return the largest decimal number +it("returns the median for [-40.4, -20.7, -30.3, -70.6]", () => { + expect(findMax([-40.4, -20.7, -30.3, -70.6])).toEqual(-20.7); +}); // Given an array containing a value that isn't a number // When passed to the max function From 349d4214b09c006fb172c3a0600ec5f562466a2f Mon Sep 17 00:00:00 2001 From: HM-127BTY Date: Tue, 3 Nov 2026 11:38:30 +0000 Subject: [PATCH 18/32] added code and test for when array is empty to return Infinity --- Sprint-1/implement/max.js | 4 ++++ Sprint-1/implement/max.test.js | 3 +++ 2 files changed, 7 insertions(+) diff --git a/Sprint-1/implement/max.js b/Sprint-1/implement/max.js index a9108b411..6e395223e 100644 --- a/Sprint-1/implement/max.js +++ b/Sprint-1/implement/max.js @@ -1,5 +1,9 @@ function findMax(elements) { + if (elements.length === 0) { + return "Infinity"; + } const copyElements = Array.from(elements); + let sortElements = copyElements.sort((a, b) => a - b); return sortElements.at(-1); } diff --git a/Sprint-1/implement/max.test.js b/Sprint-1/implement/max.test.js index a23caa3c8..79f17d75e 100644 --- a/Sprint-1/implement/max.test.js +++ b/Sprint-1/implement/max.test.js @@ -17,6 +17,9 @@ const findMax = require("./max.js"); // Then it should return -Infinity // Delete this test.todo and replace it with a test. test.todo("given an empty array, returns -Infinity"); +it("returns the Infinity for []", () => { + expect(findMax([])).toEqual("Infinity"); +}); // Given an array with one number // When passed to the max function From 0eb9caf4664687a2fe05915445040d058cf2ec30 Mon Sep 17 00:00:00 2001 From: HM-127BTY Date: Tue, 3 Nov 2026 11:50:11 +0000 Subject: [PATCH 19/32] Added test and code to throw error when is not array and array not containing numbers. --- Sprint-1/implement/max.js | 10 +++++++++- Sprint-1/implement/max.test.js | 10 ++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/Sprint-1/implement/max.js b/Sprint-1/implement/max.js index 6e395223e..7559139ab 100644 --- a/Sprint-1/implement/max.js +++ b/Sprint-1/implement/max.js @@ -2,8 +2,16 @@ function findMax(elements) { if (elements.length === 0) { return "Infinity"; } - const copyElements = Array.from(elements); + if (!Array.isArray(elements)) { + throw new Error("findMax requires an array of numbers"); + } + const copyElements = Array.from(elements); + for (const item of copyElements) { + if (typeof item !== "number") { + throw new Error("findMax requires an array of numbers"); + } + } let sortElements = copyElements.sort((a, b) => a - b); return sortElements.at(-1); } diff --git a/Sprint-1/implement/max.test.js b/Sprint-1/implement/max.test.js index 79f17d75e..83ec711ee 100644 --- a/Sprint-1/implement/max.test.js +++ b/Sprint-1/implement/max.test.js @@ -52,7 +52,17 @@ it("returns the median for [-40.4, -20.7, -30.3, -70.6]", () => { // 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 error 'findMax requires an array of numbers' when containing a value that isn't a number", () => { + expect(() => findMax([10, 40, "hello", 30])).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 error 'findMax requires an array of numbers' Given something that isn't an array at all", () => { + expect(() => findMax(42)).toThrow( + new Error("findMax requires an array of numbers") + ); +}); From d48bbb7e5672f8a3a57775489e71c291d91f46b5 Mon Sep 17 00:00:00 2001 From: HM-127BTY Date: Tue, 3 Nov 2026 12:12:38 +0000 Subject: [PATCH 20/32] Built code and added first test to check for Mean of numbers in array. --- Sprint-1/implement/mean.js | 9 ++++++++- Sprint-1/implement/mean.test.js | 4 +++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/Sprint-1/implement/mean.js b/Sprint-1/implement/mean.js index 36909ea1f..1e2696087 100644 --- a/Sprint-1/implement/mean.js +++ b/Sprint-1/implement/mean.js @@ -1,3 +1,10 @@ -function calculateMean(list) {} +function calculateMean(list) { + const copyList = Array.from(list); + let total = 0; + for (let i = 0; i < copyList.length; i++) { + total += copyList[i]; + } + return total / copyList.length; +} module.exports = calculateMean; diff --git a/Sprint-1/implement/mean.test.js b/Sprint-1/implement/mean.test.js index 847b88676..06008402a 100644 --- a/Sprint-1/implement/mean.test.js +++ b/Sprint-1/implement/mean.test.js @@ -23,7 +23,9 @@ 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"); +it("returns the Infinity for []", () => { + expect(calculateMean([1, 2, 6])).toEqual(3); +}); // Given an array with a single number // When passed to calculateMean From 5090836792b2fa83e9d1dbfa11891f8daf2b6df8 Mon Sep 17 00:00:00 2001 From: HM-127BTY Date: Tue, 3 Nov 2026 12:14:31 +0000 Subject: [PATCH 21/32] fixed errors and removed todo. added test for single number in rray --- Sprint-1/implement/mean.test.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Sprint-1/implement/mean.test.js b/Sprint-1/implement/mean.test.js index 06008402a..40fccb494 100644 --- a/Sprint-1/implement/mean.test.js +++ b/Sprint-1/implement/mean.test.js @@ -23,13 +23,16 @@ 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. -it("returns the Infinity for []", () => { +it("Returns correct value of 3 for mean of numbers in array of [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 +it("returns mean of single number in array", () => { + expect(calculateMean([6])).toEqual(6); +}); // Given an array containing negative or decimal numbers // When passed to calculateMean From 6f6333db1a695e2064b97119be2c5794aa9ce99e Mon Sep 17 00:00:00 2001 From: HM-127BTY Date: Tue, 3 Nov 2026 12:20:56 +0000 Subject: [PATCH 22/32] added test and code for empty array --- Sprint-1/implement/mean.js | 4 ++++ Sprint-1/implement/mean.test.js | 9 ++++++++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/Sprint-1/implement/mean.js b/Sprint-1/implement/mean.js index 1e2696087..7db6b3e3e 100644 --- a/Sprint-1/implement/mean.js +++ b/Sprint-1/implement/mean.js @@ -1,4 +1,8 @@ function calculateMean(list) { + if (list.length === 0) { + throw new Error("calculateMean requires a non-empty array"); + } + const copyList = Array.from(list); let total = 0; for (let i = 0; i < copyList.length; i++) { diff --git a/Sprint-1/implement/mean.test.js b/Sprint-1/implement/mean.test.js index 40fccb494..15c419c21 100644 --- a/Sprint-1/implement/mean.test.js +++ b/Sprint-1/implement/mean.test.js @@ -37,11 +37,18 @@ it("returns mean of single number in array", () => { // Given an array containing negative or decimal numbers // When passed to calculateMean // Then it should return the correct mean +it("returns mean containing negative or decimal numbers in array", () => { + expect(calculateMean([-6, 8.5, 23])).toEqual(8.5); +}); // Given an empty array // When passed to calculateMean // Then it should throw Error("calculateMean requires a non-empty array") - +it("Throws error 'calculateMean requires a non-empty array' Given empty", () => { + 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") From 3cfe5756dd7ac3df1945c990caf8aed2d53e1f79 Mon Sep 17 00:00:00 2001 From: HM-127BTY Date: Tue, 3 Nov 2026 12:26:29 +0000 Subject: [PATCH 23/32] Added code and test for thrown error for when not array --- Sprint-1/implement/mean.js | 4 ++++ Sprint-1/implement/mean.test.js | 7 +++++++ 2 files changed, 11 insertions(+) diff --git a/Sprint-1/implement/mean.js b/Sprint-1/implement/mean.js index 7db6b3e3e..a48136c6e 100644 --- a/Sprint-1/implement/mean.js +++ b/Sprint-1/implement/mean.js @@ -3,6 +3,10 @@ function calculateMean(list) { throw new Error("calculateMean requires a non-empty array"); } + if (!Array.isArray(list)) { + throw new Error("calculateMean requires an array of numbers"); + } + const copyList = Array.from(list); let total = 0; for (let i = 0; i < copyList.length; i++) { diff --git a/Sprint-1/implement/mean.test.js b/Sprint-1/implement/mean.test.js index 15c419c21..6dfe983bc 100644 --- a/Sprint-1/implement/mean.test.js +++ b/Sprint-1/implement/mean.test.js @@ -52,6 +52,13 @@ it("Throws error 'calculateMean requires a non-empty array' Given empty", () => // 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 error 'calculateMean requires an array of numbers' Given value that isn't an array", () => { + expect(() => + calculateMean("Hello bob").toThrow( + new Error("calculateMean requires an array of numbers") + ) + ); +}); // Given no argument at all // When passed to calculateMean From 7ddb8cbd2a67a463c1fbd2179b01071ded8adf5d Mon Sep 17 00:00:00 2001 From: HM-127BTY Date: Tue, 3 Nov 2026 12:39:27 +0000 Subject: [PATCH 24/32] editing test order as showing in Acceptance Criteria. was not able to make commit before this save. --- Sprint-1/implement/dedupe.test.js | 75 +++++++++++++++---------------- Sprint-1/implement/max.test.js | 1 - Sprint-1/implement/mean.test.js | 15 +++++++ 3 files changed, 51 insertions(+), 40 deletions(-) diff --git a/Sprint-1/implement/dedupe.test.js b/Sprint-1/implement/dedupe.test.js index 0b4852bd6..cc3805a97 100644 --- a/Sprint-1/implement/dedupe.test.js +++ b/Sprint-1/implement/dedupe.test.js @@ -4,39 +4,7 @@ Dedupe Array 📖 Dedupe means **deduplicate** */ describe("deduplicate", () => { - it(`returns the deduplicate for string letters`, () => { - expect(dedupe(["a", "a", "a", "b", "b", "c"])).toEqual(["a", "b", "c"]); - }); - - it(`returns the deduplicate for numbers`, () => { - expect(dedupe([5, 1, 1, 2, 3, 2, 5, 8])).toEqual([5, 1, 2, 3, 8]); - }); - - it(`returns the deduplicate while not numerical in order`, () => { - expect(dedupe([1, 2, 1])).toEqual([1, 2]); - }); - - it(`returns the deduplicate for string and numbers `, () => { - expect(dedupe([5, 1, 1, 2, 3, "a", "a", "a", "c"])).toEqual([ - 5, - 1, - 2, - 3, - "a", - "c", - ]); - }); - - it("doesn't modify the input array [3, 1, 1, 2]", () => { - const array = [3, 1, 1, 2]; - dedupe(array); - expect(array).toEqual([3, 1, 1, 2]); - }); - it(`returns the empty array when given an empty array`, () => { - expect(dedupe([])).toEqual([]); - }); -}); -/* + /* 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'] @@ -44,18 +12,47 @@ 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] */ -// Acceptance Criteria: - -// 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"); + // Acceptance Criteria: + // Given an empty array + // When passed to the dedupe function + // Then it should return an empty array + it(`returns the empty array when given 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 +it("doesn't modify the input array [3, 1, 1, 2]", () => { + const array = [3, 1, 1, 2]; + dedupe(array); + expect(array).toEqual([3, 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(`returns the deduplicate for string letters`, () => { + expect(dedupe(["a", "a", "a", "b", "b", "c"])).toEqual(["a", "b", "c"]); +}); + +it(`returns the deduplicate for numbers`, () => { + expect(dedupe([5, 1, 1, 2, 3, 2, 5, 8])).toEqual([5, 1, 2, 3, 8]); +}); + +it(`returns the deduplicate while not numerical in order`, () => { + expect(dedupe([1, 2, 1])).toEqual([1, 2]); +}); + +it(`returns the deduplicate for string and numbers `, () => { + expect(dedupe([5, 1, 1, 2, 3, "a", "a", "a", "c"])).toEqual([ + 5, + 1, + 2, + 3, + "a", + "c", + ]); +}); diff --git a/Sprint-1/implement/max.test.js b/Sprint-1/implement/max.test.js index 83ec711ee..fd966d067 100644 --- a/Sprint-1/implement/max.test.js +++ b/Sprint-1/implement/max.test.js @@ -16,7 +16,6 @@ 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"); it("returns the Infinity for []", () => { expect(findMax([])).toEqual("Infinity"); }); diff --git a/Sprint-1/implement/mean.test.js b/Sprint-1/implement/mean.test.js index 6dfe983bc..45783bac0 100644 --- a/Sprint-1/implement/mean.test.js +++ b/Sprint-1/implement/mean.test.js @@ -49,6 +49,7 @@ it("Throws error 'calculateMean requires a non-empty array' Given empty", () => 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") @@ -63,7 +64,21 @@ it("Throws error 'calculateMean requires an array of numbers' Given value that i // Given no argument at all // When passed to calculateMean // Then it should throw Error("calculateMean requires an array of numbers") +it("Throws error 'calculateMean requires an array of numbers' Given 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 error 'calculateMean requires an array of numbers' an array containing a non-number value, e.g. [1, '2', 3]", () => { + expect(() => + calculateMean([1, "2", 3]).toThrow( + new Error("calculateMean requires an array of numbers") + ) + ); +}); From 92cea6aa95944aff9ff03bf7396a16c2321e205a Mon Sep 17 00:00:00 2001 From: HM-127BTY Date: Tue, 3 Nov 2026 15:06:04 +0000 Subject: [PATCH 25/32] Added in describeMedian to the code and first test. --- Sprint-1/implement/describe-median.js | 4 +++- Sprint-1/implement/describe-median.test.js | 3 +++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/Sprint-1/implement/describe-median.js b/Sprint-1/implement/describe-median.js index 3381cfc0a..8659350fc 100644 --- a/Sprint-1/implement/describe-median.js +++ b/Sprint-1/implement/describe-median.js @@ -20,6 +20,8 @@ function calculateMedian(list) { } // Implement this function. See describe-median.test.js for the acceptance criteria. -function describeMedian(list) {} +function describeMedian(list) { + return `The median is ${calculateMedian(list)}`; +} module.exports = describeMedian; diff --git a/Sprint-1/implement/describe-median.test.js b/Sprint-1/implement/describe-median.test.js index e7d18a264..96eefc59c 100644 --- a/Sprint-1/implement/describe-median.test.js +++ b/Sprint-1/implement/describe-median.test.js @@ -22,6 +22,9 @@ 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. test.todo('given [1, 2, 3], returns "The median is 2"'); +it("returns 'The median is 2'", () => { + expect(describeMedian([1, 2, 3])).toEqual("The median is 2"); +}); // Given an empty array // When passed to describeMedian From 59cd4420ddcbf44fed471ada6aa99bca191458c4 Mon Sep 17 00:00:00 2001 From: HM-127BTY Date: Tue, 3 Nov 2026 15:20:05 +0000 Subject: [PATCH 26/32] added thrown error in code and test for empty array --- Sprint-1/implement/describe-median.js | 5 +++++ Sprint-1/implement/describe-median.test.js | 7 +++++++ 2 files changed, 12 insertions(+) diff --git a/Sprint-1/implement/describe-median.js b/Sprint-1/implement/describe-median.js index 8659350fc..5a9d1a560 100644 --- a/Sprint-1/implement/describe-median.js +++ b/Sprint-1/implement/describe-median.js @@ -21,6 +21,11 @@ function calculateMedian(list) { // Implement this function. See describe-median.test.js for the acceptance criteria. function describeMedian(list) { + if (list.length === 0) { + throw new Error( + "Could not calculate a median: calculateMedian requires a non-empty array" + ); + } return `The median is ${calculateMedian(list)}`; } diff --git a/Sprint-1/implement/describe-median.test.js b/Sprint-1/implement/describe-median.test.js index 96eefc59c..97833d84a 100644 --- a/Sprint-1/implement/describe-median.test.js +++ b/Sprint-1/implement/describe-median.test.js @@ -29,6 +29,13 @@ it("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" +it("Throws error 'Could not calculate a median: calculateMedian requires a non-empty array' when array empty", () => { + 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 From 904a8aecc82221287b1e1622526057fce5729bc9 Mon Sep 17 00:00:00 2001 From: HM-127BTY Date: Tue, 3 Nov 2026 16:16:38 +0000 Subject: [PATCH 27/32] Finished adding in the catch part for the tests and in teh code. --- Sprint-1/implement/describe-median.js | 9 ++++----- Sprint-1/implement/describe-median.test.js | 14 ++++++++------ 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/Sprint-1/implement/describe-median.js b/Sprint-1/implement/describe-median.js index 5a9d1a560..c9ce2385a 100644 --- a/Sprint-1/implement/describe-median.js +++ b/Sprint-1/implement/describe-median.js @@ -21,12 +21,11 @@ function calculateMedian(list) { // Implement this function. See describe-median.test.js for the acceptance criteria. function describeMedian(list) { - if (list.length === 0) { - throw new Error( - "Could not calculate a median: calculateMedian requires a non-empty array" - ); + try { + return `The median is ${calculateMedian(list)}`; + } catch (error) { + return `Could not calculate a median: ${error.message}`; } - return `The median is ${calculateMedian(list)}`; } module.exports = describeMedian; diff --git a/Sprint-1/implement/describe-median.test.js b/Sprint-1/implement/describe-median.test.js index 97833d84a..52540b428 100644 --- a/Sprint-1/implement/describe-median.test.js +++ b/Sprint-1/implement/describe-median.test.js @@ -21,7 +21,6 @@ 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"'); it("returns 'The median is 2'", () => { expect(describeMedian([1, 2, 3])).toEqual("The median is 2"); }); @@ -29,14 +28,17 @@ it("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" -it("Throws error 'Could not calculate a median: calculateMedian requires a non-empty array' when array empty", () => { - expect(() => describeMedian([])).toThrow( - new Error( - "Could not calculate a median: calculateMedian requires a non-empty array" - ) +it("returns error 'Could not calculate a median: calculateMedian requires a non-empty array' when array empty", () => { + 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("Throws error 'calculateMedian requires an array of numbers' when it isn't an array of numbers", () => { + expect(describeMedian(["banana"])).toEqual( + "Could not calculate a median: calculateMedian requires an array of numbers" + ); +}); From 9977484caf0e2c7b14d1c587f33f6b20a955d6e5 Mon Sep 17 00:00:00 2001 From: HM-127BTY Date: Tue, 3 Nov 2026 16:24:10 +0000 Subject: [PATCH 28/32] Built code for sum, was able to copy paste from past tasks in this sprint and adjust for the code here. --- Sprint-1/implement/sum.js | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/Sprint-1/implement/sum.js b/Sprint-1/implement/sum.js index 9062aafe3..c757369ef 100644 --- a/Sprint-1/implement/sum.js +++ b/Sprint-1/implement/sum.js @@ -1,4 +1,22 @@ function sum(elements) { + if (elements.length === 0) { + return 0; + } + 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 copyElements = Array.from(elements); + let total = 0; + for (let i = 0; i < copyElements.length; i++) { + total += copyElements[i]; + } + return total; + } } module.exports = sum; From c4c5fbc347d335e8f354b66d4aebd94527d6b77e Mon Sep 17 00:00:00 2001 From: HM-127BTY Date: Tue, 3 Nov 2026 16:27:33 +0000 Subject: [PATCH 29/32] added in some codes to work with the sum of numbers. --- Sprint-1/implement/sum.test.js | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/Sprint-1/implement/sum.test.js b/Sprint-1/implement/sum.test.js index a288e0f0a..c91c5852d 100644 --- a/Sprint-1/implement/sum.test.js +++ b/Sprint-1/implement/sum.test.js @@ -13,15 +13,23 @@ 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") +it("returns 0 when given an empty array", () => { + 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 same number when given an array with just one number", () => { + expect(sum([5])).toEqual(5); +}); // Given an array containing negative numbers // When passed to the sum function // Then it should still return the correct total sum +it("returns total when given an array with negative numbers", () => { + expect(sum([-5, -2, -10])).toEqual(-17); +}); // Given an array with decimal/float numbers // When passed to the sum function From 9d8c1e2d3000b1f8ecc84d6062bbcc93accaf560 Mon Sep 17 00:00:00 2001 From: HM-127BTY Date: Tue, 3 Nov 2026 16:35:05 +0000 Subject: [PATCH 30/32] added in test and code for decimals up to 2 decimals. --- Sprint-1/implement/sum.js | 6 +++++- Sprint-1/implement/sum.test.js | 3 +++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/Sprint-1/implement/sum.js b/Sprint-1/implement/sum.js index c757369ef..64fcb4441 100644 --- a/Sprint-1/implement/sum.js +++ b/Sprint-1/implement/sum.js @@ -15,7 +15,11 @@ function sum(elements) { for (let i = 0; i < copyElements.length; i++) { total += copyElements[i]; } - return total; + if (Number.isInteger(total)) { + return total; + } else { + return Number(total.toFixed(2)); + } } } diff --git a/Sprint-1/implement/sum.test.js b/Sprint-1/implement/sum.test.js index c91c5852d..8673c05ba 100644 --- a/Sprint-1/implement/sum.test.js +++ b/Sprint-1/implement/sum.test.js @@ -34,6 +34,9 @@ it("returns total when given an array with negative numbers", () => { // Given an array with decimal/float numbers // When passed to the sum function // Then it should return the correct total sum +it("returns total when given an array with decimal/float numbers", () => { + expect(sum([2.6, 4.6, 8.12])).toEqual(15.32); +}); // Given an array containing a value that isn't a number // When passed to the sum function From 31b226e422cb63a39edef200eac519f9aa776ab1 Mon Sep 17 00:00:00 2001 From: HM-127BTY Date: Tue, 3 Nov 2026 16:40:17 +0000 Subject: [PATCH 31/32] added in thrown error for value that isn't a number --- Sprint-1/implement/sum.test.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Sprint-1/implement/sum.test.js b/Sprint-1/implement/sum.test.js index 8673c05ba..f25a80cce 100644 --- a/Sprint-1/implement/sum.test.js +++ b/Sprint-1/implement/sum.test.js @@ -41,6 +41,11 @@ it("returns total when given an array with decimal/float 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 an error 'sum requires an array of numbers' when value isn't a number", () => { + expect(() => sum(["hello", "bob"])).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 From cfe837f300ec6c90c6e8e8d6b48509ff407073db Mon Sep 17 00:00:00 2001 From: HM-127BTY Date: Tue, 3 Nov 2026 16:41:31 +0000 Subject: [PATCH 32/32] added in thrown error test for no array --- Sprint-1/implement/sum.test.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/Sprint-1/implement/sum.test.js b/Sprint-1/implement/sum.test.js index f25a80cce..e0fd7cfc1 100644 --- a/Sprint-1/implement/sum.test.js +++ b/Sprint-1/implement/sum.test.js @@ -42,7 +42,7 @@ it("returns total when given an array with decimal/float numbers", () => { // When passed to the sum function // Then it should throw Error("sum requires an array of numbers") it("throws an error 'sum requires an array of numbers' when value isn't a number", () => { - expect(() => sum(["hello", "bob"])).toThrow( + expect(()=> sum(["hello", "bob"])).toThrow( new Error("sum requires an array of numbers") ); }); @@ -50,3 +50,8 @@ it("throws an error 'sum requires an array of numbers' when value isn't a number // 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 an error 'sum requires an array of numbers' when value isn't an array", () => { + expect(() => sum(["2", 2, 5, "bob"])).toThrow( + new Error("sum requires an array of numbers") + ); +});