From 1215e85e184676a8f21384fb5b639cca6a9611fa Mon Sep 17 00:00:00 2001 From: Ogbemi mene Date: Thu, 23 Jul 2026 21:49:17 +0100 Subject: [PATCH 1/8] commit for median --- Sprint-1/fix/median.js | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index b22590bc6..218be1029 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -6,9 +6,26 @@ // or 'list' has mixed values (the function is expected to sort only numbers). function calculateMedian(list) { - const middleIndex = Math.floor(list.length / 2); - const median = list.splice(middleIndex, 1)[0]; - return median; + if (!Array.isArray(list) || list.length === 0) { + return null; + } + const numbersOnly = list.filter((element) => typeof element === "number"); + if (numbersOnly.length === 0) { + return null; + } + + const sortedList = [...numbersOnly].sort((a, b) => a - b); + + if (sortedList.length % 2 === 0) { + const middleIndex = Math.floor(sortedList.length / 2); + return (sortedList[middleIndex - 1] + sortedList[middleIndex]) / 2; + } + const middleIndex = Math.floor(sortedList.length / 2); + + return sortedList[middleIndex]; } +const salaries = [10, 40, 50, 70, 90]; +const median = calculateMedian(salaries); + module.exports = calculateMedian; From 4be2913ea80be45b52eb8074e938a54e3872fdcf Mon Sep 17 00:00:00 2001 From: Ogbemi mene Date: Thu, 23 Jul 2026 21:52:01 +0100 Subject: [PATCH 2/8] commit for median test file --- Sprint-1/fix/median.test.js | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/Sprint-1/fix/median.test.js b/Sprint-1/fix/median.test.js index 21da654d7..c262c3776 100644 --- a/Sprint-1/fix/median.test.js +++ b/Sprint-1/fix/median.test.js @@ -13,7 +13,8 @@ describe("calculateMedian", () => { { input: [1, 2, 3, 4], expected: 2.5 }, { input: [1, 2, 3, 4, 5, 6], expected: 3.5 }, ].forEach(({ input, expected }) => - it(`returns the median for [${input}]`, () => expect(calculateMedian(input)).toEqual(expected)) + it(`returns the median for [${input}]`, () => + expect(calculateMedian(input)).toEqual(expected)) ); [ @@ -24,7 +25,8 @@ describe("calculateMedian", () => { { input: [110, 20, 0], expected: 20 }, { input: [6, -2, 2, 12, 14], expected: 6 }, ].forEach(({ input, expected }) => - it(`returns the correct median for unsorted array [${input}]`, () => expect(calculateMedian(input)).toEqual(expected)) + it(`returns the correct median for unsorted array [${input}]`, () => + expect(calculateMedian(input)).toEqual(expected)) ); it("doesn't modify the input array [3, 1, 2]", () => { @@ -33,8 +35,17 @@ describe("calculateMedian", () => { expect(list).toEqual([3, 1, 2]); }); - [ 'not an array', 123, null, undefined, {}, [], ["apple", null, undefined] ].forEach(val => - it(`returns null for non-numeric array (${val})`, () => expect(calculateMedian(val)).toBe(null)) + [ + "not an array", + 123, + null, + undefined, + {}, + [], + ["apple", null, undefined], + ].forEach((val) => + it(`returns null for non-numeric array (${val})`, () => + expect(calculateMedian(val)).toBe(null)) ); [ @@ -45,6 +56,7 @@ describe("calculateMedian", () => { { input: [3, "apple", 1, null, 2, undefined, 4], expected: 2.5 }, { input: ["banana", 5, 3, "apple", 1, 4, 2], expected: 3 }, ].forEach(({ input, expected }) => - it(`filters out non-numeric values and calculates the median for [${input}]`, () => expect(calculateMedian(input)).toEqual(expected)) + it(`filters out non-numeric values and calculates the median for [${input}]`, () => + expect(calculateMedian(input)).toEqual(expected)) ); }); From 9aecc90087aa519a7e5659424fc2037f152b7f47 Mon Sep 17 00:00:00 2001 From: Ogbemi mene Date: Thu, 23 Jul 2026 21:53:19 +0100 Subject: [PATCH 3/8] commit for dedupe --- Sprint-1/implement/dedupe.js | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/Sprint-1/implement/dedupe.js b/Sprint-1/implement/dedupe.js index 781e8718a..3e644b297 100644 --- a/Sprint-1/implement/dedupe.js +++ b/Sprint-1/implement/dedupe.js @@ -1 +1,9 @@ -function dedupe() {} +function dedupe(list) { + if (!Array.isArray(list)) { + return []; + } + + return [...new Set(list)]; +} + +module.exports = dedupe; From 9ef2d38ca15e884eafeff6fc41ad73fe0fa2beaa Mon Sep 17 00:00:00 2001 From: Ogbemi mene Date: Thu, 23 Jul 2026 21:54:44 +0100 Subject: [PATCH 4/8] commit for tdedupe test file --- Sprint-1/implement/dedupe.test.js | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/Sprint-1/implement/dedupe.test.js b/Sprint-1/implement/dedupe.test.js index d7c8e3d8e..4b7c7d9fe 100644 --- a/Sprint-1/implement/dedupe.test.js +++ b/Sprint-1/implement/dedupe.test.js @@ -16,7 +16,7 @@ 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.todo("given an empty array, it returns an empty array"); // Given an array with no duplicates // When passed to the dedupe function @@ -24,5 +24,30 @@ test.todo("given an empty array, it returns an empty array"); // Given an array of strings or numbers // When passed to the dedupe function -// Then it should return a new array with duplicates removed while preserving the +// Then it should return a new array with duplicates removed while preserving the // first occurrence of each element from the original array. + +describe("dedupe function", () => { + test("should remove duplicate elements from an array", () => { + expect(dedupe([1, 2, 2, 3, 1, 4])).toEqual([1, 2, 3, 4]); + expect(dedupe(["apple", "banana", "apple", "orange"])).toEqual([ + "apple", + "banana", + "orange", + ]); + }); + + test("should return the same array if there are no duplicates", () => { + expect(dedupe([1, 2, 3])).toEqual([1, 2, 3]); + }); + + test("should return an empty array if given an empty array", () => { + expect(dedupe([])).toEqual([]); + }); + + test("should return an empty array if given invalid input (non-arrays)", () => { + expect(dedupe(null)).toEqual([]); + expect(dedupe(undefined)).toEqual([]); + expect(dedupe("not an array")).toEqual([]); + }); +}); From 0a01de518ad8288d02256daecd629313730a11cf Mon Sep 17 00:00:00 2001 From: Ogbemi mene Date: Thu, 23 Jul 2026 21:55:52 +0100 Subject: [PATCH 5/8] commit for max function --- Sprint-1/implement/max.js | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/Sprint-1/implement/max.js b/Sprint-1/implement/max.js index 6dd76378e..9b1e2c0cb 100644 --- a/Sprint-1/implement/max.js +++ b/Sprint-1/implement/max.js @@ -1,4 +1,14 @@ function findMax(elements) { + if (!Array.isArray(elements) || elements.length === 0) { + return -Infinity; + } + const numbersOnly = elements.filter( + (item) => typeof item === "number" && !isNaN(item) + ); + if (numbersOnly.length === 0) { + return -Infinity; + } + return Math.max(...numbersOnly); } module.exports = findMax; From 69f993e6aff94ef2491f19a58acba04d3b80e5d3 Mon Sep 17 00:00:00 2001 From: Ogbemi mene Date: Thu, 23 Jul 2026 21:57:13 +0100 Subject: [PATCH 6/8] commit for max function test --- Sprint-1/implement/max.test.js | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/Sprint-1/implement/max.test.js b/Sprint-1/implement/max.test.js index 82f18fd88..3ad9ae9ca 100644 --- a/Sprint-1/implement/max.test.js +++ b/Sprint-1/implement/max.test.js @@ -41,3 +41,35 @@ test.todo("given an empty array, returns -Infinity"); // 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 + +describe("findMax function", () => { + test("should return the maximum number in an array of positive numbers", () => { + expect(findMax([1, 5, 3, 9, 2])).toBe(9); + }); + + test("should return the maximum number in an array with negative numbers", () => { + expect(findMax([-10, -3, -50, -1])).toBe(-1); + }); + + test("should handle arrays with mixed types and ignore non-numbers", () => { + expect(findMax([1, "apple", 5, null, true, 3])).toBe(5); + }); + + test("should ignore NaN values", () => { + expect(findMax([1, NaN, 10, 2])).toBe(10); + }); + + test("should return -Infinity if given an empty array", () => { + expect(findMax([])).toBe(-Infinity); + }); + + test("should return -Infinity if given an array with no valid numbers", () => { + expect(findMax(["a", "b", null, NaN])).toBe(-Infinity); + }); + + test("should return -Infinity for non-array inputs", () => { + expect(findMax(null)).toBe(-Infinity); + expect(findMax(undefined)).toBe(-Infinity); + expect(findMax("hello")).toBe(-Infinity); + }); +}); From 9e0cfa8063dabff7501983f47ae3f13522bbace7 Mon Sep 17 00:00:00 2001 From: Ogbemi mene Date: Thu, 23 Jul 2026 21:58:57 +0100 Subject: [PATCH 7/8] commit for sum function --- Sprint-1/implement/sum.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/Sprint-1/implement/sum.js b/Sprint-1/implement/sum.js index 9062aafe3..569b20a2f 100644 --- a/Sprint-1/implement/sum.js +++ b/Sprint-1/implement/sum.js @@ -1,4 +1,17 @@ function sum(elements) { + if (!Array.isArray(elements) || elements.length === 0) { + return 0; + } + + const numbersOnly = elements.filter( + (item) => typeof item === "number" && !isNaN(item) + ); + + if (numbersOnly.length === 0) { + return 0; + } + + return numbersOnly.reduce((acc, curr) => acc + curr, 0); } module.exports = sum; From 5b6c9c2b0ee0647cc8013fbd17b40a6376147534 Mon Sep 17 00:00:00 2001 From: Ogbemi mene Date: Thu, 23 Jul 2026 22:00:18 +0100 Subject: [PATCH 8/8] commit for sum.test function --- Sprint-1/implement/sum.test.js | 41 +++++++++++++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/Sprint-1/implement/sum.test.js b/Sprint-1/implement/sum.test.js index dd0a090ca..d2b58bf6e 100644 --- a/Sprint-1/implement/sum.test.js +++ b/Sprint-1/implement/sum.test.js @@ -13,7 +13,7 @@ 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.todo("given an empty array, returns 0"); // Given an array with just one number // When passed to the sum function @@ -34,3 +34,42 @@ test.todo("given an empty array, returns 0") // 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 + +describe("sum function", () => { + // Given an empty array + test("given an empty array, returns 0", () => { + expect(sum([])).toBe(0); + }); + + // Given an array with just one number + test("given an array with just one number, returns that number", () => { + expect(sum([42])).toBe(42); + }); + + // Given an array containing negative numbers + test("given an array containing negative numbers, returns the correct total sum", () => { + expect(sum([10, -5, 20, -15])).toBe(10); + }); + + // Given an array with decimal/float numbers + test("given an array with decimal/float numbers, returns the correct total sum", () => { + expect(sum([1.5, 2.25, 3.25])).toBe(7); + }); + + // Given an array containing non-number values + test("given an array containing non-number values, ignores non-numerical values", () => { + expect(sum(["hey", 10, "hi", 60, 10])).toBe(80); + expect(sum([10, true, null, undefined, NaN, 20])).toBe(30); + }); + + // Given an array with only non-number values + test("given an array with only non-number values, returns 0", () => { + expect(sum(["apple", "banana", true, null])).toBe(0); + }); + + // Edge case: Non-array inputs + test("given non-array inputs, returns 0", () => { + expect(sum(null)).toBe(0); + expect(sum("not an array")).toBe(0); + }); +});