From 0c5a66807fde43e64394279149a8ffae0148b1fc Mon Sep 17 00:00:00 2001 From: jamesishimwe Date: Tue, 7 Apr 2026 17:19:56 +0200 Subject: [PATCH 01/16] Add test for data structure not an array return null --- Sprint-1/fix/median.js | 1 + 1 file changed, 1 insertion(+) diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index b22590bc6..e5329e1f2 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -6,6 +6,7 @@ // or 'list' has mixed values (the function is expected to sort only numbers). function calculateMedian(list) { + if (!Array.isArray(list)) return null; const middleIndex = Math.floor(list.length / 2); const median = list.splice(middleIndex, 1)[0]; return median; From c2bcbac86d48664c14ffa27d8e499142be6ed915 Mon Sep 17 00:00:00 2001 From: jamesishimwe Date: Tue, 7 Apr 2026 17:21:03 +0200 Subject: [PATCH 02/16] Add test for empty number list that returns null --- Sprint-1/fix/median.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index e5329e1f2..af8293c74 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -7,6 +7,9 @@ function calculateMedian(list) { if (!Array.isArray(list)) return null; + + const numbers = list.filter(val => typeof val === 'number'); + if (numbers.length === 0) return null; const middleIndex = Math.floor(list.length / 2); const median = list.splice(middleIndex, 1)[0]; return median; From 8c52305a8431df9a6e51e91004236e5f3e155a0b Mon Sep 17 00:00:00 2001 From: jamesishimwe Date: Tue, 7 Apr 2026 17:21:54 +0200 Subject: [PATCH 03/16] Sort the numbers in the list --- Sprint-1/fix/median.js | 1 + 1 file changed, 1 insertion(+) diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index af8293c74..5b48a560a 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -10,6 +10,7 @@ function calculateMedian(list) { const numbers = list.filter(val => typeof val === 'number'); if (numbers.length === 0) return null; + numbers.sort((a, b) => a - b); const middleIndex = Math.floor(list.length / 2); const median = list.splice(middleIndex, 1)[0]; return median; From b7a626a66fab40b83e25a7348939ffd949406777 Mon Sep 17 00:00:00 2001 From: jamesishimwe Date: Tue, 7 Apr 2026 17:23:00 +0200 Subject: [PATCH 04/16] Add test for even size of array --- Sprint-1/fix/median.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index 5b48a560a..59125d18e 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -11,6 +11,12 @@ function calculateMedian(list) { const numbers = list.filter(val => typeof val === 'number'); if (numbers.length === 0) return null; numbers.sort((a, b) => a - b); + + if (numbers.length % 2 === 0) { + const mid1 = numbers[numbers.length / 2 - 1]; + const mid2 = numbers[numbers.length / 2]; + return (mid1 + mid2) / 2; + } const middleIndex = Math.floor(list.length / 2); const median = list.splice(middleIndex, 1)[0]; return median; From cea6f8dc8dca3b71d39c1676f14a57b91081cdd2 Mon Sep 17 00:00:00 2001 From: jamesishimwe Date: Tue, 7 Apr 2026 17:25:45 +0200 Subject: [PATCH 05/16] Use the new list numbers of sorted numbers --- 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 59125d18e..a2a10dd9d 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -17,8 +17,8 @@ function calculateMedian(list) { const mid2 = numbers[numbers.length / 2]; return (mid1 + mid2) / 2; } - const middleIndex = Math.floor(list.length / 2); - const median = list.splice(middleIndex, 1)[0]; + const middleIndex = Math.floor(numbers.length / 2); + const median = numbers[middleIndex]; return median; } From 8d00bca07a9a5562cc65afa69eeaf986336bca8c Mon Sep 17 00:00:00 2001 From: jamesishimwe Date: Tue, 7 Apr 2026 19:06:07 +0200 Subject: [PATCH 06/16] Initialise withoutduplicate array --- 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 781e8718a..22002dafb 100644 --- a/Sprint-1/implement/dedupe.js +++ b/Sprint-1/implement/dedupe.js @@ -1 +1,4 @@ -function dedupe() {} +function dedupe(list) { + let listWithoutDuplicates = []; + +} From c9001bc7fd461652049d70c9b6f33809d5cec6b4 Mon Sep 17 00:00:00 2001 From: jamesishimwe Date: Tue, 7 Apr 2026 19:07:09 +0200 Subject: [PATCH 07/16] Implement a for loop for adding to the array unique elements --- 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 22002dafb..6e5f81a2e 100644 --- a/Sprint-1/implement/dedupe.js +++ b/Sprint-1/implement/dedupe.js @@ -1,4 +1,9 @@ function dedupe(list) { let listWithoutDuplicates = []; - + for (let i = 0; i < list.length; i++) { + if(listWithoutDuplicates.includes(list[i]) === false) { + listWithoutDuplicates.push(list[i]); + } + } + return listWithoutDuplicates; } From 4133ed9a237b43ef0297cbbf791c0b655fa9aa96 Mon Sep 17 00:00:00 2001 From: jamesishimwe Date: Tue, 7 Apr 2026 19:09:50 +0200 Subject: [PATCH 08/16] Add test for empty array --- Sprint-1/implement/dedupe.test.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/Sprint-1/implement/dedupe.test.js b/Sprint-1/implement/dedupe.test.js index 23e0f8638..4336757db 100644 --- a/Sprint-1/implement/dedupe.test.js +++ b/Sprint-1/implement/dedupe.test.js @@ -25,3 +25,11 @@ test.todo("given an empty array, it returns an empty array"); // 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 + +describe("dedupe", () => { + it("given an empty array, it returns an empty array", () => { + expect(dedupe([])).toEqual([]); + }); + + +}); \ No newline at end of file From 06ef0692ee8f879a6521bbe143f48924dd77e8f5 Mon Sep 17 00:00:00 2001 From: jamesishimwe Date: Tue, 7 Apr 2026 19:10:23 +0200 Subject: [PATCH 09/16] Add test for non-duplicates array --- Sprint-1/implement/dedupe.test.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Sprint-1/implement/dedupe.test.js b/Sprint-1/implement/dedupe.test.js index 4336757db..841f9328d 100644 --- a/Sprint-1/implement/dedupe.test.js +++ b/Sprint-1/implement/dedupe.test.js @@ -31,5 +31,9 @@ describe("dedupe", () => { expect(dedupe([])).toEqual([]); }); - + it("given an array with no duplicates, it returns a copy of the original array", () => { + expect(dedupe([1, 2, 3])).toEqual([1, 2, 3]); + }); + + }); \ No newline at end of file From 6bc8f58744cc58d1cb373f305ee4601b3f9cda98 Mon Sep 17 00:00:00 2001 From: jamesishimwe Date: Tue, 7 Apr 2026 19:10:39 +0200 Subject: [PATCH 10/16] Add test for duplicate arrays --- Sprint-1/implement/dedupe.test.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Sprint-1/implement/dedupe.test.js b/Sprint-1/implement/dedupe.test.js index 841f9328d..c1cb9512d 100644 --- a/Sprint-1/implement/dedupe.test.js +++ b/Sprint-1/implement/dedupe.test.js @@ -35,5 +35,9 @@ describe("dedupe", () => { expect(dedupe([1, 2, 3])).toEqual([1, 2, 3]); }); - + it("given an array with strings or numbers, it removes the duplicate values, preserving the first occurrence of each element", () => { + 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]); + }); }); \ No newline at end of file From 07a13743e8f83ced6fc565a29310869d504a3e2a Mon Sep 17 00:00:00 2001 From: jamesishimwe Date: Tue, 7 Apr 2026 19:18:56 +0200 Subject: [PATCH 11/16] Export the function to be reused in the tests --- Sprint-1/implement/dedupe.js | 1 + 1 file changed, 1 insertion(+) diff --git a/Sprint-1/implement/dedupe.js b/Sprint-1/implement/dedupe.js index 6e5f81a2e..d43d1c419 100644 --- a/Sprint-1/implement/dedupe.js +++ b/Sprint-1/implement/dedupe.js @@ -7,3 +7,4 @@ function dedupe(list) { } return listWithoutDuplicates; } +module.exports = dedupe; \ No newline at end of file From 0f551a381c114377e3800cc703466b9fb601838b Mon Sep 17 00:00:00 2001 From: jamesishimwe Date: Tue, 7 Apr 2026 19:28:22 +0200 Subject: [PATCH 12/16] Implement findMax --- Sprint-1/implement/max.js | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/Sprint-1/implement/max.js b/Sprint-1/implement/max.js index 6dd76378e..d4e02ce5f 100644 --- a/Sprint-1/implement/max.js +++ b/Sprint-1/implement/max.js @@ -1,4 +1,11 @@ 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; +module.exports = findMax; \ No newline at end of file From 5b170db6211267adba4d766b6b4c33d1e2d58807 Mon Sep 17 00:00:00 2001 From: jamesishimwe Date: Tue, 7 Apr 2026 19:43:03 +0200 Subject: [PATCH 13/16] Add tests for max --- 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 82f18fd88..49ba64599 100644 --- a/Sprint-1/implement/max.test.js +++ b/Sprint-1/implement/max.test.js @@ -41,3 +41,28 @@ 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("max", () => { + test("given an empty array, returns -Infinity", () => { + expect(findMax([])).toBe(-Infinity); + }); + + test("given an array with one number, returns that number", () => { + expect(findMax([42])).toBe(42); + }); + test("given an array with both positive and negative numbers, returns the largest number overall", () => { + expect(findMax([-10, 0, 5, -20, 15])).toBe(15); + }); + test("given an array with just negative numbers, returns the closest one to zero", () => { + expect(findMax([-50, -20, -5, -1])).toBe(-1); + }); + test("given an array with decimal numbers, returns the largest decimal number", () => { + expect(findMax([1.5, 2.3, 0.7, 3.8])).toBe(3.8); + }); + 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); + }); + test("given an array with only non-number values, returns the least surprising value given how it behaves for all other inputs", () => { + expect(findMax(['hey', 'hi', 'hello'])).toBe(-Infinity); + }); +}); \ No newline at end of file From 796aa7ae67de5c1b14cf64feb718ad8fde8aa747 Mon Sep 17 00:00:00 2001 From: jamesishimwe Date: Tue, 7 Apr 2026 19:44:34 +0200 Subject: [PATCH 14/16] Implement sum function --- Sprint-1/implement/sum.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/Sprint-1/implement/sum.js b/Sprint-1/implement/sum.js index 9062aafe3..13d33c496 100644 --- a/Sprint-1/implement/sum.js +++ b/Sprint-1/implement/sum.js @@ -1,4 +1,11 @@ function sum(elements) { + let total = 0; + for (let element of elements) { + if (typeof element === 'number') { + total += element; + } + } + return total; } module.exports = sum; From e3f3e0c0c81193961bc8807e84461389b97c29bc Mon Sep 17 00:00:00 2001 From: jamesishimwe Date: Tue, 7 Apr 2026 19:45:37 +0200 Subject: [PATCH 15/16] Implement tests for sum --- Sprint-1/implement/sum.test.js | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/Sprint-1/implement/sum.test.js b/Sprint-1/implement/sum.test.js index dd0a090ca..62dea20e9 100644 --- a/Sprint-1/implement/sum.test.js +++ b/Sprint-1/implement/sum.test.js @@ -34,3 +34,28 @@ 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", () => { + test("given an empty array, returns 0", () => { + expect(sum([])).toBe(0); + }); + + test("given an array with just one number, returns that number", () => { + expect(sum([5])).toBe(5); + }); + + test("given an array containing negative numbers, returns the correct total sum", () => { + expect(sum([-1, -2, -3])).toBe(-6); + }); + + test("given an array with decimal/float numbers, returns the correct total sum", () => { + expect(sum([1.5, 2.5, 3.0])).toBe(7.0); + }); + + test("given an array containing non-number values, ignores them and returns the sum of the numerical elements", () => { + expect(sum(['hey', 10, 'hi', 60, 10])).toBe(80); + }); + + test("given an array with only non-number values, returns 0", () => { + expect(sum(['hey', 'hi', 'hello'])).toBe(0); + }); +}); \ No newline at end of file From 629a3d2b5e0a4f2b5a2700a65389ace88a6becab Mon Sep 17 00:00:00 2001 From: jamesishimwe Date: Tue, 7 Apr 2026 20:02:29 +0200 Subject: [PATCH 16/16] Refactor the includes function to use the for ...of ... loop --- Sprint-1/refactor/includes.js | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/Sprint-1/refactor/includes.js b/Sprint-1/refactor/includes.js index 29dad81f0..4a23cb6d2 100644 --- a/Sprint-1/refactor/includes.js +++ b/Sprint-1/refactor/includes.js @@ -1,8 +1,5 @@ -// 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; } @@ -10,4 +7,4 @@ function includes(list, target) { return false; } -module.exports = includes; +module.exports = includes; \ No newline at end of file