From f4c5eb05adcd22665a72b2ab15c261e1a87cb572 Mon Sep 17 00:00:00 2001 From: B3-Bisrat Date: Fri, 17 Jul 2026 13:05:56 +0100 Subject: [PATCH 1/3] Sprint-1 fix is done. --- Sprint-1/fix/median.js | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index b22590bc6..287e0e572 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -6,9 +6,25 @@ // 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; + } // If list is NOT an array, return null. + + const numbers = list.filter((item) => typeof item === "number"); + // Keep the item only if its type is number. + if (numbers.length === 0) { + return null; + } + + numbers.sort((a, b) => a - b); // This sorts numbers from smallest to largest + + const middleIndex = Math.floor(numbers.length / 2); + + if (numbers.length % 2 === 0) { + return (numbers[middleIndex - 1] + numbers[middleIndex]) / 2; + } + + return numbers[middleIndex]; } module.exports = calculateMedian; From 68bed284965bfa3af301e48b41da1877384ed418 Mon Sep 17 00:00:00 2001 From: B3-Bisrat Date: Fri, 17 Jul 2026 15:08:15 +0100 Subject: [PATCH 2/3] Implement is done. --- Sprint-1/implement/dedupe.js | 14 ++++++++++++- Sprint-1/implement/dedupe.test.js | 35 +++++++++++++++++++++++++++++-- Sprint-1/implement/max.js | 15 +++++++++++++ Sprint-1/implement/max.test.js | 28 ++++++++++++++++++++++++- Sprint-1/implement/sum.js | 8 +++++++ Sprint-1/implement/sum.test.js | 24 ++++++++++++++++++++- 6 files changed, 119 insertions(+), 5 deletions(-) diff --git a/Sprint-1/implement/dedupe.js b/Sprint-1/implement/dedupe.js index 781e8718a..8e095e255 100644 --- a/Sprint-1/implement/dedupe.js +++ b/Sprint-1/implement/dedupe.js @@ -1 +1,13 @@ -function dedupe() {} +function dedupe(array) { + const newArray = []; + + for (let i = 0; i < array.length; i++) { + if (!newArray.includes(array[i])) { + newArray.push(array[i]); + } + } + + return newArray; +} + +module.exports = dedupe; diff --git a/Sprint-1/implement/dedupe.test.js b/Sprint-1/implement/dedupe.test.js index d7c8e3d8e..ff9173a8c 100644 --- a/Sprint-1/implement/dedupe.test.js +++ b/Sprint-1/implement/dedupe.test.js @@ -16,13 +16,44 @@ 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"); + +// Empty array +test("should return an empty array when given an empty array", () => { + const array = []; + const result = dedupe(array); + + expect(result).toEqual([]); +}); // Given an array with no duplicates // When passed to the dedupe function // Then it should return a copy of the original array +// No duplicates +test("should return a copy when the array has no duplicates", () => { + const array = [1, 2, 3]; + const result = dedupe(array); + + expect(result).toEqual([1, 2, 3]); +}); + // 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. + +// Remove duplicate strings +test("should remove duplicate strings and keep first occurrence", () => { + const array = ["a", "a", "a", "b", "b", "c"]; + const result = dedupe(array); + + expect(result).toEqual(["a", "b", "c"]); +}); + +// Remove duplicate numbers +test("should remove duplicate numbers and keep first occurrence", () => { + const array = [5, 1, 1, 2, 3, 2, 5, 8]; + const result = dedupe(array); + + expect(result).toEqual([5, 1, 2, 3, 8]); +}); diff --git a/Sprint-1/implement/max.js b/Sprint-1/implement/max.js index 6dd76378e..a5d8f9275 100644 --- a/Sprint-1/implement/max.js +++ b/Sprint-1/implement/max.js @@ -1,4 +1,19 @@ function findMax(elements) { + let max = -Infinity; // Start with the smallest possible value. + + // loop + for (let i = 0; i < elements.length; i++) { + if (typeof elements[i] === "number") { + // Check if the current element is a number and ignores strings, objects, null + + if (elements[i] > max) { + // If it is bigger, update max with this new bigger number + max = elements[i]; + } + } + } + return max; } +// Export the function so it can be used in the test file module.exports = findMax; diff --git a/Sprint-1/implement/max.test.js b/Sprint-1/implement/max.test.js index 82f18fd88..bacca371a 100644 --- a/Sprint-1/implement/max.test.js +++ b/Sprint-1/implement/max.test.js @@ -16,28 +16,54 @@ 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([])).toEqual(-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([5])).toEqual(5); +}); + // Given an array with both positive and negative numbers // When passed to the max function // Then it should return the largest number overall +test("returns the largest number from positive and negative numbers", () => { + expect(findMax([-5, 10, -2, 7])).toEqual(10); +}); + // Given an array with just negative numbers // When passed to the max function // Then it should return the closest one to zero +test("returns the closest number to zero when all numbers are negative", () => { + expect(findMax([-10, -3, -20])).toEqual(-3); +}); + // Given an array with decimal numbers // When passed to the max function // Then it should return the largest decimal number +test("returns the largest decimal number", () => { + expect(findMax([1.2, 5.8, 3.4])).toEqual(5.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("ignores non-number values", () => { + expect(findMax(["hey", 10, "hi", 60, 10])).toEqual(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("returns -Infinity when there are no numbers", () => { + expect(findMax(["banana", "orange"])).toEqual(-Infinity); +}); diff --git a/Sprint-1/implement/sum.js b/Sprint-1/implement/sum.js index 9062aafe3..0cc2ab8bf 100644 --- a/Sprint-1/implement/sum.js +++ b/Sprint-1/implement/sum.js @@ -1,4 +1,12 @@ function sum(elements) { + let total = 0; + for (let i = 0; i < elements.length; i++) { + // Only add numbers and ignore other data types + if (typeof elements[i] === "number") { + total = total + elements[i]; + } + } + return total; } module.exports = sum; diff --git a/Sprint-1/implement/sum.test.js b/Sprint-1/implement/sum.test.js index dd0a090ca..cf5c96c49 100644 --- a/Sprint-1/implement/sum.test.js +++ b/Sprint-1/implement/sum.test.js @@ -13,24 +13,46 @@ 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([])).toEqual(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([5])).toEqual(5); +}); + // Given an array containing negative numbers // When passed to the sum function // Then it should still return the correct total sum +test("returns the correct total with negative numbers", () => { + expect(sum([-5, 10, -2])).toEqual(3); +}); + // Given an array with decimal/float numbers // When passed to the sum function // Then it should return the correct total sum +test("returns the correct total with decimal numbers", () => { + expect(sum([1.5, 2.5])).toEqual(4); +}); + // 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("ignores non-number values and sums numbers only", () => { + expect(sum(["hey", 10, "hi", 60, 10])).toEqual(80); +}); + // 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("returns 0 when there are no numbers", () => { + expect(sum(["hello", "world"])).toEqual(0); +}); From 2cbf34931f23b6676c6efc14dd23a58cf2f0ad16 Mon Sep 17 00:00:00 2001 From: B3-Bisrat Date: Fri, 17 Jul 2026 15:34:49 +0100 Subject: [PATCH 3/3] Refactor is done. --- Sprint-1/refactor/includes.js | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/Sprint-1/refactor/includes.js b/Sprint-1/refactor/includes.js index 29dad81f0..1d4b4c051 100644 --- a/Sprint-1/refactor/includes.js +++ b/Sprint-1/refactor/includes.js @@ -1,12 +1,22 @@ // Refactor the implementation of includes to use a for...of loop -function includes(list, target) { +/*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 (const element of list) { + if (element === target) { + return true; + } + } + return false; }