From dcdec4581b359239954c1ba53df23b579c40b947 Mon Sep 17 00:00:00 2001 From: Fares Bakhet Date: Tue, 4 Nov 2025 16:18:36 +0000 Subject: [PATCH 01/14] Fix the implementation --- Sprint-1/fix/median.js | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index b22590bc6..47b30af08 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) { - const middleIndex = Math.floor(list.length / 2); - const median = list.splice(middleIndex, 1)[0]; - return median; + // Make sure input is an array + if (!Array.isArray(list)) return null; + + // Filter only valid numbers (exclude NaN, null, strings, etc.) + const numbers = list.filter((n) => typeof n === "number" && !isNaN(n)); + + if (numbers.length === 0) return null; + + const sorted = [...numbers].sort((a, b) => a - b); + + const mid = Math.floor(sorted.length / 2); + + if (sorted.length % 2 === 0) { + return (sorted[mid - 1] + sorted[mid]) / 2; + } + return sorted[mid]; } module.exports = calculateMedian; From 589c8d9c94990ddaf0f08e976b4bdfffd072ad0f Mon Sep 17 00:00:00 2001 From: Fares Bakhet Date: Tue, 4 Nov 2025 17:15:40 +0000 Subject: [PATCH 02/14] Implement a Deduplicate function --- Sprint-1/implement/dedupe.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/Sprint-1/implement/dedupe.js b/Sprint-1/implement/dedupe.js index 781e8718a..7d9f034c6 100644 --- a/Sprint-1/implement/dedupe.js +++ b/Sprint-1/implement/dedupe.js @@ -1 +1,6 @@ -function dedupe() {} +function dedupe(arr) { + const deduplicates = arr.filter((item, index) => arr.indexOf(item) === index); + return deduplicates; +} + +module.exports = dedupe; From 3ebe1f91daedaab99c61b9c2aa9fcfba6f422540 Mon Sep 17 00:00:00 2001 From: Fares Bakhet Date: Tue, 4 Nov 2025 17:16:51 +0000 Subject: [PATCH 03/14] Run tests for the Deduplicate function --- Sprint-1/implement/dedupe.test.js | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/Sprint-1/implement/dedupe.test.js b/Sprint-1/implement/dedupe.test.js index 23e0f8638..f51ca1deb 100644 --- a/Sprint-1/implement/dedupe.test.js +++ b/Sprint-1/implement/dedupe.test.js @@ -16,12 +16,36 @@ 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", () => { + [{ input: [], expected: [] }].forEach(({ input, expected }) => { + expect(dedupe(input)).toEqual(expected); + }); +}); // 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", () => { + [ + { input: [1, 2, 3], expected: [1, 2, 3] }, + { input: ["a", "b", "c"], expected: ["a", "b", "c"] }, + { input: [true, false], expected: [true, false] }, + ].forEach(({ input, expected }) => { + expect(dedupe(input)).toEqual(expected); + }); +}); + // 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 duplicate values preserving the first occurrence of each element", () => { + [ + { input: [1, 2, 2, 3, 3, 3], expected: [1, 2, 3] }, + { input: ["a", "b", "b", "c", "c", "c"], expected: ["a", "b", "c"] }, + { input: [true, false, false, true], expected: [true, false] }, + ].forEach(({ input, expected }) => { + expect(dedupe(input)).toEqual(expected); + }); +}); From 88176351e6e494bb24aa8f771ee84535f5d490b3 Mon Sep 17 00:00:00 2001 From: Fares Bakhet Date: Tue, 4 Nov 2025 19:30:35 +0000 Subject: [PATCH 04/14] Implement findMax function --- Sprint-1/implement/max.js | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/Sprint-1/implement/max.js b/Sprint-1/implement/max.js index 6dd76378e..e76ebd3d4 100644 --- a/Sprint-1/implement/max.js +++ b/Sprint-1/implement/max.js @@ -1,4 +1,23 @@ -function findMax(elements) { +function findMax(num) { + for (let i = 0; i < num.length; i++) { + if (num.length === 0) { + return -Infinity; + } + if (num.length === 1) { + return num[0]; + } + if (Math.sign(num) === -1 && num.length > 1) { + return Math.max(...num); + } + if (num.isDecimal === true) { + return Math.max(...num); + } + if (typeof num[i] !== "number") { + num.splice(i, 1); + i--; + } + } + return Math.max(...num); } module.exports = findMax; From 744af5da7531ddda150002341fd629ec603ba6da Mon Sep 17 00:00:00 2001 From: Fares Bakhet Date: Tue, 4 Nov 2025 19:31:29 +0000 Subject: [PATCH 05/14] Adding tests for max.js file --- Sprint-1/implement/max.test.js | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/Sprint-1/implement/max.test.js b/Sprint-1/implement/max.test.js index 82f18fd88..663e9f867 100644 --- a/Sprint-1/implement/max.test.js +++ b/Sprint-1/implement/max.test.js @@ -16,28 +16,48 @@ 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 an array with both positive and negative numbers, returns the largest number", () => { + expect(findMax([30, -10, 50, -20, 40])).toBe(50); +}); // 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, returns the closest one to zero", () => { + expect(findMax([-30, -10, -50, -20, -40])).toBe(-10); +}); // 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", () => { + expect(findMax([3.5, 2.1, 4.8, 1.9])).toBe(4.8); +}); // 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, returns the max and ignores non-numeric values", () => { + expect(findMax(["hey", 10, "hi", 60, 10])).toBe(60); +}); // 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, returns the least surprising value", () => { + expect(findMax(["a", "b", "c"])).toBe("c"); +}); From 61a84cea5f5ac1ed271774c02582def633a4efcb Mon Sep 17 00:00:00 2001 From: Fares Bakhet Date: Wed, 5 Nov 2025 17:26:52 +0000 Subject: [PATCH 06/14] Implementing a sum function --- Sprint-1/implement/sum.js | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/Sprint-1/implement/sum.js b/Sprint-1/implement/sum.js index 9062aafe3..c9124b3a3 100644 --- a/Sprint-1/implement/sum.js +++ b/Sprint-1/implement/sum.js @@ -1,4 +1,15 @@ +const { array, string } = require("yargs"); + function sum(elements) { + let totalSum = 0; + for (let i = 0; i < elements.length; i++) { + if (typeof elements[i] === "number") { + totalSum += elements[i]; + } + } + return totalSum; } +console.log(sum(["cow", "true", "ana"])); + module.exports = sum; From 1762ae053f76456f25e8f3f42fd5149ddd04caed Mon Sep 17 00:00:00 2001 From: Fares Bakhet Date: Wed, 5 Nov 2025 17:27:25 +0000 Subject: [PATCH 07/14] Run tests for the sum.js file --- Sprint-1/implement/sum.test.js | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/Sprint-1/implement/sum.test.js b/Sprint-1/implement/sum.test.js index dd0a090ca..8310875bb 100644 --- a/Sprint-1/implement/sum.test.js +++ b/Sprint-1/implement/sum.test.js @@ -13,24 +13,41 @@ 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 containing negative numbers, returns the correct total sum", () => { + expect(sum([-1, -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 an array with decimal numbers, returns the correct total sum", () => { + expect(sum([1.5, 2.5, 3.5])).toBe(7.5); +}); // 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 with non-number values, ignores them and returns the sum of numbers", () => { + expect(sum([10, "hello", 20, true, 30, null])).toBe(60); +}); // 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, returns 0", () => { + expect(sum(["hello", true, null])).toBe(0); +}); From bf825f1e693f35454fcd087feaadcb3bf446b536 Mon Sep 17 00:00:00 2001 From: Fares Bakhet Date: Sat, 8 Nov 2025 15:54:32 +0000 Subject: [PATCH 08/14] Editing the code --- Sprint-1/implement/sum.js | 4 ---- 1 file changed, 4 deletions(-) diff --git a/Sprint-1/implement/sum.js b/Sprint-1/implement/sum.js index c9124b3a3..ff5d88da0 100644 --- a/Sprint-1/implement/sum.js +++ b/Sprint-1/implement/sum.js @@ -1,5 +1,3 @@ -const { array, string } = require("yargs"); - function sum(elements) { let totalSum = 0; for (let i = 0; i < elements.length; i++) { @@ -10,6 +8,4 @@ function sum(elements) { return totalSum; } -console.log(sum(["cow", "true", "ana"])); - module.exports = sum; From c8d37344ee2dbc384e50149973d742e7a2ffc170 Mon Sep 17 00:00:00 2001 From: Fares Bakhet Date: Sat, 8 Nov 2025 16:04:24 +0000 Subject: [PATCH 09/14] Refactoring the implementation --- Sprint-1/refactor/includes.js | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/Sprint-1/refactor/includes.js b/Sprint-1/refactor/includes.js index 29dad81f0..8125a2408 100644 --- a/Sprint-1/refactor/includes.js +++ b/Sprint-1/refactor/includes.js @@ -1,8 +1,17 @@ // 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) { +// return true; +// } +// } +// return false; +// } + 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 cd9b36a83bf5a0895e70a4637cbcd5d9d7e41984 Mon Sep 17 00:00:00 2001 From: Fares Bakhet Date: Wed, 26 Nov 2025 16:26:19 +0000 Subject: [PATCH 10/14] fix deupe.test.js file --- Sprint-1/implement/dedupe.test.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Sprint-1/implement/dedupe.test.js b/Sprint-1/implement/dedupe.test.js index f51ca1deb..8a19aa4a4 100644 --- a/Sprint-1/implement/dedupe.test.js +++ b/Sprint-1/implement/dedupe.test.js @@ -32,7 +32,11 @@ test("given an array with no duplicates, it returns a copy of the original array { input: ["a", "b", "c"], expected: ["a", "b", "c"] }, { input: [true, false], expected: [true, false] }, ].forEach(({ input, expected }) => { + const result = dedupe(input); + expect(dedupe(input)).toEqual(expected); + expect(result).not.toBe(input); + }); }); From 9420f6aa185b8e1817095d71de5f10c949907fde Mon Sep 17 00:00:00 2001 From: Fares Bakhet Date: Wed, 26 Nov 2025 16:26:55 +0000 Subject: [PATCH 11/14] fix file --- Sprint-1/fix/median.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index 47b30af08..e287dba40 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -14,10 +14,10 @@ function calculateMedian(list) { if (numbers.length === 0) return null; - const sorted = [...numbers].sort((a, b) => a - b); - const mid = Math.floor(sorted.length / 2); + const sorted = numbers.sort((a, b) => a - b); + if (sorted.length % 2 === 0) { return (sorted[mid - 1] + sorted[mid]) / 2; } From c8f6f8f165faef174d1de4fddd5f2aa59f334085 Mon Sep 17 00:00:00 2001 From: Fares Bakhet Date: Wed, 26 Nov 2025 16:35:01 +0000 Subject: [PATCH 12/14] fix max.js file --- Sprint-1/implement/max.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sprint-1/implement/max.js b/Sprint-1/implement/max.js index e76ebd3d4..ac6212f31 100644 --- a/Sprint-1/implement/max.js +++ b/Sprint-1/implement/max.js @@ -9,7 +9,7 @@ function findMax(num) { if (Math.sign(num) === -1 && num.length > 1) { return Math.max(...num); } - if (num.isDecimal === true) { + if (num.isInteger === true) { return Math.max(...num); } if (typeof num[i] !== "number") { From 89e0e23d6d44199aa748c00847935e45208a037e Mon Sep 17 00:00:00 2001 From: Fares Bakhet Date: Wed, 26 Nov 2025 16:55:45 +0000 Subject: [PATCH 13/14] Editing max.js file --- Sprint-1/implement/max.js | 28 +++++++++------------------- 1 file changed, 9 insertions(+), 19 deletions(-) diff --git a/Sprint-1/implement/max.js b/Sprint-1/implement/max.js index ac6212f31..b79915807 100644 --- a/Sprint-1/implement/max.js +++ b/Sprint-1/implement/max.js @@ -1,23 +1,13 @@ -function findMax(num) { - for (let i = 0; i < num.length; i++) { - if (num.length === 0) { - return -Infinity; - } - if (num.length === 1) { - return num[0]; - } - if (Math.sign(num) === -1 && num.length > 1) { - return Math.max(...num); - } - if (num.isInteger === true) { - return Math.max(...num); - } - if (typeof num[i] !== "number") { - num.splice(i, 1); - i--; - } +function findMax(arr) { + if (!Array.isArray(arr) || arr.length === 0) return -Infinity; + + const numbers = arr.filter((n) => typeof n === "number" && !isNaN(n)); + + if (numbers.length > 0) { + return Math.max(...numbers); } - return Math.max(...num); + + return arr[arr.length - 1]; } module.exports = findMax; From 84955122b5f3832f5b62fba59147427e4996e141 Mon Sep 17 00:00:00 2001 From: Fares Bakhet Date: Wed, 26 Nov 2025 17:00:01 +0000 Subject: [PATCH 14/14] Editing sum.test.js file --- Sprint-1/implement/sum.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sprint-1/implement/sum.test.js b/Sprint-1/implement/sum.test.js index 8310875bb..17a48e04c 100644 --- a/Sprint-1/implement/sum.test.js +++ b/Sprint-1/implement/sum.test.js @@ -35,7 +35,7 @@ test("given an array containing negative numbers, returns the correct total sum" // When passed to the sum function // Then it should return the correct total sum test("given an array with decimal numbers, returns the correct total sum", () => { - expect(sum([1.5, 2.5, 3.5])).toBe(7.5); + expect(sum([1.5, 2.5, 3.5])).toBeCloseTo(7.5); }); // Given an array containing non-number values