From 74647da2749d685a3c699325913be26ddc3ad9cb Mon Sep 17 00:00:00 2001 From: abduhasen Date: Sat, 18 Jul 2026 13:57:43 +0100 Subject: [PATCH 1/3] fix codes,write test cases and refactor codes --- Sprint-1/fix/median.js | 18 ++++++++-- Sprint-1/implement/dedupe.js | 14 +++++++- Sprint-1/implement/dedupe.test.js | 48 ++++++++++++++++++++++++-- Sprint-1/implement/max.js | 16 +++++++++ Sprint-1/implement/max.test.js | 57 +++++++++++++++++++++++++++---- Sprint-1/implement/sum.js | 15 ++++++++ Sprint-1/implement/sum.test.js | 44 +++++++++++++++++++++--- Sprint-1/refactor/includes.js | 3 +- 8 files changed, 198 insertions(+), 17 deletions(-) diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index b22590bc6..482ef8fc2 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) { + if (!Array.isArray(list)) { + return null; + } + list = list.filter((item) => typeof item === "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; + const sumOfIndex = list[middleIndex] + list[middleIndex - 1]; + const middleValue = sumOfIndex / 2; + if (list.length % 2 > 0) { + return list[middleIndex]; + } else { + return middleValue; + } } module.exports = calculateMedian; diff --git a/Sprint-1/implement/dedupe.js b/Sprint-1/implement/dedupe.js index 781e8718a..6d179de93 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 []; + } + const uniqueSet = new Set(arr); + if (arr.length !== uniqueSet.size) { + return [...uniqueSet]; + } else { + return [...arr]; + } +} + +module.exports = dedupe; diff --git a/Sprint-1/implement/dedupe.test.js b/Sprint-1/implement/dedupe.test.js index d7c8e3d8e..29354af43 100644 --- a/Sprint-1/implement/dedupe.test.js +++ b/Sprint-1/implement/dedupe.test.js @@ -16,13 +16,57 @@ 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("given an empty array, it returns an empty array", () => { + const arr = []; + const emptyArray = dedupe(arr); + expect(emptyArray).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 the cope of original array when no duplicates of array pass to it", () => { + const arr = [2, 3, 4, 5]; + const result = dedupe(arr); + expect(result).toEqual(arr); + expect(result).not.toBe(arr); +}); +test("the function should return the cope of original array when no duplicates of array pass to it ", () => { + const arr = [13, 9, 8, 7]; + + const result = dedupe(arr); + expect(result).toEqual(arr); + expect(result).not.toBe(arr); +}); // 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. +test("the function should return the first occurrence of each element when duplicated arrays of element pass to it", () => { + const arr = [3, 3, 3, 7, 8, 9, 9, 6, 6]; + + const newResult = dedupe(arr); + expect(newResult).toEqual([3, 7, 8, 9, 6]); +}); +test("the function should return the first occurrence of each element when duplicated arrays of element pass to it", () => { + const arr = [3, 3, "hi", 8, 8, "hi", "you", 8, "you", "hi"]; + + const newResult = dedupe(arr); + expect(newResult).toEqual([3, "hi", 8, "you"]); +}); +test("the function should return the first occurrence of each element when duplicated arrays of element pass to it", () => { + const arr = [ + "orange", + "egg", + "egg", + "banana", + "orange", + "apple", + "banana", + "apple", + ]; + + const newResult = dedupe(arr); + expect(newResult).toEqual(["orange", "egg", "banana", "apple"]); +}); diff --git a/Sprint-1/implement/max.js b/Sprint-1/implement/max.js index 6dd76378e..ccd036cfe 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; + } else if (elements.length === 1) { + return elements[0]; + } + const number = elements.filter((value) => typeof value === "number"); + if (number.length === 0) { + return undefined; + } + let max = elements[0]; + for (let i = 0; i < elements.length; i++) { + if (max < elements[i]) { + max = elements[i]; + } + } + return max; } module.exports = findMax; diff --git a/Sprint-1/implement/max.test.js b/Sprint-1/implement/max.test.js index 82f18fd88..d6f406b7e 100644 --- a/Sprint-1/implement/max.test.js +++ b/Sprint-1/implement/max.test.js @@ -16,28 +16,73 @@ 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("when given an empty array to the function it returns Infinity", () => { + const elements = []; + + const result = findMax(elements); + expect(result).toEqual(Infinity); +}); // Given an array with one number // When passed to the max function // Then it should return that number - +test("when the given is array of one number it should return that number", () => { + const elements = [30]; + const result = findMax(elements); + expect(result).toEqual(30); +}); // Given an array with both positive and negative numbers // When passed to the max function // Then it should return the largest number overall - +test("should return the largest number when the array contains both negative and positive numbers", () => { + const elements = [-10, -12, -7, 0, 2, 4]; + const result = findMax(elements); + expect(result).toEqual(4); +}); +test("should return the largest number when the array contains both negative and positive numbers", () => { + const elements = [-20, -6, -2, 0, 7, 9, 5]; + const result = findMax(elements); + expect(result).toEqual(9); +}); // Given an array with just negative numbers // When passed to the max function // Then it should return the closest one to zero - +test("should return number that is closest to zero when the array contains all negative numbers", () => { + const elements = [-20, -6, -2, -4, -7, -9, -5]; + const result = findMax(elements); + expect(result).toEqual(-2); +}); +test("should return number that is closest to zero when the array contains all negative numbers", () => { + const elements = [-20, -11, -1, 0, -7, -9, -5]; + const result = findMax(elements); + expect(result).toEqual(0); +}); // Given an array with decimal numbers // When passed to the max function // Then it should return the largest decimal number - +test("should return the largest decimal number when the array contains decimal numbers", () => { + const elements = [2.7, 6.3, 7.3, 4.9, 7.9, 4.5]; + const result = findMax(elements); + expect(result).toEqual(7.9); +}); +test("should return the largest decimal number when the array contains decimal numbers", () => { + const elements = [11.8, 16.3, 27.3, 41.9, 0.9, 14.5]; + const result = findMax(elements); + expect(result).toEqual(41.9); +}); // 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("should return the largest number ignoring non-numerical values when the array contains non-numerical value", () => { + const elements = [11, "apple", 27, "hi", 9, "zero"]; + const result = findMax(elements); + expect(result).toEqual(27); +}); // 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("should return only number value, when the array contains non-numerical value", () => { + const elements = ["apple", "hi", "book", "zero"]; + const result = findMax(elements); + expect(result).toEqual(undefined); +}); diff --git a/Sprint-1/implement/sum.js b/Sprint-1/implement/sum.js index 9062aafe3..e7f6cdbd9 100644 --- a/Sprint-1/implement/sum.js +++ b/Sprint-1/implement/sum.js @@ -1,4 +1,19 @@ function sum(elements) { + if (elements.length === 0) { + return 0; + } + const number = elements.filter((value) => typeof value === "number"); + if (number.length === 0) { + return undefined; + } + if (elements.length === 1) { + return elements[0]; + } + let arraySum = 0; + for (let i = 0; i < number.length; i++) { + arraySum += number[i]; + } + return arraySum; } module.exports = sum; diff --git a/Sprint-1/implement/sum.test.js b/Sprint-1/implement/sum.test.js index dd0a090ca..ae3fbe97a 100644 --- a/Sprint-1/implement/sum.test.js +++ b/Sprint-1/implement/sum.test.js @@ -13,24 +13,60 @@ 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("when empty array is given it should returns 0", () => { + const elements = []; + const result = sum(elements); + expect(result).toEqual(0); +}); // Given an array with just one number // When passed to the sum function // Then it should return that number - +test("when array with just one number given it should returns that number", () => { + const elements = [13]; + const result = sum(elements); + expect(result).toEqual(13); +}); +test("when array with just one number given it should returns that number", () => { + const elements = [9]; + const result = sum(elements); + expect(result).toEqual(9); +}); // Given an array containing negative numbers // When passed to the sum function // Then it should still return the correct total sum - +test("when array with negative numbers given it should return the correct total sum", () => { + const elements = [-9, -2, -4, -7]; + const result = sum(elements); + expect(result).toEqual(-22); +}); +test("when array with negative numbers given it should return the correct total sum", () => { + const elements = [-11, -9, -2, -5]; + const result = sum(elements); + expect(result).toEqual(-27); +}); // Given an array with decimal/float numbers // When passed to the sum function // Then it should return the correct total sum - +test("when array that contain decimal or float number pass to the function it should return the correct total sum", () => { + const elements = [0.9, 1 / 2, 4.3, 3.7, 1 / 4]; + const result = sum(elements); + expect(result).toEqual(9.649999999999999); +}); // 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("when array that contain number and non-number pass to the function it should return the sum of the numerical elements", () => { + const elements = [9, 1, 4, "one", 3, "two", 4]; + const result = sum(elements); + expect(result).toEqual(21); +}); // 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("when array that contain non-number pass to the function it should return undefined", () => { + const elements = ["window", "one", "name", "two"]; + const result = sum(elements); + expect(result).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 7a74af8bb32fbb811ca0c727c2a5818ec5e278b8 Mon Sep 17 00:00:00 2001 From: abduhasen Date: Wed, 5 Aug 2026 12:01:20 +0100 Subject: [PATCH 2/3] fix codes based on mentor feedback --- Sprint-1/fix/median.js | 17 +++++++++-------- Sprint-1/implement/dedupe.js | 8 ++------ Sprint-1/implement/max.js | 10 +++++----- Sprint-1/implement/max.test.js | 5 +++++ 4 files changed, 21 insertions(+), 19 deletions(-) diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index 482ef8fc2..e538d2ea3 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -9,18 +9,19 @@ function calculateMedian(list) { if (!Array.isArray(list)) { return null; } - list = list.filter((item) => typeof item === "number"); - if (list.length === 0) { + filteredList = list.filter((item) => typeof item === "number"); + if (filteredList.length === 0) { return null; } - list.sort((a, b) => a - b); - const middleIndex = Math.floor(list.length / 2); - const sumOfIndex = list[middleIndex] + list[middleIndex - 1]; - const middleValue = sumOfIndex / 2; - if (list.length % 2 > 0) { - return list[middleIndex]; + filteredList.sort((a, b) => a - b); + const middleIndex = Math.floor(filteredList.length / 2); + if (filteredList.length % 2 > 0) { + return filteredList[middleIndex]; } else { + const sumOfIndex = + filteredList[middleIndex] + filteredList[middleIndex - 1]; + const middleValue = sumOfIndex / 2; return middleValue; } } diff --git a/Sprint-1/implement/dedupe.js b/Sprint-1/implement/dedupe.js index 6d179de93..d14fa86ff 100644 --- a/Sprint-1/implement/dedupe.js +++ b/Sprint-1/implement/dedupe.js @@ -3,11 +3,7 @@ function dedupe(arr) { return []; } const uniqueSet = new Set(arr); - if (arr.length !== uniqueSet.size) { - return [...uniqueSet]; - } else { - return [...arr]; - } + return [...uniqueSet]; } - +console.log(dedupe([3, 3, 3, 7, 8, 9, 9, 6, 6])); module.exports = dedupe; diff --git a/Sprint-1/implement/max.js b/Sprint-1/implement/max.js index ccd036cfe..94178118f 100644 --- a/Sprint-1/implement/max.js +++ b/Sprint-1/implement/max.js @@ -1,17 +1,17 @@ function findMax(elements) { if (elements.length === 0) { return Infinity; - } else if (elements.length === 1) { + } else if (elements.length === 1 && !Number.isNaN(Number(elements[0]))) { return elements[0]; } - const number = elements.filter((value) => typeof value === "number"); + const number = elements.filter((value) => !Number.isNaN(Number(value))); if (number.length === 0) { return undefined; } - let max = elements[0]; + let max = number[0]; for (let i = 0; i < elements.length; i++) { - if (max < elements[i]) { - max = elements[i]; + if (max < number[i]) { + max = number[i]; } } return max; diff --git a/Sprint-1/implement/max.test.js b/Sprint-1/implement/max.test.js index d6f406b7e..c4e0e18f8 100644 --- a/Sprint-1/implement/max.test.js +++ b/Sprint-1/implement/max.test.js @@ -86,3 +86,8 @@ test("should return only number value, when the array contains non-numerical val const result = findMax(elements); expect(result).toEqual(undefined); }); +test("should return only number value, when the array contains non-numerical value", () => { + const elements = ["apple"]; + const result = findMax(elements); + expect(result).toEqual(undefined); +}); From d2f021135d182be1c6aa27c5244ea5b485142d79 Mon Sep 17 00:00:00 2001 From: abduhasen Date: Mon, 10 Aug 2026 12:27:37 +0100 Subject: [PATCH 3/3] fixed a code based on mentor feedback ( wrote additional filter using && ) --- Sprint-1/fix/median.js | 4 +++- Sprint-1/implement/sum.js | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index e538d2ea3..e0785a675 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -9,7 +9,9 @@ function calculateMedian(list) { if (!Array.isArray(list)) { return null; } - filteredList = list.filter((item) => typeof item === "number"); + const filteredList = list.filter( + (item) => typeof item === "number" && !Number.isNaN(item) + ); if (filteredList.length === 0) { return null; } diff --git a/Sprint-1/implement/sum.js b/Sprint-1/implement/sum.js index e7f6cdbd9..a1a7a759e 100644 --- a/Sprint-1/implement/sum.js +++ b/Sprint-1/implement/sum.js @@ -2,7 +2,9 @@ function sum(elements) { if (elements.length === 0) { return 0; } - const number = elements.filter((value) => typeof value === "number"); + const number = elements.filter( + (value) => typeof value === "number" && !Number.isNaN(value) + ); if (number.length === 0) { return undefined; }