From 8ef0e3daf1f759cad0de66287df11cdd13bf831f Mon Sep 17 00:00:00 2001 From: Oussama Mouggal Date: Tue, 3 Mar 2026 13:03:31 +0000 Subject: [PATCH 1/5] calculated Median using function that handle non-array inputs and mixed values --- Sprint-1/fix/median.js | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index b22590bc6..6ead83668 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -6,9 +6,23 @@ // 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)) { + return null; + } +const numbers = list.filter(val => typeof val === 'number' && !isNaN(val)); +if (numbers.length === 0) { + return null; + } + numbers.sort((a, b) => a - b); + + const length = numbers.length; + const middle = Math.floor(length / 2); + + if (length % 2 === 1) { + return numbers[middle]; + } else { + return (numbers[middle - 1] + numbers[middle]) / 2; + } } module.exports = calculateMedian; From 4d632dc74b4f61318664247edfeecaf019920fe3 Mon Sep 17 00:00:00 2001 From: Oussama Mouggal Date: Tue, 3 Mar 2026 13:16:17 +0000 Subject: [PATCH 2/5] Implement dedupe function and add test cases --- Sprint-1/implement/dedupe.js | 10 +++++++++- Sprint-1/implement/dedupe.test.js | 16 +++++++++++++++- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/Sprint-1/implement/dedupe.js b/Sprint-1/implement/dedupe.js index 781e8718a..a5dde2b74 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; diff --git a/Sprint-1/implement/dedupe.test.js b/Sprint-1/implement/dedupe.test.js index 23e0f8638..0c609a0ed 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", () => { + expect(dedupe([])).toEqual([]); +}); // Given an array with no duplicates // When passed to the dedupe function // Then it should return a copy of the original array +test("given an array with no duplicates, it returns a copy of the original array", () => { + const input = [1, 2, 3, 4]; + const result = dedupe(input); + + expect(result).toEqual([1, 2, 3, 4]); + expect(result).not.toBe(input); +}); // 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 duplicates and preserves first occurrence", () => { + expect(dedupe(["a", "a", "a", "b", "b", "c"])) .toEqual(["a", "b", "c"]); + expect(dedupe([5, 1, 1, 2, 3, 2, 5, 8])).toEqual([5, 1, 2, 3, 8]); + expect(dedupe([1, 2, 1])).toEqual([1, 2]); +}); From 77aeaf42194c54b641db443d04059439d8a0be7e Mon Sep 17 00:00:00 2001 From: Oussama Mouggal Date: Tue, 3 Mar 2026 13:23:58 +0000 Subject: [PATCH 3/5] worked with findMax function and added test cases --- Sprint-1/implement/max.js | 6 ++++++ Sprint-1/implement/max.test.js | 23 ++++++++++++++++++++++- 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/Sprint-1/implement/max.js b/Sprint-1/implement/max.js index 6dd76378e..dc9d9ffef 100644 --- a/Sprint-1/implement/max.js +++ b/Sprint-1/implement/max.js @@ -1,4 +1,10 @@ function findMax(elements) { + if (!Array.isArray(elements)) { + return -Infinity; + } + + const numbers = elements.filter((element) => typeof element === "number" && !Number.isNaN(element)); + return numbers.length === 0 ? -Infinity : Math.max(...numbers); } module.exports = findMax; diff --git a/Sprint-1/implement/max.test.js b/Sprint-1/implement/max.test.js index 82f18fd88..852ec1cb0 100644 --- a/Sprint-1/implement/max.test.js +++ b/Sprint-1/implement/max.test.js @@ -16,28 +16,49 @@ 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([])).toBe(-Infinity); +}); // 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, returns that number", () => { + expect(findMax([42])).toBe(42); +}); // 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 positive and negative numbers, returns the largest overall", () => { + expect(findMax([-10, 4, -2, 15, 7])).toBe(15); +}); // Given an array with just negative numbers // When passed to the max function // Then it should return the closest one to zero +test("given only negative numbers, returns the closest one to zero", () => { + expect(findMax([-8, -3, -20, -11])).toBe(-3); +}); // Given an array with decimal numbers // When passed to the max function // Then it should return the largest decimal number +test("given decimal numbers, returns the largest decimal", () => { + expect(findMax([1.2, 3.14, 3.139, -0.5])).toBe(3.14); +}); // 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 non-number values, ignores them and returns max number", () => { + expect(findMax(["hey", 10, "hi", 60, 10])).toBe(60); + expect(findMax([null, undefined, 4, "9", true, 2])).toBe(4); +}); // 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 only non-number values, returns -Infinity", () => { + expect(findMax(["a", null, undefined, false, {}])).toBe(-Infinity); +}); From e9f131d4266c1231d72a06f958b87aa1e9bed415 Mon Sep 17 00:00:00 2001 From: Oussama Mouggal Date: Tue, 3 Mar 2026 13:28:24 +0000 Subject: [PATCH 4/5] Add tests for sum function --- Sprint-1/implement/sum.js | 7 +++++++ Sprint-1/implement/sum.test.js | 20 +++++++++++++++++++- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/Sprint-1/implement/sum.js b/Sprint-1/implement/sum.js index 9062aafe3..ab9045883 100644 --- a/Sprint-1/implement/sum.js +++ b/Sprint-1/implement/sum.js @@ -1,4 +1,11 @@ function sum(elements) { + if (!Array.isArray(elements)) { + return 0; + } + + return elements + .filter((element) => typeof element === "number" && !Number.isNaN(element)) + .reduce((total, number) => total + number, 0); } module.exports = sum; diff --git a/Sprint-1/implement/sum.test.js b/Sprint-1/implement/sum.test.js index dd0a090ca..47d71a748 100644 --- a/Sprint-1/implement/sum.test.js +++ b/Sprint-1/implement/sum.test.js @@ -13,24 +13,42 @@ 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([])).toBe(0); +}); // Given an array with just one number // When passed to the sum function // Then it should return that number +test("given an array with one number, returns that number", () => { + expect(sum([42])).toBe(42); +}); // 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 with negative numbers, returns correct total", () => { + expect(sum([10, -5, -2, 3])).toBe(6); +}); // Given an array with decimal/float numbers // When passed to the sum function // Then it should return the correct total sum +test("given decimal numbers, returns correct total", () => { + expect(sum([1.5, 2.5, 3.25])).toBe(7.25); +}); // 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 non-number values, ignores them and sums numbers", () => { + expect(sum(["hey", 10, "hi", 60, 10])).toBe(80); + expect(sum([null, 4, "5", true, undefined, 6])).toBe(10); +}); // 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 only non-number values, returns 0", () => { + expect(sum(["a", null, undefined, false, {}])).toBe(0); +}); From 8b8b7a83e448a6f8e35dd49984f581a496b0ec92 Mon Sep 17 00:00:00 2001 From: Oussama Mouggal Date: Tue, 3 Mar 2026 13:41:06 +0000 Subject: [PATCH 5/5] changed for loop to (for .. of) for cleaner code --- 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; }