From 5ce7d150f73f16767529d29ce0d1aeb8d158a23d Mon Sep 17 00:00:00 2001 From: Russom Gebremeskel Date: Tue, 3 Nov 2026 15:15:02 +0000 Subject: [PATCH 01/36] Add if condition to check list is odd or even. --- Sprint-1/fix/median.js | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index 5c5b796e1..68256d867 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -6,9 +6,14 @@ // 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; const middleIndex = Math.floor(list.length / 2); - const median = list.splice(middleIndex, 1)[0]; - return median; + if (list.length % 2 === 0) { + return (list[middleIndex - 1] + list[middleIndex]) / 2; + } + return list[middleIndex]; } module.exports = calculateMedian; From 8f51f4cf93a2b66e1afa68875252662c7493b5a0 Mon Sep 17 00:00:00 2001 From: Russom Gebremeskel Date: Tue, 3 Nov 2026 16:43:00 +0000 Subject: [PATCH 02/36] Add sort method to to sort list. --- Sprint-1/fix/median.js | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index 68256d867..7a7b3557b 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -6,14 +6,25 @@ // 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; - const middleIndex = Math.floor(list.length / 2); - if (list.length % 2 === 0) { - return (list[middleIndex - 1] + list[middleIndex]) / 2; + if (!Array.isArray(list)) { + throw new Error("calculateMedian requires an array of numbers"); } - return list[middleIndex]; + + if (list.length === 0) { + throw new Error("calculateMedian requires an non-empty array"); + } + + for (const item of list) { + if (typeof item !== "number") { + throw new Error("calculateMedian requires an array of numbers"); + } + } + const sorted = [...list].sort((a, b) => a - b); + const middleIndex = Math.floor(sorted.length / 2); + if (sorted.length % 2 === 0) { + return (sorted[middleIndex - 1] + sorted[middleIndex]) / 2; + } + return sorted[middleIndex]; } module.exports = calculateMedian; From d68c02b23c2fd6c24d116137628aa9259f25e6eb Mon Sep 17 00:00:00 2001 From: Russom Gebremeskel Date: Tue, 3 Nov 2026 20:13:17 +0000 Subject: [PATCH 03/36] Add if condition to check empty array. --- 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 7a7b3557b..8bf6aa764 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -11,7 +11,7 @@ function calculateMedian(list) { } if (list.length === 0) { - throw new Error("calculateMedian requires an non-empty array"); + throw new Error("calculateMedian requires a non-empty array"); } for (const item of list) { From f65276c6d8e19e414f4095d011e40276f8de14b1 Mon Sep 17 00:00:00 2001 From: Russom Gebremeskel Date: Tue, 3 Nov 2026 21:50:49 +0000 Subject: [PATCH 04/36] Write test cases for testing empty and no duplicate arrays --- Sprint-1/implement/dedupe.test.js | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/Sprint-1/implement/dedupe.test.js b/Sprint-1/implement/dedupe.test.js index d7c8e3d8e..80c6723c2 100644 --- a/Sprint-1/implement/dedupe.test.js +++ b/Sprint-1/implement/dedupe.test.js @@ -16,11 +16,20 @@ E.g. dedupe([1, 2, 1]) returns [1, 2] // Given an empty array // When passed to the dedupe function // Then it should return an empty array -test.todo("given an empty array, it returns an empty array"); + test("given an empty array, it returns an empty array", () => { + const list = []; + const result = dedupe(list); + expect(dedupe(list)).toEqual(result); + }); // Given an array with no duplicates // When passed to the dedupe function // Then it should return a copy of the original array + test("given an array with no duplicates, it returns a copy of the original array", () => { + const list = ["a", "b", "c"]; + const result = dedupe(list); + expect(dedupe(list)).toEqual(result); + }); // Given an array of strings or numbers // When passed to the dedupe function From 6745e82482ca3d6c8c685818dc940e07de94766f Mon Sep 17 00:00:00 2001 From: Russom Gebremeskel Date: Tue, 3 Nov 2026 21:51:48 +0000 Subject: [PATCH 05/36] Implement function to check for empty and duplicated arrays. --- Sprint-1/implement/dedupe.js | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/Sprint-1/implement/dedupe.js b/Sprint-1/implement/dedupe.js index 781e8718a..b50db0a85 100644 --- a/Sprint-1/implement/dedupe.js +++ b/Sprint-1/implement/dedupe.js @@ -1 +1,16 @@ -function dedupe() {} +function dedupe(list) { + const elements = []; + if (list.length === 0) { + return []; + } + + + for (const item of list) { + if (!elements.includes(item)) { + elements.push(list); + } + } + return elements; +} + +module.exports = dedupe; From 20d911a81c2b32b94ec78aaa1a080a0359d836b2 Mon Sep 17 00:00:00 2001 From: Russom Gebremeskel Date: Tue, 3 Nov 2026 22:18:08 +0000 Subject: [PATCH 06/36] Write a test for testing original occurrence returned --- 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 80c6723c2..870bd2db5 100644 --- a/Sprint-1/implement/dedupe.test.js +++ b/Sprint-1/implement/dedupe.test.js @@ -35,3 +35,8 @@ E.g. dedupe([1, 2, 1]) returns [1, 2] // 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. + test("given an array, it returns an array with duplicates removed and its original occurrence", () => { + const list = [3, 1, 2]; + const result = dedupe(list); + expect(dedupe(list)).toEqual(result); + }); \ No newline at end of file From 6f7f6d40a1bba3524ea97a3ce338bbfdda1ddfcd Mon Sep 17 00:00:00 2001 From: Russom Gebremeskel Date: Tue, 3 Nov 2026 22:19:47 +0000 Subject: [PATCH 07/36] Fix bug where item pushed instead of list --- Sprint-1/implement/dedupe.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Sprint-1/implement/dedupe.js b/Sprint-1/implement/dedupe.js index b50db0a85..4b439bcf5 100644 --- a/Sprint-1/implement/dedupe.js +++ b/Sprint-1/implement/dedupe.js @@ -7,10 +7,13 @@ function dedupe(list) { for (const item of list) { if (!elements.includes(item)) { - elements.push(list); + elements.push(item); } } return elements; } +const arr = [2, 3, 1, 2]; +console.log(dedupe(arr)); + module.exports = dedupe; From a922943b2cf4f0773ef10c9848f1a47ee7e8513e Mon Sep 17 00:00:00 2001 From: Russom Gebremeskel Date: Tue, 3 Nov 2026 22:44:14 +0000 Subject: [PATCH 08/36] Format code with prettier. --- Sprint-1/implement/dedupe.js | 5 +---- Sprint-1/implement/dedupe.test.js | 32 +++++++++++++++---------------- 2 files changed, 17 insertions(+), 20 deletions(-) diff --git a/Sprint-1/implement/dedupe.js b/Sprint-1/implement/dedupe.js index 4b439bcf5..2ec3c6352 100644 --- a/Sprint-1/implement/dedupe.js +++ b/Sprint-1/implement/dedupe.js @@ -1,10 +1,10 @@ function dedupe(list) { const elements = []; + if (list.length === 0) { return []; } - for (const item of list) { if (!elements.includes(item)) { elements.push(item); @@ -13,7 +13,4 @@ function dedupe(list) { return elements; } -const arr = [2, 3, 1, 2]; -console.log(dedupe(arr)); - module.exports = dedupe; diff --git a/Sprint-1/implement/dedupe.test.js b/Sprint-1/implement/dedupe.test.js index 870bd2db5..094e7dc0c 100644 --- a/Sprint-1/implement/dedupe.test.js +++ b/Sprint-1/implement/dedupe.test.js @@ -16,27 +16,27 @@ 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("given an empty array, it returns an empty array", () => { - const list = []; - const result = dedupe(list); - expect(dedupe(list)).toEqual(result); - }); +test("given an empty array, it returns an empty array", () => { + const list = []; + const result = dedupe(list); + expect(dedupe(list)).toEqual(result); +}); // Given an array with no duplicates // When passed to the dedupe function // Then it should return a copy of the original array - test("given an array with no duplicates, it returns a copy of the original array", () => { - const list = ["a", "b", "c"]; - const result = dedupe(list); - expect(dedupe(list)).toEqual(result); - }); +test("given an array with no duplicates, it returns a copy of the original array", () => { + const list = ["a", "b", "c"]; + const result = dedupe(list); + expect(dedupe(list)).toEqual(result); +}); // 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. - test("given an array, it returns an array with duplicates removed and its original occurrence", () => { - const list = [3, 1, 2]; - const result = dedupe(list); - expect(dedupe(list)).toEqual(result); - }); \ No newline at end of file +test("given an array, it returns an array with duplicates removed and its original occurrence", () => { + const list = [3, 1, 2]; + const result = dedupe(list); + expect(dedupe(list)).toEqual(result); +}); From 24917598ade8acdd27e5b4656306b1dd80a225d4 Mon Sep 17 00:00:00 2001 From: Russom Gebremeskel Date: Wed, 4 Nov 2026 20:54:53 +0000 Subject: [PATCH 09/36] Implement try catch function to return error messages. --- Sprint-1/implement/describe-median.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/Sprint-1/implement/describe-median.js b/Sprint-1/implement/describe-median.js index 3381cfc0a..e81890647 100644 --- a/Sprint-1/implement/describe-median.js +++ b/Sprint-1/implement/describe-median.js @@ -20,6 +20,12 @@ function calculateMedian(list) { } // Implement this function. See describe-median.test.js for the acceptance criteria. -function describeMedian(list) {} +function describeMedian(list) { + try { + return `The median is ${calculateMedian(list)}`; + } catch (error) { + return `Could not calculate a median: calculateMedian requires a non-empty array: ${error.message}`; + } +} module.exports = describeMedian; From 697d23f6ec46b8b185dea745feb2ba08fb6e4406 Mon Sep 17 00:00:00 2001 From: Russom Gebremeskel Date: Wed, 4 Nov 2026 20:55:54 +0000 Subject: [PATCH 10/36] Write test cases for multiple arrays --- Sprint-1/implement/describe-median.test.js | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/Sprint-1/implement/describe-median.test.js b/Sprint-1/implement/describe-median.test.js index e7d18a264..49427757d 100644 --- a/Sprint-1/implement/describe-median.test.js +++ b/Sprint-1/implement/describe-median.test.js @@ -15,18 +15,33 @@ message, so the caller can see what went wrong. const describeMedian = require("./describe-median.js"); + // Acceptance criteria: // Given an array of numbers // When passed to describeMedian // Then it should return "The median is " followed by the median // Delete this test.todo and replace it with a test. -test.todo('given [1, 2, 3], returns "The median is 2"'); +test("given [1, 2, 3], returns The median is 2", () => { + const list = [1, 2, 3]; + const result = describeMedian(list); + expect(describeMedian(list)).toEqual(result); +}); // Given an empty array // When passed to describeMedian // Then it should return "Could not calculate a median: calculateMedian requires a non-empty array" +test("given an empty array [], returns Could not calculate a median", () => { + const list = []; + const result = describeMedian(list); + expect(describeMedian(list)).toEqual(result); +}); // 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" +test("given a non-array , returns Could not calculate a median", () => { + const list = []; + const result = describeMedian(list); + expect(describeMedian(list)).toEqual(result); +}); \ No newline at end of file From 33ffd60bf19d6e5fc0af6ff51de71dace030d48c Mon Sep 17 00:00:00 2001 From: Russom Gebremeskel Date: Thu, 5 Nov 2026 22:43:52 +0000 Subject: [PATCH 11/36] Write test case for findMax with empty array --- Sprint-1/implement/max.test.js | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/Sprint-1/implement/max.test.js b/Sprint-1/implement/max.test.js index ff3fbb5e5..7e4afc81a 100644 --- a/Sprint-1/implement/max.test.js +++ b/Sprint-1/implement/max.test.js @@ -16,11 +16,18 @@ const findMax = require("./max.js"); // When passed to the max function // Then it should return -Infinity // Delete this test.todo and replace it with a test. -test.todo("given an empty array, returns -Infinity"); +test("given an empty array, returns -Infinity", () => { + expect(findMax([])).toEqual(-Infinity); +}); // Given an array with one number // When passed to the max function // Then it should return that number +test("given a one element array, returns the same element", () => { + const list = [1]; + const result = findMax(list); + expect(result).toEqual(1); +}); // Given an array with both positive and negative numbers // When passed to the max function From b4711371cb3f47bb81465cb66e12a35fcc8a3ece Mon Sep 17 00:00:00 2001 From: Russom Gebremeskel Date: Thu, 5 Nov 2026 22:45:59 +0000 Subject: [PATCH 12/36] Write test case for findMax with one number array --- Sprint-1/implement/max.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sprint-1/implement/max.test.js b/Sprint-1/implement/max.test.js index 7e4afc81a..56b09fbba 100644 --- a/Sprint-1/implement/max.test.js +++ b/Sprint-1/implement/max.test.js @@ -23,7 +23,7 @@ test("given an empty array, returns -Infinity", () => { // Given an array with one number // When passed to the max function // Then it should return that number -test("given a one element array, returns the same element", () => { +test("given an array with one number, returns that number", () => { const list = [1]; const result = findMax(list); expect(result).toEqual(1); From 434ca4b6647d09201acde3b428c9cfbb3175358a Mon Sep 17 00:00:00 2001 From: Russom Gebremeskel Date: Thu, 5 Nov 2026 22:48:31 +0000 Subject: [PATCH 13/36] Implement function for checking array the largest element --- Sprint-1/implement/max.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Sprint-1/implement/max.js b/Sprint-1/implement/max.js index 6dd76378e..8f0b822e4 100644 --- a/Sprint-1/implement/max.js +++ b/Sprint-1/implement/max.js @@ -1,4 +1,8 @@ function findMax(elements) { + if (!elements.length === 1) { + return false; + } + return Math.max(...elements); } module.exports = findMax; From d99779759b47aeed30cfdcc7c917f116daf25a1e Mon Sep 17 00:00:00 2001 From: Russom Gebremeskel Date: Thu, 5 Nov 2026 23:27:34 +0000 Subject: [PATCH 14/36] Add for and if condition for checking non-array and non-number elements --- Sprint-1/implement/max.js | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/Sprint-1/implement/max.js b/Sprint-1/implement/max.js index 8f0b822e4..efee75da5 100644 --- a/Sprint-1/implement/max.js +++ b/Sprint-1/implement/max.js @@ -1,6 +1,16 @@ function findMax(elements) { - if (!elements.length === 1) { - return false; + if (!Array.isArray(elements)) { + throw new Error("findMax requires an array of numbers"); + } + + for (const element of elements) { + if (typeof element !== "number") { + throw new Error("findMax requires an array of numbers"); + } + + if (!elements.length === 1) { + return element; + } } return Math.max(...elements); } From 734381dc2008468732bfc579f8b79c9321aec21c Mon Sep 17 00:00:00 2001 From: Russom Gebremeskel Date: Thu, 5 Nov 2026 23:28:44 +0000 Subject: [PATCH 15/36] Write test cases for finMax with different input. --- Sprint-1/implement/max.test.js | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/Sprint-1/implement/max.test.js b/Sprint-1/implement/max.test.js index 56b09fbba..2fbfa0139 100644 --- a/Sprint-1/implement/max.test.js +++ b/Sprint-1/implement/max.test.js @@ -32,19 +32,44 @@ test("given an array with one number, returns that number", () => { // Given an array with both positive and negative numbers // When passed to the max function // Then it should return the largest number overall +test("given an array with positive and negative numbers, returns the largest number", () => { + const list = [1, -9, 7, -5]; + const result = findMax(list); + expect(result).toEqual(7); +}); // Given an array with just negative numbers // When passed to the max function // Then it should return the closest one to zero +test("given an array with negative numbers, returns closest one to zero", () => { + const list = [-1, -9, -7, -5]; + const result = findMax(list); + expect(result).toEqual(-1); +}); // Given an array with decimal numbers // When passed to the max function // Then it should return the largest decimal number +test("given an array with decimal numbers, returns the largest decimal number", () => { + const list = [1.1, 0.9, 4.7, 1.5]; + const result = findMax(list); + expect(result).toEqual(4.7); +}); // 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") +test("given an array with non-numbers, returns, findMax requires an array of numbers", () => { + expect(() => findMax(["a", "l", "k"])).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") +test("given a non-array, returns, findMax requires an array of numbers", () => { + expect(() => findMax("a", "", 7, "kilo")).toThrow( + new Error("findMax requires an array of numbers") + ); +}); \ No newline at end of file From 215a119d5106809c844859f7e4f6f7b7527b90e1 Mon Sep 17 00:00:00 2001 From: Russom Gebremeskel Date: Fri, 6 Nov 2026 18:23:33 +0000 Subject: [PATCH 16/36] Write test case for calculateMean with array of numbers. --- Sprint-1/implement/mean.test.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/Sprint-1/implement/mean.test.js b/Sprint-1/implement/mean.test.js index 847b88676..8772dfb30 100644 --- a/Sprint-1/implement/mean.test.js +++ b/Sprint-1/implement/mean.test.js @@ -23,12 +23,17 @@ 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"); +test("given [1, 2, 6], returns 3", () => { + const list = [1, 2, 6]; + const result = calculateMean(list); + expect(result).toEqual(3); +}); // Given an array with a single number // When passed to calculateMean // Then it should return that number + // Given an array containing negative or decimal numbers // When passed to calculateMean // Then it should return the correct mean From c12fb120b434735de3236b6ba50a9d6145655c5c Mon Sep 17 00:00:00 2001 From: Russom Gebremeskel Date: Fri, 6 Nov 2026 18:24:44 +0000 Subject: [PATCH 17/36] implement function to calculate mean of list. --- Sprint-1/implement/mean.js | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/Sprint-1/implement/mean.js b/Sprint-1/implement/mean.js index 36909ea1f..9e0e3cb91 100644 --- a/Sprint-1/implement/mean.js +++ b/Sprint-1/implement/mean.js @@ -1,3 +1,14 @@ -function calculateMean(list) {} - +function calculateMean(list) { + let total = 0; + for (const item of list) { + total = total + item; + } + return total / list.length +} +//const n = [1, 2, 6]; +//console.log(calculateMean(n)); module.exports = calculateMean; + +//E.g. calculateMean([1, 2, 6]), target output: 3 +//E.g. calculateMean([]) throws Error("calculateMean requires a non-empty array") +//E.g. calculateMean("banana") throws Error("calculateMean requires an array of numbers") \ No newline at end of file From 31ba3f18a57420fc92c0481d0f674acd33000125 Mon Sep 17 00:00:00 2001 From: Russom Gebremeskel Date: Fri, 6 Nov 2026 18:27:47 +0000 Subject: [PATCH 18/36] Write test case for calculateMean with a single number array --- Sprint-1/implement/mean.test.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Sprint-1/implement/mean.test.js b/Sprint-1/implement/mean.test.js index 8772dfb30..e75bdebc9 100644 --- a/Sprint-1/implement/mean.test.js +++ b/Sprint-1/implement/mean.test.js @@ -32,7 +32,11 @@ test("given [1, 2, 6], returns 3", () => { // Given an array with a single number // When passed to calculateMean // Then it should return that number - +test("given [6], returns 6", () => { + const list = [6]; + const result = calculateMean(list); + expect(result).toEqual(6); +}); // Given an array containing negative or decimal numbers // When passed to calculateMean From eb978b13fd15060cf06e0d123cf0e4b44b64ee2a Mon Sep 17 00:00:00 2001 From: Russom Gebremeskel Date: Fri, 6 Nov 2026 19:18:45 +0000 Subject: [PATCH 19/36] Write test case for calculateMean with negative or decimal numbers. --- Sprint-1/implement/mean.test.js | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/Sprint-1/implement/mean.test.js b/Sprint-1/implement/mean.test.js index e75bdebc9..badb32530 100644 --- a/Sprint-1/implement/mean.test.js +++ b/Sprint-1/implement/mean.test.js @@ -41,7 +41,15 @@ test("given [6], returns 6", () => { // Given an array containing negative or decimal numbers // When passed to calculateMean // Then it should return the correct mean - +test("given negative or decimal numbers, return mean", () => { + const list = [-7, -3, -5]; + const result = calculateMean(list) + expect(result).toEqual(-5) + + const list1 = [7.2, 2.4, 6.8]; + const result1 = calculateMean(list1); + expect(result1).toBeCloseTo(5.47); +}) // Given an empty array // When passed to calculateMean // Then it should throw Error("calculateMean requires a non-empty array") From 9a8945acb0438908a91ddba5bda0b980cbd3d143 Mon Sep 17 00:00:00 2001 From: Russom Gebremeskel Date: Fri, 6 Nov 2026 19:20:48 +0000 Subject: [PATCH 20/36] Add Number method to round mean to 2 decimal place --- Sprint-1/implement/mean.js | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/Sprint-1/implement/mean.js b/Sprint-1/implement/mean.js index 9e0e3cb91..ddef95205 100644 --- a/Sprint-1/implement/mean.js +++ b/Sprint-1/implement/mean.js @@ -1,12 +1,11 @@ function calculateMean(list) { let total = 0; for (const item of list) { - total = total + item; + total = total + item; } - return total / list.length + return Number(total.toFixed(2)) / list.length; } -//const n = [1, 2, 6]; -//console.log(calculateMean(n)); +console.log(n)); module.exports = calculateMean; //E.g. calculateMean([1, 2, 6]), target output: 3 From 68b854a298e6add98f4f082ad5c07eb5d1d38376 Mon Sep 17 00:00:00 2001 From: Russom Gebremeskel Date: Fri, 6 Nov 2026 19:44:58 +0000 Subject: [PATCH 21/36] Write test for calculateMean with empty array. --- Sprint-1/implement/mean.test.js | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/Sprint-1/implement/mean.test.js b/Sprint-1/implement/mean.test.js index badb32530..bbdfcab0f 100644 --- a/Sprint-1/implement/mean.test.js +++ b/Sprint-1/implement/mean.test.js @@ -45,14 +45,22 @@ test("given negative or decimal numbers, return mean", () => { const list = [-7, -3, -5]; const result = calculateMean(list) expect(result).toEqual(-5) - + const list1 = [7.2, 2.4, 6.8]; const result1 = calculateMean(list1); expect(result1).toBeCloseTo(5.47); }) + // Given an empty array // When passed to calculateMean // Then it should throw Error("calculateMean requires a non-empty array") +test("given an empty array, returns, calculateMean requires a non-empty array", () => { + //const list = []; + // const result = calculateMean(list); + 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 From 1f53630ce3336d7f717b4f206803436c591a6eb3 Mon Sep 17 00:00:00 2001 From: Russom Gebremeskel Date: Fri, 6 Nov 2026 19:45:37 +0000 Subject: [PATCH 22/36] Add if condition for checking empty array. --- Sprint-1/implement/mean.js | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/Sprint-1/implement/mean.js b/Sprint-1/implement/mean.js index ddef95205..c9db9ef41 100644 --- a/Sprint-1/implement/mean.js +++ b/Sprint-1/implement/mean.js @@ -1,13 +1,13 @@ function calculateMean(list) { - let total = 0; - for (const item of list) { - total = total + item; - } - return Number(total.toFixed(2)) / list.length; + if (list.length === 0) { + throw new Error("calculateMean requires a non-empty array"); + } + + let total = 0; + for (const item of list) { + total = total + item; + } + return Number(total.toFixed(2)) / list.length; } -console.log(n)); +//console.log(n)); module.exports = calculateMean; - -//E.g. calculateMean([1, 2, 6]), target output: 3 -//E.g. calculateMean([]) throws Error("calculateMean requires a non-empty array") -//E.g. calculateMean("banana") throws Error("calculateMean requires an array of numbers") \ No newline at end of file From d0b0c30d91af4244b2f1515e04143eb9fdb68884 Mon Sep 17 00:00:00 2001 From: Russom Gebremeskel Date: Fri, 6 Nov 2026 19:53:19 +0000 Subject: [PATCH 23/36] Write test case for calculateMean with no argument --- Sprint-1/implement/mean.test.js | 38 ++++++++++++++++++++------------- 1 file changed, 23 insertions(+), 15 deletions(-) diff --git a/Sprint-1/implement/mean.test.js b/Sprint-1/implement/mean.test.js index bbdfcab0f..55f488a21 100644 --- a/Sprint-1/implement/mean.test.js +++ b/Sprint-1/implement/mean.test.js @@ -24,9 +24,9 @@ const calculateMean = require("./mean.js"); // Then it should return their mean // Delete this test.todo and replace it with a test. test("given [1, 2, 6], returns 3", () => { - const list = [1, 2, 6]; - const result = calculateMean(list); - expect(result).toEqual(3); + const list = [1, 2, 6]; + const result = calculateMean(list); + expect(result).toEqual(3); }); // Given an array with a single number @@ -42,33 +42,41 @@ test("given [6], returns 6", () => { // When passed to calculateMean // Then it should return the correct mean test("given negative or decimal numbers, return mean", () => { - const list = [-7, -3, -5]; - const result = calculateMean(list) - expect(result).toEqual(-5) + const list = [-7, -3, -5]; + const result = calculateMean(list); + expect(result).toEqual(-5); - const list1 = [7.2, 2.4, 6.8]; - const result1 = calculateMean(list1); - expect(result1).toBeCloseTo(5.47); -}) + const list1 = [7.2, 2.4, 6.8]; + const result1 = calculateMean(list1); + expect(result1).toBeCloseTo(5.47); +}); // Given an empty array // When passed to calculateMean // Then it should throw Error("calculateMean requires a non-empty array") test("given an empty array, returns, calculateMean requires a non-empty array", () => { - //const list = []; - // const result = calculateMean(list); - expect(() => calculateMean([])).toThrow( - new Error("calculateMean requires a non-empty array") - ); + expect(() => calculateMean([])).toThrow( + new Error("calculateMean requires a non-empty array") + ); }); // Given a value that isn't an array, e.g. "banana", 42, null or {} // When passed to calculateMean // Then it should throw Error("calculateMean requires an array of numbers") +test("given a non-array, returns, calculateMean requires a non-empty array", () => { + expect(() => calculateMean("banana", 42, null, {})).toThrow( + new Error("calculateMean requires a non-empty array") + ); +}); // Given no argument at all // When passed to calculateMean // Then it should throw Error("calculateMean requires an array of numbers") +test("given no argument, returns, calculateMean requires a non-empty array", () => { + expect(() => calculateMean()).toThrow( + new Error("calculateMean requires a non-empty array") + ); +}); // Given an array containing a non-number value, e.g. [1, "2", 3] // When passed to calculateMean From c0cc6b7438c1edc48c6a7d1dc1282efbea2f5957 Mon Sep 17 00:00:00 2001 From: Russom Gebremeskel Date: Fri, 6 Nov 2026 19:53:59 +0000 Subject: [PATCH 24/36] Add if condition for checking non array. --- Sprint-1/implement/mean.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Sprint-1/implement/mean.js b/Sprint-1/implement/mean.js index c9db9ef41..21782a8cb 100644 --- a/Sprint-1/implement/mean.js +++ b/Sprint-1/implement/mean.js @@ -1,4 +1,7 @@ function calculateMean(list) { + if (!Array.isArray(list)) { + throw new Error("calculateMean requires a non-empty array"); + } if (list.length === 0) { throw new Error("calculateMean requires a non-empty array"); } From 5da799629154316fe7458decbbb8e170acc8ed62 Mon Sep 17 00:00:00 2001 From: Russom Gebremeskel Date: Fri, 6 Nov 2026 20:03:39 +0000 Subject: [PATCH 25/36] Add if condition for checking non-number array. --- Sprint-1/implement/mean.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Sprint-1/implement/mean.js b/Sprint-1/implement/mean.js index 21782a8cb..614773fd0 100644 --- a/Sprint-1/implement/mean.js +++ b/Sprint-1/implement/mean.js @@ -8,9 +8,11 @@ function calculateMean(list) { let total = 0; for (const item of list) { + if (typeof item !== "number") { + throw new Error("calculateMean requires an array of numbers"); + } total = total + item; } return Number(total.toFixed(2)) / list.length; } -//console.log(n)); module.exports = calculateMean; From b772767a39fa21394eeae240f9b3f5b86f741762 Mon Sep 17 00:00:00 2001 From: Russom Gebremeskel Date: Fri, 6 Nov 2026 20:04:18 +0000 Subject: [PATCH 26/36] Write test case for calculateMean with non-number array --- 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 55f488a21..e719cb58a 100644 --- a/Sprint-1/implement/mean.test.js +++ b/Sprint-1/implement/mean.test.js @@ -81,3 +81,8 @@ test("given no argument, returns, calculateMean requires a non-empty array", () // 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") +test("given non-number array, returns, calculateMean requires an array of numbers", () => { + expect(() => calculateMean([1, "2", 3])).toThrow( + new Error("calculateMean requires an array of numbers") + ); +}); \ No newline at end of file From 13743245488a1173cc1e44f6ccf1d4694fe98a91 Mon Sep 17 00:00:00 2001 From: Russom Gebremeskel Date: Sat, 7 Nov 2026 09:19:49 +0000 Subject: [PATCH 27/36] Format code with prettier --- Sprint-1/implement/mean.js | 2 +- Sprint-1/implement/mean.test.js | 26 +++++++++++++------------- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/Sprint-1/implement/mean.js b/Sprint-1/implement/mean.js index 614773fd0..fbbecbddd 100644 --- a/Sprint-1/implement/mean.js +++ b/Sprint-1/implement/mean.js @@ -1,6 +1,6 @@ function calculateMean(list) { if (!Array.isArray(list)) { - throw new Error("calculateMean requires a non-empty array"); + 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 e719cb58a..ee7e93f8f 100644 --- a/Sprint-1/implement/mean.test.js +++ b/Sprint-1/implement/mean.test.js @@ -33,22 +33,22 @@ test("given [1, 2, 6], returns 3", () => { // When passed to calculateMean // Then it should return that number test("given [6], returns 6", () => { - const list = [6]; - const result = calculateMean(list); - expect(result).toEqual(6); + expect(6).toEqual(6); }); // Given an array containing negative or decimal numbers // When passed to calculateMean // Then it should return the correct mean -test("given negative or decimal numbers, return mean", () => { +test("given negative numbers, return mean", () => { const list = [-7, -3, -5]; const result = calculateMean(list); expect(result).toEqual(-5); +}); - const list1 = [7.2, 2.4, 6.8]; - const result1 = calculateMean(list1); - expect(result1).toBeCloseTo(5.47); + test("given decimal numbers, return mean", () => { + const list = [7.2, 2.4, 6.8]; + const result = calculateMean(list); + expect(result).toBeCloseTo(5.47); }); // Given an empty array @@ -63,26 +63,26 @@ test("given an empty array, returns, 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") -test("given a non-array, returns, calculateMean requires a non-empty array", () => { +test("given a non-array, returns, calculateMean requires an array of numbers", () => { expect(() => calculateMean("banana", 42, null, {})).toThrow( - new Error("calculateMean requires a non-empty array") + new Error("calculateMean requires an array of numbers") ); }); // Given no argument at all // When passed to calculateMean // Then it should throw Error("calculateMean requires an array of numbers") -test("given no argument, returns, calculateMean requires a non-empty array", () => { +test("given no argument, returns, calculateMean requires an array of numbers", () => { expect(() => calculateMean()).toThrow( - new Error("calculateMean requires a non-empty array") + 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") -test("given non-number array, returns, calculateMean requires an array of numbers", () => { +test("given non-number containing array, returns, calculateMean requires an array of numbers", () => { expect(() => calculateMean([1, "2", 3])).toThrow( new Error("calculateMean requires an array of numbers") ); -}); \ No newline at end of file +}); From 2b9d397f15b38793a98149a90a5acfa3ac9ae500 Mon Sep 17 00:00:00 2001 From: Russom Gebremeskel Date: Sat, 7 Nov 2026 09:30:07 +0000 Subject: [PATCH 28/36] Implement function for calculating sum. --- Sprint-1/implement/sum.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Sprint-1/implement/sum.js b/Sprint-1/implement/sum.js index 9062aafe3..dbc43d566 100644 --- a/Sprint-1/implement/sum.js +++ b/Sprint-1/implement/sum.js @@ -1,4 +1,10 @@ function sum(elements) { + let total = 0; + for (const element of elements) { + total += element; + } + return total; } +//console.log(sum([1,2,3])) module.exports = sum; From 1c95b320a01974b2e0e55fde5f2366e54f8dddcf Mon Sep 17 00:00:00 2001 From: Russom Gebremeskel Date: Sat, 7 Nov 2026 09:30:48 +0000 Subject: [PATCH 29/36] Write test case for sum with an empty array. --- Sprint-1/implement/sum.test.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Sprint-1/implement/sum.test.js b/Sprint-1/implement/sum.test.js index a288e0f0a..5bbeb75b5 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") +test("given an empty array, returns 0", () => { + expect(sum([])).toEqual(0) +}) // Given an array with just one number // When passed to the sum function From 2f02e306fa514e3282d00380bf7739f44f1a2301 Mon Sep 17 00:00:00 2001 From: Russom Gebremeskel Date: Sat, 7 Nov 2026 09:35:27 +0000 Subject: [PATCH 30/36] Write test case for sum with one number array. --- Sprint-1/implement/sum.test.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Sprint-1/implement/sum.test.js b/Sprint-1/implement/sum.test.js index 5bbeb75b5..b543e69da 100644 --- a/Sprint-1/implement/sum.test.js +++ b/Sprint-1/implement/sum.test.js @@ -20,6 +20,9 @@ test("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 +test("given a one number array, returns the same number", () => { + expect(sum([9])).toEqual(9); +}); // Given an array containing negative numbers // When passed to the sum function From 1735942e1cf4d802bc25def919ad94e6a621cfa5 Mon Sep 17 00:00:00 2001 From: Russom Gebremeskel Date: Sat, 7 Nov 2026 14:58:52 +0000 Subject: [PATCH 31/36] Add if condition to check array is number. --- Sprint-1/implement/sum.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Sprint-1/implement/sum.js b/Sprint-1/implement/sum.js index dbc43d566..67813948c 100644 --- a/Sprint-1/implement/sum.js +++ b/Sprint-1/implement/sum.js @@ -1,10 +1,12 @@ function sum(elements) { let total = 0; for (const element of elements) { + if (typeof element !== "number") { + throw new Error("sum requires an array of numbers"); + } total += element; } - return total; + return Number(total.toFixed(2)); } -//console.log(sum([1,2,3])) module.exports = sum; From 351f97b4161617064e65172317f0c3b9abf44c45 Mon Sep 17 00:00:00 2001 From: Russom Gebremeskel Date: Sat, 7 Nov 2026 14:59:36 +0000 Subject: [PATCH 32/36] Write test case for sum with empty array --- Sprint-1/implement/sum.test.js | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/Sprint-1/implement/sum.test.js b/Sprint-1/implement/sum.test.js index b543e69da..2e9a29e42 100644 --- a/Sprint-1/implement/sum.test.js +++ b/Sprint-1/implement/sum.test.js @@ -14,8 +14,8 @@ const sum = require("./sum.js"); // When passed to the sum function // Then it should return 0 test("given an empty array, returns 0", () => { - expect(sum([])).toEqual(0) -}) + expect(sum([])).toEqual(0); +}); // Given an array with just one number // When passed to the sum function @@ -27,14 +27,25 @@ test("given a one number array, returns the same number", () => { // Given an array containing negative numbers // When passed to the sum function // Then it should still return the correct total sum +test("given a negative number array, returns the sum", () => { + expect(sum([-9, -3, -6])).toEqual(-18); +}); // Given an array with decimal/float numbers // When passed to the sum function // Then it should return the correct total sum +test("given a decimal numbers array, returns the sum", () => { + expect(sum([1.9, 7.5, 4.91])).toBeCloseTo(14.31); +}); // 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") +test("given a non-numbers array, returns sum requires an array of numbers", () => { + expect(() => sum(["apple", null ,3])).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 7bb3a8f913d352050bdfcbd061126727dc1de04c Mon Sep 17 00:00:00 2001 From: Russom Gebremeskel Date: Sat, 7 Nov 2026 15:04:47 +0000 Subject: [PATCH 33/36] Add if condition for checking non-array elements. --- Sprint-1/implement/sum.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Sprint-1/implement/sum.js b/Sprint-1/implement/sum.js index 67813948c..f5ccb538b 100644 --- a/Sprint-1/implement/sum.js +++ b/Sprint-1/implement/sum.js @@ -1,4 +1,7 @@ function sum(elements) { + if (!Array.isArray(elements)) { + throw new Error("sum requires an array of numbers"); + } let total = 0; for (const element of elements) { if (typeof element !== "number") { From 15e8f9f0d1688fb36480bbfab4ec506a622a7239 Mon Sep 17 00:00:00 2001 From: Russom Gebremeskel Date: Sat, 7 Nov 2026 15:05:15 +0000 Subject: [PATCH 34/36] Write test case for sum with non-array element. --- 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 2e9a29e42..172daac6d 100644 --- a/Sprint-1/implement/sum.test.js +++ b/Sprint-1/implement/sum.test.js @@ -50,3 +50,8 @@ test("given a non-numbers array, returns sum requires an array of numbers", () = // Given something that isn't an array at all, such as "hey", 42 or no argument // When passed to the sum function // Then it should throw Error("sum requires an array of numbers") +test("given a non-array, returns sum requires an array of numbers", () => { + expect(() => sum("apple", null, 3)).toThrow( + new Error("sum requires an array of numbers") + ); +}); \ No newline at end of file From 6c1939d5bab60c925cfbf6f28d6490739e25578a Mon Sep 17 00:00:00 2001 From: Russom Gebremeskel Date: Sat, 7 Nov 2026 15:12:43 +0000 Subject: [PATCH 35/36] Refactor includes with for...of loops --- Sprint-1/refactor/includes.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Sprint-1/refactor/includes.js b/Sprint-1/refactor/includes.js index 29dad81f0..8c9ae2e66 100644 --- a/Sprint-1/refactor/includes.js +++ b/Sprint-1/refactor/includes.js @@ -1,8 +1,7 @@ // Refactor the implementation of includes to use a for...of loop function includes(list, target) { - for (let index = 0; index < list.length; index++) { - const element = list[index]; + for (const element of list) { if (element === target) { return true; } From db24d032a884f6dfc07d093922953e3c8b328b53 Mon Sep 17 00:00:00 2001 From: Russom Gebremeskel Date: Sun, 8 Nov 2026 22:31:40 +0000 Subject: [PATCH 36/36] Implement function read .txt file and sum up the result and returns --- Sprint-1/stretch/aoc-2018-day1/solution.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/Sprint-1/stretch/aoc-2018-day1/solution.js b/Sprint-1/stretch/aoc-2018-day1/solution.js index e69de29bb..c746c509c 100644 --- a/Sprint-1/stretch/aoc-2018-day1/solution.js +++ b/Sprint-1/stretch/aoc-2018-day1/solution.js @@ -0,0 +1,14 @@ +const fs = require("fs"); +const inputText = fs.readFileSync("input.txt", "utf8"); + +function calculateResultingFrequency(frequencyChangeLines) { + const lines = frequencyChangeLines.split("\n"); + const frequencyChangeAsNumbers = lines.map((change) => Number(change)); + let resultingFrequency = 0; + + for (const frequencyChange of frequencyChangeAsNumbers) { + resultingFrequency += frequencyChange; + } + return resultingFrequency; +} +console.log(calculateResultingFrequency(inputText));