From d1362cd3100d498de8990bff755ff35b4bf4cb06 Mon Sep 17 00:00:00 2001 From: Hanna Bohlin Date: Fri, 30 Oct 2026 16:46:34 +0000 Subject: [PATCH 01/37] Refactor calculateMedian so even length lists give correct median --- Sprint-1/fix/median.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index 5c5b796e1..14bf431a2 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -7,6 +7,11 @@ function calculateMedian(list) { const middleIndex = Math.floor(list.length / 2); + + if (list.length % 2 === 0){ + return (list[middleIndex -1] + list[middleIndex])/2; + } + const median = list.splice(middleIndex, 1)[0]; return median; } From ca7d11eb47a86b42c81df558c20eed85e23ba1b6 Mon Sep 17 00:00:00 2001 From: Hanna Bohlin Date: Fri, 30 Oct 2026 17:04:55 +0000 Subject: [PATCH 02/37] Sorted the list in a non mutating way before finding middle value --- Sprint-1/fix/median.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index 14bf431a2..6cde6cd6c 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -6,13 +6,16 @@ // or contains values that aren't numbers (the function is expected to throw - see the tests). function calculateMedian(list) { + + const sortedList = list.toSorted(); + const middleIndex = Math.floor(list.length / 2); if (list.length % 2 === 0){ - return (list[middleIndex -1] + list[middleIndex])/2; + return (sortedList[middleIndex -1] + sortedList[middleIndex])/2; } - const median = list.splice(middleIndex, 1)[0]; + const median = sortedList.splice(middleIndex, 1)[0]; return median; } From c5a3e4ee00fa59116894a50c451383acfc19f67c Mon Sep 17 00:00:00 2001 From: Hanna Bohlin Date: Sat, 31 Oct 2026 15:14:34 +0000 Subject: [PATCH 03/37] Add sorting function to toSorted so it sorts ascending --- Sprint-1/fix/median.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index 6cde6cd6c..9cd843a31 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -7,7 +7,7 @@ function calculateMedian(list) { - const sortedList = list.toSorted(); + const sortedList = list.toSorted((a,b) => a - b); const middleIndex = Math.floor(list.length / 2); @@ -19,4 +19,6 @@ function calculateMedian(list) { return median; } +calculateMedian([110, 20, 0]); + module.exports = calculateMedian; From 882c3a9716ba97fff099700d10a0be501afebf6a Mon Sep 17 00:00:00 2001 From: Hanna Bohlin Date: Sat, 31 Oct 2026 15:16:19 +0000 Subject: [PATCH 04/37] Removing slice to not mutate array and refactor return statement --- Sprint-1/fix/median.js | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index 9cd843a31..4090abec8 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -15,10 +15,7 @@ function calculateMedian(list) { return (sortedList[middleIndex -1] + sortedList[middleIndex])/2; } - const median = sortedList.splice(middleIndex, 1)[0]; - return median; + return sortedList[middleIndex]; } -calculateMedian([110, 20, 0]); - module.exports = calculateMedian; From 5fa46eca64235baf86bbcb3b9de7c17b5b6ec019 Mon Sep 17 00:00:00 2001 From: Hanna Bohlin Date: Sat, 31 Oct 2026 15:20:56 +0000 Subject: [PATCH 05/37] Now throws when list is empty array --- Sprint-1/fix/median.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index 4090abec8..5cb91c0cd 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -7,6 +7,10 @@ function calculateMedian(list) { + if (list.length === 0){ + throw new Error("calculateMedian requires a non-empty array"); + } + const sortedList = list.toSorted((a,b) => a - b); const middleIndex = Math.floor(list.length / 2); From 88ea97f07632d0e2201e6c18cdd190dbbeaaf364 Mon Sep 17 00:00:00 2001 From: Hanna Bohlin Date: Sat, 31 Oct 2026 15:22:29 +0000 Subject: [PATCH 06/37] Now throws when list is a single string --- Sprint-1/fix/median.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index 5cb91c0cd..7f18109bc 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -9,6 +9,10 @@ function calculateMedian(list) { if (list.length === 0){ throw new Error("calculateMedian requires a non-empty array"); + }; + + if (typeof list === "string"){ + throw new Error("calculateMedian requires an array of numbers"); } const sortedList = list.toSorted((a,b) => a - b); From 474445f95b22c19a6d2c0c24de8321e1a94174fc Mon Sep 17 00:00:00 2001 From: Hanna Bohlin Date: Sat, 31 Oct 2026 15:23:59 +0000 Subject: [PATCH 07/37] Throws when list is a number --- Sprint-1/fix/median.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index 7f18109bc..283be3006 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -13,6 +13,10 @@ function calculateMedian(list) { if (typeof list === "string"){ throw new Error("calculateMedian requires an array of numbers"); + }; + + if (typeof list === "number"){ + throw new Error("calculateMedian requires an array of numbers"); } const sortedList = list.toSorted((a,b) => a - b); From 73722de986a389f2ef389f266241bc84cbef6bdc Mon Sep 17 00:00:00 2001 From: Hanna Bohlin Date: Sat, 31 Oct 2026 15:27:14 +0000 Subject: [PATCH 08/37] Throws when list is null --- Sprint-1/fix/median.js | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index 283be3006..8a04b41cc 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -6,25 +6,28 @@ // or contains values that aren't numbers (the function is expected to throw - see the tests). function calculateMedian(list) { + if (list === null) { + throw new Error("calculateMedian requires an array of numbers"); + } - if (list.length === 0){ + if (list.length === 0) { throw new Error("calculateMedian requires a non-empty array"); - }; + } - if (typeof list === "string"){ + if (typeof list === "string") { throw new Error("calculateMedian requires an array of numbers"); - }; + } - if (typeof list === "number"){ + if (typeof list === "number") { throw new Error("calculateMedian requires an array of numbers"); } - const sortedList = list.toSorted((a,b) => a - b); + const sortedList = list.toSorted((a, b) => a - b); const middleIndex = Math.floor(list.length / 2); - if (list.length % 2 === 0){ - return (sortedList[middleIndex -1] + sortedList[middleIndex])/2; + if (list.length % 2 === 0) { + return (sortedList[middleIndex - 1] + sortedList[middleIndex]) / 2; } return sortedList[middleIndex]; From 5d200175cab5813a0a517f9f86554083bd08be70 Mon Sep 17 00:00:00 2001 From: Hanna Bohlin Date: Sat, 31 Oct 2026 15:31:43 +0000 Subject: [PATCH 09/37] Throws when list is object --- Sprint-1/fix/median.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index 8a04b41cc..617e7251f 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -22,6 +22,10 @@ function calculateMedian(list) { throw new Error("calculateMedian requires an array of numbers"); } + if (typeof list === "object" && !Array.isArray(list)){ + throw new Error("calculateMedian requires an array of numbers"); + }; + const sortedList = list.toSorted((a, b) => a - b); const middleIndex = Math.floor(list.length / 2); From 9843b6bd4337c191fbbcd0ee2510aa06e85b11ec Mon Sep 17 00:00:00 2001 From: Hanna Bohlin Date: Sat, 31 Oct 2026 15:37:24 +0000 Subject: [PATCH 10/37] Set parameter to default value making it throw if attribute isn't specified --- Sprint-1/fix/median.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index 617e7251f..35a50055b 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -5,7 +5,7 @@ // 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) { +function calculateMedian(list=0) { if (list === null) { throw new Error("calculateMedian requires an array of numbers"); } From d2a28a915d2327dd5f9551d052111ecbc9aa28ab Mon Sep 17 00:00:00 2001 From: Hanna Bohlin Date: Sat, 31 Oct 2026 16:16:31 +0000 Subject: [PATCH 11/37] Throws when list contains any strings --- Sprint-1/fix/median.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index 35a50055b..66d760799 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -26,6 +26,12 @@ function calculateMedian(list=0) { throw new Error("calculateMedian requires an array of numbers"); }; + for (item of list){ + if (typeof item === "string"){ + throw new Error("calculateMedian requires an array of numbers"); + } + } + const sortedList = list.toSorted((a, b) => a - b); const middleIndex = Math.floor(list.length / 2); From 07073aa2706922fb1c43c1e037c99084682d8123 Mon Sep 17 00:00:00 2001 From: Hanna Bohlin Date: Sat, 31 Oct 2026 16:27:31 +0000 Subject: [PATCH 12/37] Throws when list contains null --- Sprint-1/fix/median.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index 66d760799..4c4c10021 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -27,7 +27,7 @@ function calculateMedian(list=0) { }; for (item of list){ - if (typeof item === "string"){ + if (typeof item === "string" || item === null){ throw new Error("calculateMedian requires an array of numbers"); } } From fe7960bf5aef710504daa59fbb4187659c2663d1 Mon Sep 17 00:00:00 2001 From: Hanna Bohlin Date: Sat, 31 Oct 2026 16:32:01 +0000 Subject: [PATCH 13/37] Throws when list containes undefined value --- Sprint-1/fix/median.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index 4c4c10021..6d721d904 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -27,7 +27,7 @@ function calculateMedian(list=0) { }; for (item of list){ - if (typeof item === "string" || item === null){ + if (typeof item === "string" || item === null || item === undefined){ throw new Error("calculateMedian requires an array of numbers"); } } From 995d91e660f7f01aea3621b6766293b43cc35bd5 Mon Sep 17 00:00:00 2001 From: Hanna Bohlin Date: Tue, 3 Nov 2026 09:24:21 +0000 Subject: [PATCH 14/37] Returns -infinity for empty array passed --- Sprint-1/implement/max.js | 3 +++ Sprint-1/implement/max.test.js | 4 +++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/Sprint-1/implement/max.js b/Sprint-1/implement/max.js index 6dd76378e..d5856ff62 100644 --- a/Sprint-1/implement/max.js +++ b/Sprint-1/implement/max.js @@ -1,4 +1,7 @@ function findMax(elements) { + if (elements.length === 0){ + return -Infinity; + } } module.exports = findMax; diff --git a/Sprint-1/implement/max.test.js b/Sprint-1/implement/max.test.js index ff3fbb5e5..c13efcc29 100644 --- a/Sprint-1/implement/max.test.js +++ b/Sprint-1/implement/max.test.js @@ -16,7 +16,9 @@ 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("given an empty array, returns -Infinity", () => { + expect(findMax([])).toEqual(-Infinity); +}); // Given an array with one number // When passed to the max function From 74383eace6f160f5fe61f285a257809f84bfc247 Mon Sep 17 00:00:00 2001 From: Hanna Bohlin Date: Tue, 3 Nov 2026 09:27:08 +0000 Subject: [PATCH 15/37] Returns solo number passed in array --- Sprint-1/implement/max.js | 2 ++ Sprint-1/implement/max.test.js | 3 +++ 2 files changed, 5 insertions(+) diff --git a/Sprint-1/implement/max.js b/Sprint-1/implement/max.js index d5856ff62..e4040c9fd 100644 --- a/Sprint-1/implement/max.js +++ b/Sprint-1/implement/max.js @@ -2,6 +2,8 @@ function findMax(elements) { if (elements.length === 0){ return -Infinity; } + + return elements[0]; } module.exports = findMax; diff --git a/Sprint-1/implement/max.test.js b/Sprint-1/implement/max.test.js index c13efcc29..8cc4ec60b 100644 --- a/Sprint-1/implement/max.test.js +++ b/Sprint-1/implement/max.test.js @@ -23,6 +23,9 @@ it("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("Given array with one number should return that number", () => { + expect(findMax([3])).toEqual(3); +}) // Given an array with both positive and negative numbers // When passed to the max function From 0a5abe53737ad74a6b51ae2078e946aba5859c32 Mon Sep 17 00:00:00 2001 From: Hanna Bohlin Date: Tue, 3 Nov 2026 09:34:17 +0000 Subject: [PATCH 16/37] Returns the biggest value in an array regardless of negative or decimal values present --- Sprint-1/implement/max.js | 10 +++++++++- Sprint-1/implement/max.test.js | 11 ++++++++++- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/Sprint-1/implement/max.js b/Sprint-1/implement/max.js index e4040c9fd..2694f1031 100644 --- a/Sprint-1/implement/max.js +++ b/Sprint-1/implement/max.js @@ -3,7 +3,15 @@ function findMax(elements) { return -Infinity; } - return elements[0]; + let max = elements[0]; + + for (element of elements){ + if (element > max){ + max = element; + } + } + + return max; } module.exports = findMax; diff --git a/Sprint-1/implement/max.test.js b/Sprint-1/implement/max.test.js index 8cc4ec60b..d902c80fc 100644 --- a/Sprint-1/implement/max.test.js +++ b/Sprint-1/implement/max.test.js @@ -25,19 +25,28 @@ it("given an empty array, returns -Infinity", () => { // Then it should return that number it("Given array with one number should return that number", () => { expect(findMax([3])).toEqual(3); -}) +}); // Given an array with both positive and negative numbers // When passed to the max function // Then it should return the largest number overall +it("Given array of positive and negative values returns the biggest", () => { + expect(findMax([4,-7,47,-90])).toEqual(47); +}); // Given an array with just negative numbers // When passed to the max function // Then it should return the closest one to zero +it("Given array of negative values returns the closest to zero", () => { + expect(findMax([-4, -7, -47, -90])).toEqual(-4); +}); // Given an array with decimal numbers // When passed to the max function // Then it should return the largest decimal number +it("Given array of positive and negative values returns the biggest", () => { + expect(findMax([4.3, -7.83, 47.134, -90.2947])).toEqual(47.134); +}); // Given an array containing a value that isn't a number // When passed to the max function From 9568c857e2a594bb729aa2698e9b6817879143fa Mon Sep 17 00:00:00 2001 From: Hanna Bohlin Date: Tue, 3 Nov 2026 09:38:58 +0000 Subject: [PATCH 17/37] Throws when non number part of the array --- Sprint-1/implement/max.js | 3 +++ Sprint-1/implement/max.test.js | 5 +++++ 2 files changed, 8 insertions(+) diff --git a/Sprint-1/implement/max.js b/Sprint-1/implement/max.js index 2694f1031..93b83d024 100644 --- a/Sprint-1/implement/max.js +++ b/Sprint-1/implement/max.js @@ -6,6 +6,9 @@ function findMax(elements) { let max = elements[0]; for (element of elements){ + if (typeof element != "number"){ + throw new Error("findMax requires an array of numbers"); + } if (element > max){ max = element; } diff --git a/Sprint-1/implement/max.test.js b/Sprint-1/implement/max.test.js index d902c80fc..6a72ef721 100644 --- a/Sprint-1/implement/max.test.js +++ b/Sprint-1/implement/max.test.js @@ -51,6 +51,11 @@ it("Given array of positive and negative values returns the biggest", () => { // 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("Given array with value that isn't a number, should throw", () => { + expect(() => { + findMax([5, "ooo", 203]); + }).toThrow("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 From bae2a3bd5accb4b64fdc1e4ed8f28ad480188523 Mon Sep 17 00:00:00 2001 From: Hanna Bohlin Date: Tue, 3 Nov 2026 09:44:07 +0000 Subject: [PATCH 18/37] Throws when passes non array --- Sprint-1/implement/max.js | 8 ++++++-- Sprint-1/implement/max.test.js | 11 +++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/Sprint-1/implement/max.js b/Sprint-1/implement/max.js index 93b83d024..f25b88b96 100644 --- a/Sprint-1/implement/max.js +++ b/Sprint-1/implement/max.js @@ -1,7 +1,11 @@ function findMax(elements) { + if (!Array.isArray(elements)){ + throw new Error("findMax requires an array of numbers"); + }; + if (elements.length === 0){ return -Infinity; - } + }; let max = elements[0]; @@ -12,7 +16,7 @@ function findMax(elements) { if (element > max){ max = element; } - } + }; return max; } diff --git a/Sprint-1/implement/max.test.js b/Sprint-1/implement/max.test.js index 6a72ef721..97fc8407d 100644 --- a/Sprint-1/implement/max.test.js +++ b/Sprint-1/implement/max.test.js @@ -60,3 +60,14 @@ it("Given array with value that isn't a number, should throw", () => { // 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("Given argument that isn't an array, should throw", () => { + expect(() => { + findMax("hey"); + }).toThrow("findMax requires an array of numbers"); + expect(() => { + findMax(42); + }).toThrow("findMax requires an array of numbers"); + expect(() => { + findMax(); + }).toThrow("findMax requires an array of numbers"); +}); \ No newline at end of file From 1147d90bea1c5f956b6337e0f3b78aa86cd80c17 Mon Sep 17 00:00:00 2001 From: Hanna Bohlin Date: Tue, 3 Nov 2026 09:48:15 +0000 Subject: [PATCH 19/37] Returns 0 when passed empty array --- Sprint-1/implement/sum.js | 2 ++ Sprint-1/implement/sum.test.js | 4 +++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/Sprint-1/implement/sum.js b/Sprint-1/implement/sum.js index 9062aafe3..70ebcbb8f 100644 --- a/Sprint-1/implement/sum.js +++ b/Sprint-1/implement/sum.js @@ -1,4 +1,6 @@ function sum(elements) { + let theSum = 0; + return theSum; } module.exports = sum; diff --git a/Sprint-1/implement/sum.test.js b/Sprint-1/implement/sum.test.js index a288e0f0a..001f9da1b 100644 --- a/Sprint-1/implement/sum.test.js +++ b/Sprint-1/implement/sum.test.js @@ -13,7 +13,9 @@ 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("given an empty array, returns 0", () => { + expect(sum([])).toEqual(0); +}); // Given an array with just one number // When passed to the sum function From c0907179d16f143cdae2451c94faf695799bff55 Mon Sep 17 00:00:00 2001 From: Hanna Bohlin Date: Tue, 3 Nov 2026 09:51:24 +0000 Subject: [PATCH 20/37] Returns the number when passed array with single number --- Sprint-1/implement/sum.js | 5 +++++ Sprint-1/implement/sum.test.js | 3 +++ 2 files changed, 8 insertions(+) diff --git a/Sprint-1/implement/sum.js b/Sprint-1/implement/sum.js index 70ebcbb8f..d2419e5e4 100644 --- a/Sprint-1/implement/sum.js +++ b/Sprint-1/implement/sum.js @@ -1,5 +1,10 @@ function sum(elements) { let theSum = 0; + + for (element of elements){ + theSum = element; + } + return theSum; } diff --git a/Sprint-1/implement/sum.test.js b/Sprint-1/implement/sum.test.js index 001f9da1b..0da7f7d5c 100644 --- a/Sprint-1/implement/sum.test.js +++ b/Sprint-1/implement/sum.test.js @@ -20,6 +20,9 @@ it("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 +it("given array with one number, returns that number", () => { + expect(sum([6])).toEqual(6); +}); // Given an array containing negative numbers // When passed to the sum function From e7d3cbbba491bea72268c4cc15049b4a05f01ce2 Mon Sep 17 00:00:00 2001 From: Hanna Bohlin Date: Tue, 3 Nov 2026 09:54:24 +0000 Subject: [PATCH 21/37] Returns correct sum regardless of negative and decimal values present --- Sprint-1/implement/sum.js | 4 ++-- Sprint-1/implement/sum.test.js | 6 ++++++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/Sprint-1/implement/sum.js b/Sprint-1/implement/sum.js index d2419e5e4..097a56c45 100644 --- a/Sprint-1/implement/sum.js +++ b/Sprint-1/implement/sum.js @@ -2,8 +2,8 @@ function sum(elements) { let theSum = 0; for (element of elements){ - theSum = element; - } + theSum += element; + }; return theSum; } diff --git a/Sprint-1/implement/sum.test.js b/Sprint-1/implement/sum.test.js index 0da7f7d5c..74a2aa4b2 100644 --- a/Sprint-1/implement/sum.test.js +++ b/Sprint-1/implement/sum.test.js @@ -27,10 +27,16 @@ it("given array with one number, returns that number", () => { // Given an array containing negative numbers // When passed to the sum function // Then it should still return the correct total sum +it("given array with negative numbers, returns the correct sum", () => { + expect(sum([6, -10, 3, 5])).toEqual(4); +}); // Given an array with decimal/float numbers // When passed to the sum function // Then it should return the correct total sum +it("given array with decimal numbers, returns the correct sum", () => { + expect(sum([6, 3.68, 3, 5])).toEqual(17.68); +}); // Given an array containing a value that isn't a number // When passed to the sum function From 98b981005646321210706a6a2fd32b5bf15c8bb5 Mon Sep 17 00:00:00 2001 From: Hanna Bohlin Date: Tue, 3 Nov 2026 09:57:53 +0000 Subject: [PATCH 22/37] Throws when given array containing non number --- Sprint-1/implement/sum.js | 3 +++ Sprint-1/implement/sum.test.js | 5 +++++ 2 files changed, 8 insertions(+) diff --git a/Sprint-1/implement/sum.js b/Sprint-1/implement/sum.js index 097a56c45..e8ac7615b 100644 --- a/Sprint-1/implement/sum.js +++ b/Sprint-1/implement/sum.js @@ -2,6 +2,9 @@ function sum(elements) { let theSum = 0; for (element of elements){ + if (typeof element != "number"){ + throw new Error("sum requires an array of numbers"); + } theSum += element; }; diff --git a/Sprint-1/implement/sum.test.js b/Sprint-1/implement/sum.test.js index 74a2aa4b2..3aba52a1a 100644 --- a/Sprint-1/implement/sum.test.js +++ b/Sprint-1/implement/sum.test.js @@ -41,6 +41,11 @@ it("given array with decimal numbers, returns the correct 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") +it("given array with values that aren't numbers, should throw", () => { + expect(() => { + sum([6, 3.68, 3, "hey", 5]); + }).toThrow("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 76b199aab704e734bd39291171f6c39dbdfbf7c7 Mon Sep 17 00:00:00 2001 From: Hanna Bohlin Date: Tue, 3 Nov 2026 10:00:24 +0000 Subject: [PATCH 23/37] Throws when passed argument that isn't array --- Sprint-1/implement/sum.js | 5 +++++ Sprint-1/implement/sum.test.js | 11 +++++++++++ 2 files changed, 16 insertions(+) diff --git a/Sprint-1/implement/sum.js b/Sprint-1/implement/sum.js index e8ac7615b..c13d5083c 100644 --- a/Sprint-1/implement/sum.js +++ b/Sprint-1/implement/sum.js @@ -1,4 +1,9 @@ function sum(elements) { + + if (!Array.isArray(elements)){ + throw new Error("sum requires an array of numbers"); + } + let theSum = 0; for (element of elements){ diff --git a/Sprint-1/implement/sum.test.js b/Sprint-1/implement/sum.test.js index 3aba52a1a..8a96ce09a 100644 --- a/Sprint-1/implement/sum.test.js +++ b/Sprint-1/implement/sum.test.js @@ -50,3 +50,14 @@ it("given array with values that aren't numbers, should throw", () => { // 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("Given argument that isn't an array, should throw", () => { + expect(() => { + sum("hey"); + }).toThrow("sum requires an array of numbers"); + expect(() => { + sum(42); + }).toThrow("sum requires an array of numbers"); + expect(() => { + sum(); + }).toThrow("sum requires an array of numbers"); +}); \ No newline at end of file From 4408d158b0f83c4422b72b795731c79816a15e74 Mon Sep 17 00:00:00 2001 From: Hanna Bohlin Date: Tue, 3 Nov 2026 10:28:45 +0000 Subject: [PATCH 24/37] When passed empty array, returns empty array --- Sprint-1/implement/dedupe.js | 6 +++++- Sprint-1/implement/dedupe.test.js | 4 +++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/Sprint-1/implement/dedupe.js b/Sprint-1/implement/dedupe.js index 781e8718a..4528279ed 100644 --- a/Sprint-1/implement/dedupe.js +++ b/Sprint-1/implement/dedupe.js @@ -1 +1,5 @@ -function dedupe() {} +function dedupe(elements) { + return []; +} + +module.exports = dedupe; \ No newline at end of file diff --git a/Sprint-1/implement/dedupe.test.js b/Sprint-1/implement/dedupe.test.js index d7c8e3d8e..a38e6b947 100644 --- a/Sprint-1/implement/dedupe.test.js +++ b/Sprint-1/implement/dedupe.test.js @@ -16,7 +16,9 @@ E.g. dedupe([1, 2, 1]) returns [1, 2] // Given an empty array // When passed to the dedupe function // Then it should return an empty array -test.todo("given an empty array, it returns an empty array"); +it("given an empty array, returns empty array", () => { + expect(dedupe([])).toEqual([]); +}); // Given an array with no duplicates // When passed to the dedupe function From f76781f623f2af98057a3ac985485a43a80583de Mon Sep 17 00:00:00 2001 From: Hanna Bohlin Date: Tue, 3 Nov 2026 10:30:24 +0000 Subject: [PATCH 25/37] Returns same array when no duplicates present --- Sprint-1/implement/dedupe.js | 2 +- Sprint-1/implement/dedupe.test.js | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/Sprint-1/implement/dedupe.js b/Sprint-1/implement/dedupe.js index 4528279ed..167493016 100644 --- a/Sprint-1/implement/dedupe.js +++ b/Sprint-1/implement/dedupe.js @@ -1,5 +1,5 @@ function dedupe(elements) { - return []; + return elements; } module.exports = dedupe; \ No newline at end of file diff --git a/Sprint-1/implement/dedupe.test.js b/Sprint-1/implement/dedupe.test.js index a38e6b947..9de3079dd 100644 --- a/Sprint-1/implement/dedupe.test.js +++ b/Sprint-1/implement/dedupe.test.js @@ -23,6 +23,9 @@ it("given an empty array, returns 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("given an empty array, returns empty array", () => { + expect(dedupe([4, 6, 9, 23])).toEqual([4, 6, 9, 23]); +}); // Given an array of strings or numbers // When passed to the dedupe function From f1278f1da2cf64699d12ab62b3c74dd44dc3f1df Mon Sep 17 00:00:00 2001 From: Hanna Bohlin Date: Tue, 3 Nov 2026 10:34:57 +0000 Subject: [PATCH 26/37] Dedupe now returns deduped array --- Sprint-1/implement/dedupe.js | 11 ++++++++++- Sprint-1/implement/dedupe.test.js | 5 ++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/Sprint-1/implement/dedupe.js b/Sprint-1/implement/dedupe.js index 167493016..92662b957 100644 --- a/Sprint-1/implement/dedupe.js +++ b/Sprint-1/implement/dedupe.js @@ -1,5 +1,14 @@ function dedupe(elements) { - return elements; + + const dedupedArr = []; + + for (element of elements){ + if (!dedupedArr.includes(element)){ + dedupedArr.push(element); + } + }; + + return dedupedArr; } module.exports = dedupe; \ No newline at end of file diff --git a/Sprint-1/implement/dedupe.test.js b/Sprint-1/implement/dedupe.test.js index 9de3079dd..02b77c20f 100644 --- a/Sprint-1/implement/dedupe.test.js +++ b/Sprint-1/implement/dedupe.test.js @@ -23,7 +23,7 @@ it("given an empty array, returns 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("given an empty array, returns empty array", () => { +it("given array without duplicates, returns that array", () => { expect(dedupe([4, 6, 9, 23])).toEqual([4, 6, 9, 23]); }); @@ -31,3 +31,6 @@ it("given an empty array, returns empty array", () => { // When passed to the dedupe function // Then it should return a new array with duplicates removed while preserving the // first occurrence of each element from the original array. +it("given array with duplicates, returns array without duplicates", () => { + expect(dedupe([4, 6, 9, 23, 9, 23, 23])).toEqual([4, 6, 9, 23]); +}); From cce090bf31dfc24335bd8703e6ea43789d6ae6fd Mon Sep 17 00:00:00 2001 From: Hanna Bohlin Date: Tue, 3 Nov 2026 15:12:26 +0000 Subject: [PATCH 27/37] Function returns mean of array of numbers, negative, decimal, solo number, multiple --- Sprint-1/implement/mean.js | 10 +++++++++- Sprint-1/implement/mean.test.js | 10 +++++++++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/Sprint-1/implement/mean.js b/Sprint-1/implement/mean.js index 36909ea1f..4ce0cc231 100644 --- a/Sprint-1/implement/mean.js +++ b/Sprint-1/implement/mean.js @@ -1,3 +1,11 @@ -function calculateMean(list) {} +function calculateMean(list) { + let sum = 0; + + for (item of list){ + sum += item; + } + + return sum / list.length; +} module.exports = calculateMean; diff --git a/Sprint-1/implement/mean.test.js b/Sprint-1/implement/mean.test.js index 847b88676..c08e57f5e 100644 --- a/Sprint-1/implement/mean.test.js +++ b/Sprint-1/implement/mean.test.js @@ -23,15 +23,23 @@ 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("Given array of positive numbers, it returns their mean", () => { + expect(calculateMean([1, 2, 6])).toEqual(3); +}); // Given an array with a single number // When passed to calculateMean // Then it should return that number +it("Given array of one number, it returns that number", () => { + expect(calculateMean([2])).toEqual(2); +}); // Given an array containing negative or decimal numbers // When passed to calculateMean // Then it should return the correct mean +it("Given array of negative or decimal number, it returns their mean", () => { + expect(calculateMean([1, -2, 6, 5.89, -43.284])).toEqual(-6.4788); +}); // Given an empty array // When passed to calculateMean From 0accc0352486765ad2ed1181a39de8ac550b2ed7 Mon Sep 17 00:00:00 2001 From: Hanna Bohlin Date: Tue, 3 Nov 2026 15:15:06 +0000 Subject: [PATCH 28/37] Throws when passed empty array --- Sprint-1/implement/mean.js | 7 ++++++- Sprint-1/implement/mean.test.js | 3 +++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/Sprint-1/implement/mean.js b/Sprint-1/implement/mean.js index 4ce0cc231..511b353f2 100644 --- a/Sprint-1/implement/mean.js +++ b/Sprint-1/implement/mean.js @@ -1,9 +1,14 @@ function calculateMean(list) { + + if (list.length === 0){ + throw new Error("calculateMean requires a non-empty array"); + }; + let sum = 0; for (item of list){ sum += item; - } + }; return sum / list.length; } diff --git a/Sprint-1/implement/mean.test.js b/Sprint-1/implement/mean.test.js index c08e57f5e..8fff27e89 100644 --- a/Sprint-1/implement/mean.test.js +++ b/Sprint-1/implement/mean.test.js @@ -44,6 +44,9 @@ it("Given array of negative or decimal number, it returns their mean", () => { // Given an empty array // When passed to calculateMean // Then it should throw Error("calculateMean requires a non-empty array") +it("Given an empty array, it should throw", () => { + expect(() => {calculateMean([])}).toThrow("calculateMean requires a non-empty array"); +}) // Given a value that isn't an array, e.g. "banana", 42, null or {} // When passed to calculateMean From dec5bce9cdfc9c6958d359761d2f5198fc8907ee Mon Sep 17 00:00:00 2001 From: Hanna Bohlin Date: Tue, 3 Nov 2026 15:17:35 +0000 Subject: [PATCH 29/37] Throws when passed non-array --- Sprint-1/implement/mean.js | 3 +++ Sprint-1/implement/mean.test.js | 14 ++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/Sprint-1/implement/mean.js b/Sprint-1/implement/mean.js index 511b353f2..5cb9037ce 100644 --- a/Sprint-1/implement/mean.js +++ b/Sprint-1/implement/mean.js @@ -1,5 +1,8 @@ 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"); }; diff --git a/Sprint-1/implement/mean.test.js b/Sprint-1/implement/mean.test.js index 8fff27e89..e20694ee0 100644 --- a/Sprint-1/implement/mean.test.js +++ b/Sprint-1/implement/mean.test.js @@ -51,6 +51,20 @@ it("Given an empty array, it should throw", () => { // 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("Given value that isn't array, it should throw", () => { + expect(() => { + calculateMean("banana"); + }).toThrow("calculateMean requires an array of numbers"); + expect(() => { + calculateMean(42); + }).toThrow("calculateMean requires an array of numbers"); + expect(() => { + calculateMean(null); + }).toThrow("calculateMean requires an array of numbers"); + expect(() => { + calculateMean({}); + }).toThrow("calculateMean requires an array of numbers"); +}); // Given no argument at all // When passed to calculateMean From 6caf465b5480a31edd7a52f0ae2255224fd5504c Mon Sep 17 00:00:00 2001 From: Hanna Bohlin Date: Tue, 3 Nov 2026 15:23:30 +0000 Subject: [PATCH 30/37] Already throws for passing no value --- Sprint-1/implement/mean.test.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Sprint-1/implement/mean.test.js b/Sprint-1/implement/mean.test.js index e20694ee0..6589d9c51 100644 --- a/Sprint-1/implement/mean.test.js +++ b/Sprint-1/implement/mean.test.js @@ -69,6 +69,11 @@ it("Given value that isn't array, it should throw", () => { // Given no argument at all // When passed to calculateMean // Then it should throw Error("calculateMean requires an array of numbers") +it("Given no value at all, it should throw", () => { + expect(() => { + calculateMean(); + }).toThrow("calculateMean requires an array of numbers"); +}); // Given an array containing a non-number value, e.g. [1, "2", 3] // When passed to calculateMean From aaf6237f479fc9f83f17b7225d60114207166ec4 Mon Sep 17 00:00:00 2001 From: Hanna Bohlin Date: Tue, 3 Nov 2026 15:28:34 +0000 Subject: [PATCH 31/37] Throws when sent an array including a non-number --- Sprint-1/implement/mean.js | 3 +++ Sprint-1/implement/mean.test.js | 5 +++++ 2 files changed, 8 insertions(+) diff --git a/Sprint-1/implement/mean.js b/Sprint-1/implement/mean.js index 5cb9037ce..fcf0a2cbf 100644 --- a/Sprint-1/implement/mean.js +++ b/Sprint-1/implement/mean.js @@ -10,6 +10,9 @@ function calculateMean(list) { let sum = 0; for (item of list){ + if (typeof item != "number"){ + throw new Error("calculateMean requires an array of numbers"); + }; sum += item; }; diff --git a/Sprint-1/implement/mean.test.js b/Sprint-1/implement/mean.test.js index 6589d9c51..b1e9df4ef 100644 --- a/Sprint-1/implement/mean.test.js +++ b/Sprint-1/implement/mean.test.js @@ -78,3 +78,8 @@ it("Given no value at all, it should throw", () => { // 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("Given array containing non-number, it should throw", () => { + expect(() => { + calculateMean([1, "2", 3]); + }).toThrow("calculateMean requires an array of numbers"); +}); From 4df5d9bfdf6299d96b1d79c3ea9724cfa26976fe Mon Sep 17 00:00:00 2001 From: Hanna Bohlin Date: Tue, 3 Nov 2026 15:33:36 +0000 Subject: [PATCH 32/37] Returns the correct message when given an array of numbers --- Sprint-1/implement/describe-median.js | 5 ++++- Sprint-1/implement/describe-median.test.js | 4 +++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/Sprint-1/implement/describe-median.js b/Sprint-1/implement/describe-median.js index 3381cfc0a..3b7f0d310 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 median = calculateMedian(list); + return "The median is " + median; +} module.exports = describeMedian; diff --git a/Sprint-1/implement/describe-median.test.js b/Sprint-1/implement/describe-median.test.js index e7d18a264..f4fe572c8 100644 --- a/Sprint-1/implement/describe-median.test.js +++ b/Sprint-1/implement/describe-median.test.js @@ -21,7 +21,9 @@ const describeMedian = require("./describe-median.js"); // When passed to describeMedian // Then it should return "The median is " followed by the median // Delete this test.todo and replace it with a test. -test.todo('given [1, 2, 3], returns "The median is 2"'); +it("Given array of positive numbers, it returns their mean", () => { + expect(describeMedian([1, 2, 6])).toEqual("The median is 2"); +}); // Given an empty array // When passed to describeMedian From 795191f57c58ed051b9e62889d6f39de2852c014 Mon Sep 17 00:00:00 2001 From: Hanna Bohlin Date: Tue, 3 Nov 2026 15:40:19 +0000 Subject: [PATCH 33/37] Catches error and displays error message when passed empty array --- Sprint-1/implement/describe-median.js | 8 ++++++-- Sprint-1/implement/describe-median.test.js | 7 ++++++- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/Sprint-1/implement/describe-median.js b/Sprint-1/implement/describe-median.js index 3b7f0d310..e7f234646 100644 --- a/Sprint-1/implement/describe-median.js +++ b/Sprint-1/implement/describe-median.js @@ -21,8 +21,12 @@ function calculateMedian(list) { // Implement this function. See describe-median.test.js for the acceptance criteria. function describeMedian(list) { - const median = calculateMedian(list); - return "The median is " + median; + try{ + const median = calculateMedian(list); + return "The median is " + median; + } catch (error) { + return "Could not calculate a median: " + error.message; + } } module.exports = describeMedian; diff --git a/Sprint-1/implement/describe-median.test.js b/Sprint-1/implement/describe-median.test.js index f4fe572c8..4f8357d9f 100644 --- a/Sprint-1/implement/describe-median.test.js +++ b/Sprint-1/implement/describe-median.test.js @@ -21,13 +21,18 @@ 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. -it("Given array of positive numbers, it returns their mean", () => { +it("Given array of positive numbers, it returns the correct message", () => { expect(describeMedian([1, 2, 6])).toEqual("The median is 2"); }); // Given an empty array // When passed to describeMedian // Then it should return "Could not calculate a median: calculateMedian requires a non-empty array" +it("Given an empty array, it tells you it couldn't calculate the median and why", () => { + 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 From 0b346594a8052a80fa67d83403c7245920756710 Mon Sep 17 00:00:00 2001 From: Hanna Bohlin Date: Tue, 3 Nov 2026 15:42:56 +0000 Subject: [PATCH 34/37] Already catches thrown error for array containing non-number or non number --- Sprint-1/implement/describe-median.test.js | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/Sprint-1/implement/describe-median.test.js b/Sprint-1/implement/describe-median.test.js index 4f8357d9f..6ce2adc8a 100644 --- a/Sprint-1/implement/describe-median.test.js +++ b/Sprint-1/implement/describe-median.test.js @@ -37,3 +37,14 @@ it("Given an empty array, it tells you it couldn't calculate the median and why" // 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("Given something that isn't array of numbers, it tells you it couldn't calculate the median and why", () => { + expect(describeMedian("banana")).toEqual( + "Could not calculate a median: calculateMedian requires an array of numbers" + ); + expect(describeMedian([3, "5", 8])).toEqual( + "Could not calculate a median: calculateMedian requires an array of numbers" + ); + expect(describeMedian(42)).toEqual( + "Could not calculate a median: calculateMedian requires an array of numbers" + ); +}); From 1452e8f5b39bc220ac8dbeeaf24f87e3a13cfee1 Mon Sep 17 00:00:00 2001 From: Hanna Bohlin Date: Tue, 3 Nov 2026 15:50:26 +0000 Subject: [PATCH 35/37] Refactor includes funtion to use for of loop --- Sprint-1/refactor/includes.js | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/Sprint-1/refactor/includes.js b/Sprint-1/refactor/includes.js index 29dad81f0..ac60c196d 100644 --- a/Sprint-1/refactor/includes.js +++ b/Sprint-1/refactor/includes.js @@ -1,12 +1,11 @@ // 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 (item of list){ + if (item === target){ return true; - } - } + }; + }; return false; } From 2adeba338370b50a11beb7ae2479d58b971c1a6a Mon Sep 17 00:00:00 2001 From: Hanna Bohlin Date: Tue, 3 Nov 2026 15:52:19 +0000 Subject: [PATCH 36/37] Format all files with prettier --- Sprint-1/refactor/includes.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Sprint-1/refactor/includes.js b/Sprint-1/refactor/includes.js index ac60c196d..cef2f4b62 100644 --- a/Sprint-1/refactor/includes.js +++ b/Sprint-1/refactor/includes.js @@ -1,11 +1,11 @@ // Refactor the implementation of includes to use a for...of loop function includes(list, target) { - for (item of list){ - if (item === target){ + for (item of list) { + if (item === target) { return true; - }; - }; + } + } return false; } From a94cc17bb9debeb286cc890254cc4d1e4ba89726 Mon Sep 17 00:00:00 2001 From: Hanna Bohlin Date: Tue, 3 Nov 2026 15:53:09 +0000 Subject: [PATCH 37/37] Format all files for real with prettier --- Sprint-1/fix/median.js | 10 ++++----- Sprint-1/implement/dedupe.js | 17 +++++++------- Sprint-1/implement/dedupe.test.js | 2 +- Sprint-1/implement/describe-median.js | 2 +- Sprint-1/implement/max.js | 32 +++++++++++++-------------- Sprint-1/implement/max.test.js | 14 ++++++------ Sprint-1/implement/mean.js | 29 ++++++++++++------------ Sprint-1/implement/mean.test.js | 6 +++-- Sprint-1/implement/sum.js | 23 +++++++++---------- Sprint-1/implement/sum.test.js | 2 +- 10 files changed, 68 insertions(+), 69 deletions(-) diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index 6d721d904..d272d61af 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -5,7 +5,7 @@ // 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=0) { +function calculateMedian(list = 0) { if (list === null) { throw new Error("calculateMedian requires an array of numbers"); } @@ -22,12 +22,12 @@ function calculateMedian(list=0) { throw new Error("calculateMedian requires an array of numbers"); } - if (typeof list === "object" && !Array.isArray(list)){ + if (typeof list === "object" && !Array.isArray(list)) { throw new Error("calculateMedian requires an array of numbers"); - }; + } - for (item of list){ - if (typeof item === "string" || item === null || item === undefined){ + for (item of list) { + if (typeof item === "string" || item === null || item === undefined) { throw new Error("calculateMedian requires an array of numbers"); } } diff --git a/Sprint-1/implement/dedupe.js b/Sprint-1/implement/dedupe.js index 92662b957..999b81e16 100644 --- a/Sprint-1/implement/dedupe.js +++ b/Sprint-1/implement/dedupe.js @@ -1,14 +1,13 @@ function dedupe(elements) { + const dedupedArr = []; - const dedupedArr = []; + for (element of elements) { + if (!dedupedArr.includes(element)) { + dedupedArr.push(element); + } + } - for (element of elements){ - if (!dedupedArr.includes(element)){ - dedupedArr.push(element); - } - }; - - return dedupedArr; + return dedupedArr; } -module.exports = dedupe; \ No newline at end of file +module.exports = dedupe; diff --git a/Sprint-1/implement/dedupe.test.js b/Sprint-1/implement/dedupe.test.js index 02b77c20f..f6cb4d4ff 100644 --- a/Sprint-1/implement/dedupe.test.js +++ b/Sprint-1/implement/dedupe.test.js @@ -29,7 +29,7 @@ it("given array without duplicates, returns that 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. it("given array with duplicates, returns array without duplicates", () => { expect(dedupe([4, 6, 9, 23, 9, 23, 23])).toEqual([4, 6, 9, 23]); diff --git a/Sprint-1/implement/describe-median.js b/Sprint-1/implement/describe-median.js index e7f234646..e6a0a4033 100644 --- a/Sprint-1/implement/describe-median.js +++ b/Sprint-1/implement/describe-median.js @@ -21,7 +21,7 @@ function calculateMedian(list) { // Implement this function. See describe-median.test.js for the acceptance criteria. function describeMedian(list) { - try{ + try { const median = calculateMedian(list); return "The median is " + median; } catch (error) { diff --git a/Sprint-1/implement/max.js b/Sprint-1/implement/max.js index f25b88b96..f10e67d8c 100644 --- a/Sprint-1/implement/max.js +++ b/Sprint-1/implement/max.js @@ -1,24 +1,24 @@ function findMax(elements) { - if (!Array.isArray(elements)){ - throw new Error("findMax requires an array of numbers"); - }; + if (!Array.isArray(elements)) { + throw new Error("findMax requires an array of numbers"); + } - if (elements.length === 0){ - return -Infinity; - }; + if (elements.length === 0) { + return -Infinity; + } - let max = elements[0]; + let max = elements[0]; - for (element of elements){ - if (typeof element != "number"){ - throw new Error("findMax requires an array of numbers"); - } - if (element > max){ - max = element; - } - }; + for (element of elements) { + if (typeof element != "number") { + throw new Error("findMax requires an array of numbers"); + } + if (element > max) { + max = element; + } + } - return max; + return max; } module.exports = findMax; diff --git a/Sprint-1/implement/max.test.js b/Sprint-1/implement/max.test.js index 97fc8407d..84e9830cd 100644 --- a/Sprint-1/implement/max.test.js +++ b/Sprint-1/implement/max.test.js @@ -17,21 +17,21 @@ const findMax = require("./max.js"); // Then it should return -Infinity // Delete this test.todo and replace it with a test. it("given an empty array, returns -Infinity", () => { - expect(findMax([])).toEqual(-Infinity); + expect(findMax([])).toEqual(-Infinity); }); // Given an array with one number // When passed to the max function // Then it should return that number it("Given array with one number should return that number", () => { - expect(findMax([3])).toEqual(3); + expect(findMax([3])).toEqual(3); }); // Given an array with both positive and negative numbers // When passed to the max function // Then it should return the largest number overall it("Given array of positive and negative values returns the biggest", () => { - expect(findMax([4,-7,47,-90])).toEqual(47); + expect(findMax([4, -7, 47, -90])).toEqual(47); }); // Given an array with just negative numbers @@ -52,9 +52,9 @@ it("Given array of positive and negative values returns the biggest", () => { // When passed to the max function // Then it should throw Error("findMax requires an array of numbers") it("Given array with value that isn't a number, should throw", () => { - expect(() => { - findMax([5, "ooo", 203]); - }).toThrow("findMax requires an array of numbers"); + expect(() => { + findMax([5, "ooo", 203]); + }).toThrow("findMax requires an array of numbers"); }); // Given something that isn't an array at all, such as "hey", 42 or no argument @@ -70,4 +70,4 @@ it("Given argument that isn't an array, should throw", () => { expect(() => { findMax(); }).toThrow("findMax requires an array of numbers"); -}); \ No newline at end of file +}); diff --git a/Sprint-1/implement/mean.js b/Sprint-1/implement/mean.js index fcf0a2cbf..47a471137 100644 --- a/Sprint-1/implement/mean.js +++ b/Sprint-1/implement/mean.js @@ -1,22 +1,21 @@ 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 (!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"); - }; - - let sum = 0; + let sum = 0; - for (item of list){ - if (typeof item != "number"){ - throw new Error("calculateMean requires an array of numbers"); - }; - sum += item; - }; + for (item of list) { + if (typeof item != "number") { + throw new Error("calculateMean requires an array of numbers"); + } + sum += item; + } - return sum / list.length; + return sum / list.length; } module.exports = calculateMean; diff --git a/Sprint-1/implement/mean.test.js b/Sprint-1/implement/mean.test.js index b1e9df4ef..634d8b5b4 100644 --- a/Sprint-1/implement/mean.test.js +++ b/Sprint-1/implement/mean.test.js @@ -45,8 +45,10 @@ it("Given array of negative or decimal number, it returns their mean", () => { // When passed to calculateMean // Then it should throw Error("calculateMean requires a non-empty array") it("Given an empty array, it should throw", () => { - expect(() => {calculateMean([])}).toThrow("calculateMean requires a non-empty array"); -}) + expect(() => { + calculateMean([]); + }).toThrow("calculateMean requires a non-empty array"); +}); // Given a value that isn't an array, e.g. "banana", 42, null or {} // When passed to calculateMean diff --git a/Sprint-1/implement/sum.js b/Sprint-1/implement/sum.js index c13d5083c..f2a635262 100644 --- a/Sprint-1/implement/sum.js +++ b/Sprint-1/implement/sum.js @@ -1,19 +1,18 @@ function sum(elements) { + if (!Array.isArray(elements)) { + throw new Error("sum requires an array of numbers"); + } - if (!Array.isArray(elements)){ - throw new Error("sum requires an array of numbers"); - } - - let theSum = 0; + let theSum = 0; - for (element of elements){ - if (typeof element != "number"){ - throw new Error("sum requires an array of numbers"); - } - theSum += element; - }; + for (element of elements) { + if (typeof element != "number") { + throw new Error("sum requires an array of numbers"); + } + theSum += element; + } - return theSum; + return theSum; } module.exports = sum; diff --git a/Sprint-1/implement/sum.test.js b/Sprint-1/implement/sum.test.js index 8a96ce09a..946a47f3b 100644 --- a/Sprint-1/implement/sum.test.js +++ b/Sprint-1/implement/sum.test.js @@ -60,4 +60,4 @@ it("Given argument that isn't an array, should throw", () => { expect(() => { sum(); }).toThrow("sum requires an array of numbers"); -}); \ No newline at end of file +});