From 55e404b0f8455f76bcc5cc38b7347801484052e5 Mon Sep 17 00:00:00 2001 From: abduhasen Date: Wed, 18 Mar 2026 00:49:28 +0000 Subject: [PATCH 1/6] complete of sprint 1 of Data-Grouping --- Sprint-1/fix/median.js | 22 +++++++++++++++-- Sprint-1/implement/dedupe.js | 14 ++++++++++- Sprint-1/implement/dedupe.test.js | 29 ++++++++++++++++++++-- Sprint-1/implement/max.js | 16 +++++++++++++ Sprint-1/implement/max.test.js | 35 ++++++++++++++++++++++++--- Sprint-1/implement/sum.js | 15 +++++++++++- Sprint-1/implement/sum.test.js | 40 +++++++++++++++++++++++++++---- Sprint-1/refactor/includes.js | 3 +-- 8 files changed, 158 insertions(+), 16 deletions(-) diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index b22590bc6..cb7dae786 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -6,9 +6,27 @@ // 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((value) => typeof value === "number"); + + if (list.length === 0) { + return null; + } + + list.sort((a, b) => a - b); + const middleIndex = Math.floor(list.length / 2); - const median = list.splice(middleIndex, 1)[0]; - return median; + + if (list.length % 2 === 0) { + return (list[middleIndex - 1] + list[middleIndex]) / 2; + } else { + const middleIndex = Math.floor(list.length / 2); + const median = list.splice(middleIndex, 1)[0]; + return median; + } } module.exports = calculateMedian; diff --git a/Sprint-1/implement/dedupe.js b/Sprint-1/implement/dedupe.js index 781e8718a..c59a9bbd2 100644 --- a/Sprint-1/implement/dedupe.js +++ b/Sprint-1/implement/dedupe.js @@ -1 +1,13 @@ -function dedupe() {} +function dedupe(arr) { + if (arr.length === 0) { + return []; + } + if (arr.length !== new Set(arr).size) { + const cleanedArray = [...new Set(arr)]; + return cleanedArray; + } else { + return arr; + } +} + +module.exports = dedupe; diff --git a/Sprint-1/implement/dedupe.test.js b/Sprint-1/implement/dedupe.test.js index 23e0f8638..adf022582 100644 --- a/Sprint-1/implement/dedupe.test.js +++ b/Sprint-1/implement/dedupe.test.js @@ -16,12 +16,37 @@ 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("The function should return a copy of the original array if the array contains no duplicates", () => { + expect(dedupe([2, 4, 5, 6, 8])).toEqual([2, 4, 5, 6, 8]); + expect(dedupe([3, 7, 5, 8, 14, 19])).toEqual([3, 7, 5, 8, 14, 19]); + expect(dedupe(["apple", "banana", "milk", "egg"])).toEqual([ + "apple", + "banana", + "milk", + "egg", + ]); +}); // 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("The function should return a clean array when a duplicated array is passed", () => { + expect(dedupe([2, 2, 4, 4, 5, 5, 6, 6, 6, 6, 8, 8, 8, 8])).toEqual([ + 2, 4, 5, 6, 8, + ]); + expect(dedupe([1, 1, 1, 2, 2, 3, 3, 4, 4, 6, 6, 7, 7, 8])).toEqual([ + 1, 2, 3, 4, 6, 7, 8, + ]); + expect( + dedupe(["ahmed", "ahmed", "chris", "chris", "tom", "tom", "joy"]) + ).toEqual(["ahmed", "chris", "tom", "joy"]); + expect( + dedupe([2, 2, "apple", "apple", 5, 5, "samsung", "samsung", 8, 8]) + ).toEqual([2, "apple", 5, "samsung", 8]); +}); diff --git a/Sprint-1/implement/max.js b/Sprint-1/implement/max.js index 6dd76378e..13969986b 100644 --- a/Sprint-1/implement/max.js +++ b/Sprint-1/implement/max.js @@ -1,4 +1,20 @@ function findMax(elements) { + if (elements.length === 0) { + return -Infinity; + } + + const number = elements.filter((value) => typeof value === "number"); + + if (number.length === 0) { + return undefined; + } + let maxNumber = number[0]; + for (let i = 1; i < number.length; i++) { + if (number[i] > maxNumber) { + maxNumber = number[i]; + } + } + return maxNumber; } module.exports = findMax; diff --git a/Sprint-1/implement/max.test.js b/Sprint-1/implement/max.test.js index 82f18fd88..76958728a 100644 --- a/Sprint-1/implement/max.test.js +++ b/Sprint-1/implement/max.test.js @@ -16,28 +16,57 @@ 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("the function should return -Infinity when an empty array is passed to the function", () => { + expect(findMax([])).toEqual(-Infinity); +}); // Given an array with one number // When passed to the max function // Then it should return that number - +test("The function should return the original value when an array with one number is passed", () => { + expect(findMax([17])).toBe(17); + expect(findMax([8])).toBe(8); + expect(findMax([4])).toBe(4); +}); // Given an array with both positive and negative numbers // When passed to the max function // Then it should return the largest number overall - +test("The function should return the max value when an Integers number array is passed", () => { + expect(findMax([-3, -1, 0, 1, 3, 4, 6, 7])).toBe(7); + expect(findMax([-17, -8, -5, 2, 3, 0, 1])).toBe(3); + expect(findMax([-27, 18, -16, -9, 1, 7, 9])).toBe(18); +}); // Given an array with just negative numbers // When passed to the max function // Then it should return the closest one to zero +test("The function should return the closest one to zero when an negative number array is passed", () => { + expect(findMax([-3, -1, -4, -6, -7])).toBe(-1); + expect(findMax([-17, -8, -5, -2, -3])).toBe(-2); + expect(findMax([-27, -18, -32, -67, -78, -70])).toBe(-18); +}); // Given an array with decimal numbers // When passed to the max function // Then it should return the largest decimal number +test("The function should return the closest one to zero when an negative number array is passed", () => { + expect(findMax([3.5, 1.9, 5.4, 0.6, 3.7])).toBe(5.4); + expect(findMax([1.7, 0.8, 2.532, 1.092, 0.3])).toBe(2.532); + expect(findMax([0.27, 0.18, 0.32, 0.67, 0.78, 0.7])).toBe(0.78); +}); // 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("The function should return the closest one to zero when an negative number array is passed", () => { + expect(findMax([3.5, "Leeds", "London", 3.7])).toBe(3.7); + expect(findMax(["italy", 0.8, 0.88, 0.89, "base"])).toBe(0.89); + expect(findMax([0.27, "jerry", 0.32, "apple", 0.78, "fly"])).toBe(0.78); +}); // 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("the function should return undefined when non-number value is passed", () => { + expect(findMax(["Manchester", "Leeds", "London"])).toBe(undefined); + expect(findMax(["orange", "apple", "banana"])).toBe(undefined); +}); diff --git a/Sprint-1/implement/sum.js b/Sprint-1/implement/sum.js index 9062aafe3..1907a4d4c 100644 --- a/Sprint-1/implement/sum.js +++ b/Sprint-1/implement/sum.js @@ -1,4 +1,17 @@ function sum(elements) { -} + if (elements.length === 0) { + return 0; + } + const number = elements.filter((value) => typeof value === "number"); + if (number.length === 0) { + return undefined; + } + let sum = 0; + for (let i = 0; i < number.length; i++) { + sum += number[i]; + } + return sum; +} +console.log(sum([])); module.exports = sum; diff --git a/Sprint-1/implement/sum.test.js b/Sprint-1/implement/sum.test.js index dd0a090ca..9f2a027f9 100644 --- a/Sprint-1/implement/sum.test.js +++ b/Sprint-1/implement/sum.test.js @@ -13,24 +13,54 @@ 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("The function should return 0 when an empty array is passed", () => { + expect(sum([])).toEqual(0); +}); // Given an array with just one number // When passed to the sum function // Then it should return that number - +test("The function should return single value when single value array is passed", () => { + expect(sum([7])).toEqual(7); + expect(sum([14])).toEqual(14); + expect(sum([0])).toEqual(0); + expect(sum([100])).toEqual(100); +}); // Given an array containing negative numbers // When passed to the sum function // Then it should still return the correct total sum - +test("The function should return the correct total value when negative number array is passed", () => { + expect(sum([-7, -10, -33])).toEqual(-50); + expect(sum([-6, -10, -18, -4])).toEqual(-38); + expect(sum([-100, -10, -1, -20])).toEqual(-131); + expect(sum([-12, -23, -34, -45, -56, -67])).toEqual(-237); +}); // Given an array with decimal/float numbers // When passed to the sum function // Then it should return the correct total sum - +test("The function should return the correct total value when decimal/float numbers array is passed", () => { + expect(sum([7.8045, 1.273, 3.19])).toEqual(12.2675); + expect(sum([4.6, 1.8, 3.18, 5.4])).toEqual(14.98); + expect(sum([11 / 12, 4 / 6, 1 / 2, 6 / 20])).toEqual(2.383333333333333); + expect(sum([1 / 2, 2 / 3, 3 / 4, 4 / 5, 5 / 6, 6 / 7])).toEqual( + 4.4071428571428575 + ); +}); // 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("The function should return the correct total value when an array containing non-number values is passed", () => { + expect(sum([7.8045, "two", 3.19])).toEqual(10.9945); + expect(sum(["one", 1.8, 3.18, "zero"])).toEqual(4.98); + expect(sum([1 / 12, 4 / 6, "Ahmed", 6 / 8])).toEqual(1.5); + expect(sum(["one", "two", "three", "four", "five", 1 / 2])).toEqual(0.5); +}); // 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("The function should return undefined when an array with only non-number values is passed", () => { + expect(sum(["male", "two", "age"])).toEqual(undefined); + expect(sum(["one", "zero"])).toEqual(undefined); + expect(sum(["world", "apple", "Ahmed", "london"])).toEqual(undefined); + expect(sum(["one", "two", "three", "four", "five"])).toEqual(undefined); +}); diff --git a/Sprint-1/refactor/includes.js b/Sprint-1/refactor/includes.js index 29dad81f0..994131c59 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 (let element of list) { if (element === target) { return true; } From c07db61eb9ff5c4e0339eb37713f4891e9f6c8c7 Mon Sep 17 00:00:00 2001 From: abduhasen Date: Sat, 4 Apr 2026 08:37:35 +0100 Subject: [PATCH 2/6] changed the filter method base on mentor feedback --- Sprint-1/fix/median.js | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index cb7dae786..2eaa988b0 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -10,7 +10,7 @@ function calculateMedian(list) { return null; } - list = list.filter((value) => typeof value === "number"); + list = list.filter((value) => Number.isFinite(value)); if (list.length === 0) { return null; @@ -23,9 +23,7 @@ function calculateMedian(list) { if (list.length % 2 === 0) { return (list[middleIndex - 1] + list[middleIndex]) / 2; } else { - const middleIndex = Math.floor(list.length / 2); - const median = list.splice(middleIndex, 1)[0]; - return median; + return list[middleIndex]; } } From f2129f5f0a904478f457a86bd5a02faf542b3fab Mon Sep 17 00:00:00 2001 From: abduhasen Date: Sat, 4 Apr 2026 15:52:45 +0100 Subject: [PATCH 3/6] fixing the code base on mentor feedback --- Sprint-1/implement/dedupe.js | 2 +- Sprint-1/implement/dedupe.test.js | 11 +++-------- Sprint-1/implement/max.test.js | 12 +++++++----- Sprint-1/implement/sum.js | 2 +- Sprint-1/implement/sum.test.js | 11 ++++++----- 5 files changed, 18 insertions(+), 20 deletions(-) diff --git a/Sprint-1/implement/dedupe.js b/Sprint-1/implement/dedupe.js index c59a9bbd2..42c877079 100644 --- a/Sprint-1/implement/dedupe.js +++ b/Sprint-1/implement/dedupe.js @@ -6,7 +6,7 @@ function dedupe(arr) { const cleanedArray = [...new Set(arr)]; return cleanedArray; } else { - return arr; + return [...arr]; } } diff --git a/Sprint-1/implement/dedupe.test.js b/Sprint-1/implement/dedupe.test.js index adf022582..02e133b0c 100644 --- a/Sprint-1/implement/dedupe.test.js +++ b/Sprint-1/implement/dedupe.test.js @@ -24,14 +24,9 @@ test("given an empty array, it returns an empty array", () => { // When passed to the dedupe function // Then it should return a copy of the original array test("The function should return a copy of the original array if the array contains no duplicates", () => { - expect(dedupe([2, 4, 5, 6, 8])).toEqual([2, 4, 5, 6, 8]); - expect(dedupe([3, 7, 5, 8, 14, 19])).toEqual([3, 7, 5, 8, 14, 19]); - expect(dedupe(["apple", "banana", "milk", "egg"])).toEqual([ - "apple", - "banana", - "milk", - "egg", - ]); + const original = [2, 4, 5, 6, 8]; + expect(dedupe(original)).toEqual(original); + expect(dedupe(original)).not.toBe(original); }); // Given an array with strings or numbers // When passed to the dedupe function diff --git a/Sprint-1/implement/max.test.js b/Sprint-1/implement/max.test.js index 76958728a..ef546eb1d 100644 --- a/Sprint-1/implement/max.test.js +++ b/Sprint-1/implement/max.test.js @@ -58,15 +58,17 @@ test("The function should return the closest one to zero when an negative number // When passed to the max function // Then it should return the max and ignore non-numeric values test("The function should return the closest one to zero when an negative number array is passed", () => { - expect(findMax([3.5, "Leeds", "London", 3.7])).toBe(3.7); - expect(findMax(["italy", 0.8, 0.88, 0.89, "base"])).toBe(0.89); - expect(findMax([0.27, "jerry", 0.32, "apple", 0.78, "fly"])).toBe(0.78); + expect(findMax([3.5, "Leeds", "300", "London", 3.7])).toBe(3.7); + expect(findMax(["1.778", "italy", 0.8, 0.88, 0.89, "base"])).toBe(0.89); + expect(findMax([0.27, "jerry", "140", 0.32, "apple", 0.78, "fly"])).toBe( + 0.78 + ); }); // 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("the function should return undefined when non-number value is passed", () => { - expect(findMax(["Manchester", "Leeds", "London"])).toBe(undefined); - expect(findMax(["orange", "apple", "banana"])).toBe(undefined); + expect(findMax(["Manchester", "4000", "Leeds", "London"])).toBe(undefined); + expect(findMax(["orange", "apple", "5", "banana"])).toBe(undefined); }); diff --git a/Sprint-1/implement/sum.js b/Sprint-1/implement/sum.js index 1907a4d4c..8d7b51d52 100644 --- a/Sprint-1/implement/sum.js +++ b/Sprint-1/implement/sum.js @@ -13,5 +13,5 @@ function sum(elements) { return sum; } -console.log(sum([])); + module.exports = sum; diff --git a/Sprint-1/implement/sum.test.js b/Sprint-1/implement/sum.test.js index 9f2a027f9..c2e9e8ee8 100644 --- a/Sprint-1/implement/sum.test.js +++ b/Sprint-1/implement/sum.test.js @@ -39,11 +39,12 @@ test("The function should return the correct total value when negative number ar // When passed to the sum function // Then it should return the correct total sum test("The function should return the correct total value when decimal/float numbers array is passed", () => { - expect(sum([7.8045, 1.273, 3.19])).toEqual(12.2675); - expect(sum([4.6, 1.8, 3.18, 5.4])).toEqual(14.98); - expect(sum([11 / 12, 4 / 6, 1 / 2, 6 / 20])).toEqual(2.383333333333333); - expect(sum([1 / 2, 2 / 3, 3 / 4, 4 / 5, 5 / 6, 6 / 7])).toEqual( - 4.4071428571428575 + expect(sum([7.8045, 1.273, 3.19])).toBeCloseTo(12.2675, 4); + expect(sum([4.6, 1.8, 3.18, 5.4])).toBeCloseTo(14.98); + expect(sum([11 / 12, 4 / 6, 1 / 2, 6 / 20])).toBeCloseTo(2.383333, 6); + expect(sum([1 / 2, 2 / 3, 3 / 4, 4 / 5, 5 / 6, 6 / 7])).toBeCloseTo( + 4.407142857, + 9 ); }); // Given an array containing non-number values From 51e3a0b1e0636ccb21aee834e8e7044171f3cc18 Mon Sep 17 00:00:00 2001 From: abduhasen Date: Sun, 5 Apr 2026 00:55:48 +0100 Subject: [PATCH 4/6] fixing based on mentor feedback --- Sprint-1/implement/dedupe.js | 6 +++--- Sprint-1/implement/dedupe.test.js | 27 ++++++++++++++++++++++++--- 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/Sprint-1/implement/dedupe.js b/Sprint-1/implement/dedupe.js index 42c877079..6d179de93 100644 --- a/Sprint-1/implement/dedupe.js +++ b/Sprint-1/implement/dedupe.js @@ -2,9 +2,9 @@ function dedupe(arr) { if (arr.length === 0) { return []; } - if (arr.length !== new Set(arr).size) { - const cleanedArray = [...new Set(arr)]; - return cleanedArray; + const uniqueSet = new Set(arr); + if (arr.length !== uniqueSet.size) { + return [...uniqueSet]; } else { return [...arr]; } diff --git a/Sprint-1/implement/dedupe.test.js b/Sprint-1/implement/dedupe.test.js index 02e133b0c..379c31887 100644 --- a/Sprint-1/implement/dedupe.test.js +++ b/Sprint-1/implement/dedupe.test.js @@ -24,9 +24,30 @@ test("given an empty array, it returns an empty array", () => { // When passed to the dedupe function // Then it should return a copy of the original array test("The function should return a copy of the original array if the array contains no duplicates", () => { - const original = [2, 4, 5, 6, 8]; - expect(dedupe(original)).toEqual(original); - expect(dedupe(original)).not.toBe(original); + const original1 = [2, 4, 5, 6, 8]; + const result1 = dedupe(original1); + expect(result1).toEqual(original1); + expect(result1).not.toBe(original1); + + const original2 = [3, 7, 5, 8, 14, 19]; + const result2 = dedupe(original2); + expect(result2).toEqual(original2); + expect(result2).not.toBe(original2); + + const original3 = ["apple", "banana", "milk", "egg"]; + const result3 = dedupe(original3); + expect(result3).toEqual(original3); + expect(result3).not.toBe(original3); + + const original4 = [1, "apple", true, null, 99]; + const result4 = dedupe(original4); + expect(result4).toEqual(original4); + expect(result4).not.toBe(original4); + + const original5 = [100, 200, 300, 400, 500]; + const result5 = dedupe(original5); + expect(result5).toEqual(original5); + expect(result5).not.toBe(original5); }); // Given an array with strings or numbers // When passed to the dedupe function From 760695ce4c04fba373e284c2bd9980335d9bb199 Mon Sep 17 00:00:00 2001 From: abduhasen Date: Sun, 5 Apr 2026 13:32:55 +0100 Subject: [PATCH 5/6] adding restriction on the test --- Sprint-1/implement/dedupe.test.js | 19 +++---------------- 1 file changed, 3 insertions(+), 16 deletions(-) diff --git a/Sprint-1/implement/dedupe.test.js b/Sprint-1/implement/dedupe.test.js index 379c31887..4d6a2953a 100644 --- a/Sprint-1/implement/dedupe.test.js +++ b/Sprint-1/implement/dedupe.test.js @@ -25,29 +25,16 @@ test("given an empty array, it returns an empty array", () => { // Then it should return a copy of the original array test("The function should return a copy of the original array if the array contains no duplicates", () => { const original1 = [2, 4, 5, 6, 8]; + expect(original1.length).toBe(new Set(original1).size); const result1 = dedupe(original1); expect(result1).toEqual(original1); expect(result1).not.toBe(original1); - const original2 = [3, 7, 5, 8, 14, 19]; + const original2 = [3, 9, 25, 1, 12, 42]; + expect(original2.length).toBe(new Set(original2).size); const result2 = dedupe(original2); expect(result2).toEqual(original2); expect(result2).not.toBe(original2); - - const original3 = ["apple", "banana", "milk", "egg"]; - const result3 = dedupe(original3); - expect(result3).toEqual(original3); - expect(result3).not.toBe(original3); - - const original4 = [1, "apple", true, null, 99]; - const result4 = dedupe(original4); - expect(result4).toEqual(original4); - expect(result4).not.toBe(original4); - - const original5 = [100, 200, 300, 400, 500]; - const result5 = dedupe(original5); - expect(result5).toEqual(original5); - expect(result5).not.toBe(original5); }); // Given an array with strings or numbers // When passed to the dedupe function From 4095a199a63d178b50bfb53e742a6143e65605fc Mon Sep 17 00:00:00 2001 From: abduhasen Date: Mon, 6 Apr 2026 01:57:56 +0100 Subject: [PATCH 6/6] fixing test case --- Sprint-1/implement/dedupe.test.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Sprint-1/implement/dedupe.test.js b/Sprint-1/implement/dedupe.test.js index 4d6a2953a..e4298ec1b 100644 --- a/Sprint-1/implement/dedupe.test.js +++ b/Sprint-1/implement/dedupe.test.js @@ -27,13 +27,13 @@ test("The function should return a copy of the original array if the array conta const original1 = [2, 4, 5, 6, 8]; expect(original1.length).toBe(new Set(original1).size); const result1 = dedupe(original1); - expect(result1).toEqual(original1); + expect(result1).toEqual("[2, 4, 5, 6, 8]"); expect(result1).not.toBe(original1); const original2 = [3, 9, 25, 1, 12, 42]; expect(original2.length).toBe(new Set(original2).size); const result2 = dedupe(original2); - expect(result2).toEqual(original2); + expect(result2).toEqual("[3, 9, 25, 1, 12, 42]"); expect(result2).not.toBe(original2); }); // Given an array with strings or numbers