From e628a411d6345a712e8ee48248858d3a60a82956 Mon Sep 17 00:00:00 2001 From: Daniel Aderibigbe Date: Mon, 9 Mar 2026 21:50:30 +0000 Subject: [PATCH 1/5] fix exercise completed --- Sprint-1/fix/median.js | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index b22590bc6..e618192d5 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -6,9 +6,22 @@ // or 'list' has mixed values (the function is expected to sort only numbers). function calculateMedian(list) { + if (!Array.isArray(list)) { + return null; + } + list = list.filter((item) => typeof item === "number"); + if (list.length === 0) { + return null; + } + list = [...list].sort((a, b) => a - b); + const middleIndex = Math.floor(list.length / 2); - const median = list.splice(middleIndex, 1)[0]; - return median; + const median = list[middleIndex]; + if (list.length % 2) { + return median; + } else { + return (median + list[middleIndex - 1]) / 2; + } } module.exports = calculateMedian; From e2f4c9946a103c3b92408d8992ca1845fa8156cf Mon Sep 17 00:00:00 2001 From: Daniel Aderibigbe Date: Mon, 9 Mar 2026 22:15:56 +0000 Subject: [PATCH 2/5] dedupe exercise completed. --- Sprint-1/implement/dedupe.js | 13 ++++++++++++- Sprint-1/implement/dedupe.test.js | 16 +++++++++++++++- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/Sprint-1/implement/dedupe.js b/Sprint-1/implement/dedupe.js index 781e8718a..619b9ae35 100644 --- a/Sprint-1/implement/dedupe.js +++ b/Sprint-1/implement/dedupe.js @@ -1 +1,12 @@ -function dedupe() {} +function dedupe(array) { + const result = []; + + for (let i = 0; i < array.length; i++) + if (!result.includes(array[i])) { + result.push(array[i]); + } + + return result; +} + +module.exports = dedupe; diff --git a/Sprint-1/implement/dedupe.test.js b/Sprint-1/implement/dedupe.test.js index 23e0f8638..05b308ded 100644 --- a/Sprint-1/implement/dedupe.test.js +++ b/Sprint-1/implement/dedupe.test.js @@ -16,12 +16,26 @@ E.g. dedupe([1, 2, 1]) target output: [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 input = []; + const expectedOutput = []; + expect(dedupe(input)).toEqual(expectedOutput); +}); // 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 input = [1, 2, 3]; + const expectedOutput = [1, 2, 3]; + expect(dedupe(input)).toEqual(expectedOutput); +}); // Given an array with strings or numbers // When passed to the dedupe function // Then it should remove the duplicate values, preserving the first occurence of each element +test("given an array with strings or numbers, it removes the duplicate values, preserving the first occurence of each element", () => { + const input = [5, 1, 1, 2, 3, 2, 5, 8, "egg", "egg", "bacon"]; + const expectedOutput = [5, 1, 2, 3, 8, "egg", "bacon"]; + expect(dedupe(input)).toEqual(expectedOutput); +}); From d12874146d76a6b61b155dc2f58ab2c4c24b17ac Mon Sep 17 00:00:00 2001 From: Daniel Aderibigbe Date: Mon, 9 Mar 2026 22:30:59 +0000 Subject: [PATCH 3/5] max exercise completed --- Sprint-1/implement/max.js | 8 ++++++++ Sprint-1/implement/max.test.js | 36 +++++++++++++++++++++++++++++++++- 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/Sprint-1/implement/max.js b/Sprint-1/implement/max.js index 6dd76378e..7fd4f65dc 100644 --- a/Sprint-1/implement/max.js +++ b/Sprint-1/implement/max.js @@ -1,4 +1,12 @@ function findMax(elements) { + let max = -Infinity; + + for (let i = 0; i < elements.length; i++) { + if (typeof elements[i] === "number" && elements[i] > max) { + max = elements[i]; + } + } + return max; } module.exports = findMax; diff --git a/Sprint-1/implement/max.test.js b/Sprint-1/implement/max.test.js index 82f18fd88..2a3958ebc 100644 --- a/Sprint-1/implement/max.test.js +++ b/Sprint-1/implement/max.test.js @@ -16,28 +16,62 @@ 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", () => { + const input = []; + const expectedOutput = -Infinity; + expect(findMax(input)).toBe(expectedOutput); +}); // Given an array with one number // When passed to the max function // Then it should return that number +test("given an array with one number, it returns that number", () => { + const input = [42]; + const expectedOutput = 42; + expect(findMax(input)).toBe(expectedOutput); +}); // 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 both positive and negative numbers, it returns the largest number overall", () => { + const input = [-10, -20, 5, 0, -1]; + const expectedOutput = 5; + expect(findMax(input)).toBe(expectedOutput); +}); // 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 just negative numbers, it returns the closest one to zero", () => { + const input = [-10, -20, -5, -1]; + const expectedOutput = -1; + expect(findMax(input)).toBe(expectedOutput); +}); // 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, it returns the largest decimal number", () => { + const input = [1.5, 2.3, 0.7, 2.9]; + const expectedOutput = 2.9; + expect(findMax(input)).toBe(expectedOutput); +}); // Given an array with non-number values // When passed to the max function // Then it should return the max and ignore non-numeric values +test("given an array with non-number values, it returns the max and ignore non-numeric values", () => { + const input = [10, "hello", 20, null, 5]; + const expectedOutput = 20; + expect(findMax(input)).toBe(expectedOutput); +}); // Given an array with only non-number values // When passed to the max function // Then it should return the least surprising value given how it behaves for all other inputs +test("given an array with only non-number values, it returns the least surprising value", () => { + const input = ["hello", "world"]; + const expectedOutput = -Infinity; + expect(findMax(input)).toBe(expectedOutput); +}); From 45812d4b3e967e758ceb972574161153ee44cb4c Mon Sep 17 00:00:00 2001 From: Daniel Aderibigbe Date: Mon, 9 Mar 2026 22:35:23 +0000 Subject: [PATCH 4/5] sumtest exercise completed. --- Sprint-1/implement/sum.js | 8 ++++++ Sprint-1/implement/sum.test.js | 49 +++++++++++++++++++++++++++++++++- 2 files changed, 56 insertions(+), 1 deletion(-) diff --git a/Sprint-1/implement/sum.js b/Sprint-1/implement/sum.js index 9062aafe3..ceb9800b6 100644 --- a/Sprint-1/implement/sum.js +++ b/Sprint-1/implement/sum.js @@ -1,4 +1,12 @@ function sum(elements) { + let total = 0; + + for (let i = 0; i < elements.length; i++) { + if (typeof elements[i] === "number") { + total += elements[i]; + } + } + return total; } module.exports = sum; diff --git a/Sprint-1/implement/sum.test.js b/Sprint-1/implement/sum.test.js index dd0a090ca..cbae1ebb2 100644 --- a/Sprint-1/implement/sum.test.js +++ b/Sprint-1/implement/sum.test.js @@ -13,24 +13,71 @@ 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", () => { + const input = []; + const expectedOutput = 0; + expect(sum(input)).toBe(expectedOutput); +}); // Given an array with just one number // When passed to the sum function // Then it should return that number +test("given an array with just one number, returns that number", () => { + const input = [5]; + const expectedOutput = 5; + expect(sum(input)).toBe(expectedOutput); +}); // Given an array containing negative numbers // When passed to the sum function // Then it should still return the correct total sum +test("given an array containing negative numbers, it returns the correct total sum", () => { + const input = [10, -5, 20, -3]; + const expectedOutput = 22; + expect(sum(input)).toBe(expectedOutput); +}); // Given an array with decimal/float numbers // When passed to the sum function // Then it should return the correct total sum +test("given an array with decimal numbers, it returns the correct total sum", () => { + const input = [1.5, 2.3, 0.7]; + const expectedOutput = 4.5; + expect(sum(input)).toBe(expectedOutput); +}); + +// Given an array with decimal/float numbers +// When passed to the sum function +// Then it should return the correct total sum +test("given an array with decimal numbers, it returns the correct total sum", () => { + const input = [1.5, 2.3, 0.7]; + const expectedOutput = 4.5; + expect(sum(input)).toBe(expectedOutput); +}); + +// Given an array containing non-number values +// When passed to the sum function +// Then it should ignore the non-numerical values and return the sum of the numerical elements +test("given an array containing non-number values, it ignores them and returns the sum of the numerical elements", () => { + const input = [10, "hello", 20, null, 5]; + const expectedOutput = 35; + expect(sum(input)).toBe(expectedOutput); +}); // Given an array containing non-number values // When passed to the sum function // Then it should ignore the non-numerical values and return the sum of the numerical elements +test("given an array containing non-number values, it ignores them and returns the sum of the numerical elements", () => { + const input = ["hello", "world"]; + const expectedOutput = 0; + expect(sum(input)).toBe(expectedOutput); +}); // Given an array with only non-number values // When passed to the sum function // Then it should return the least surprising value given how it behaves for all other inputs +test("given an array with only non-number values, it returns the least surprising value", () => { + const input = ["hello", "world"]; + const expectedOutput = 0; + expect(sum(input)).toBe(expectedOutput); +}); From e0ff1019e9e1f1f7d19215aa7e52674576efca18 Mon Sep 17 00:00:00 2001 From: Daniel Aderibigbe Date: Mon, 9 Mar 2026 22:45:26 +0000 Subject: [PATCH 5/5] refactor and stretch exercise completed. --- Sprint-1/refactor/includes.js | 3 +-- Sprint-1/stretch/aoc-2018-day1/solution.js | 3 +++ 2 files changed, 4 insertions(+), 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; } diff --git a/Sprint-1/stretch/aoc-2018-day1/solution.js b/Sprint-1/stretch/aoc-2018-day1/solution.js index e69de29bb..030a19d16 100644 --- a/Sprint-1/stretch/aoc-2018-day1/solution.js +++ b/Sprint-1/stretch/aoc-2018-day1/solution.js @@ -0,0 +1,3 @@ +function sum(list) { + return list.reduce((a, b) => a + b, 0); +}