From 28a696fb75a585c0a0b6845ebd495d3edf2c06a2 Mon Sep 17 00:00:00 2001 From: Yonatan Teklemariam Date: Tue, 7 Jul 2026 07:29:16 +0100 Subject: [PATCH 1/6] reverted to original --- Sprint-1/fix/median.js | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index b22590bc6..a1e52d497 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -6,9 +6,31 @@ // 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.length === 0) { + return null; + } + const numbersOnly = list.filter((item) => typeof item === "number"); + if (numbersOnly.length === 0) { + return null; + } + const sorted = [...numbersOnly].sort((a, b) => a - b); + const middleIndex = Math.floor(sorted.length / 2); + if (sorted.length % 2 === 0) { + const median = (sorted[middleIndex - 1] + sorted[middleIndex]) / 2; + return median; + } + return sorted[middleIndex]; } +/* +const middleIndex = Math.floor(list.length / 2); +const median = list.splice(middleIndex, 1)[0]; +return median; +*/ module.exports = calculateMedian; +/* + + */ From cff19af51abaccc5438b6f625a4565711f1ac370 Mon Sep 17 00:00:00 2001 From: Yonatan Teklemariam Date: Tue, 7 Jul 2026 07:45:43 +0100 Subject: [PATCH 2/6] reverted to original --- Sprint-1/fix/median.js | 28 +++------------------------- 1 file changed, 3 insertions(+), 25 deletions(-) diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index a1e52d497..b22590bc6 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -6,31 +6,9 @@ // or 'list' has mixed values (the function is expected to sort only numbers). function calculateMedian(list) { - if (!Array.isArray(list)) { - return null; - } - if (list.length === 0) { - return null; - } - const numbersOnly = list.filter((item) => typeof item === "number"); - if (numbersOnly.length === 0) { - return null; - } - const sorted = [...numbersOnly].sort((a, b) => a - b); - const middleIndex = Math.floor(sorted.length / 2); - if (sorted.length % 2 === 0) { - const median = (sorted[middleIndex - 1] + sorted[middleIndex]) / 2; - return median; - } - return sorted[middleIndex]; + const middleIndex = Math.floor(list.length / 2); + const median = list.splice(middleIndex, 1)[0]; + return median; } -/* -const middleIndex = Math.floor(list.length / 2); -const median = list.splice(middleIndex, 1)[0]; -return median; -*/ module.exports = calculateMedian; -/* - - */ From d29153cabab128393fa31bd451147266ccb8e821 Mon Sep 17 00:00:00 2001 From: Yonatan Teklemariam Date: Tue, 7 Jul 2026 08:05:17 +0100 Subject: [PATCH 3/6] London | 26-ITP-May | Yonatan Teklemariam | Sprint 1 | fix --- Sprint-1/fix/median.js | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index b22590bc6..f7e518ce7 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) { - const middleIndex = Math.floor(list.length / 2); - const median = list.splice(middleIndex, 1)[0]; - return median; + // this line of code checks if the list is an array and if it has any elements. If not, it returns null + if (!Array.isArray(list) || list.length === 0) { + return null; + } + // this line of code filters the list to only include numbers. And if there are no numbers in the list, it returns null + const numbersOnly = list.filter((item) => typeof item === "number"); + if (numbersOnly.length === 0) { + return null; + } + // this line of code sorts the numbers in ascending order and calculates the median based on whether the length of the sorted array is even or odd + const sorted = [...numbersOnly].sort((a, b) => a - b); + const middleIndex = Math.floor(sorted.length / 2); + if (sorted.length % 2 === 0) { + const median = (sorted[middleIndex - 1] + sorted[middleIndex]) / 2; + return median; + } + return sorted[middleIndex]; } module.exports = calculateMedian; From 1b0a4f52108b8cd6a2ab11d1661d0de108f72ee3 Mon Sep 17 00:00:00 2001 From: Yonatan Teklemariam Date: Tue, 7 Jul 2026 08:46:26 +0100 Subject: [PATCH 4/6] London | 26-May-ITP | Yonatan Teklemariam | Sprint 1 | implement --- Sprint-1/implement/dedupe.js | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/Sprint-1/implement/dedupe.js b/Sprint-1/implement/dedupe.js index 781e8718a..704b72d87 100644 --- a/Sprint-1/implement/dedupe.js +++ b/Sprint-1/implement/dedupe.js @@ -1 +1,20 @@ -function dedupe() {} +function dedupe(arr) { + if (!Array.isArray(arr)) { + return null; + } + + const newVersion = new Set(arr); + + // If no duplicates, return the original array + if (newVersion.size === arr.length) { + return arr; + } + + // Otherwise return the deduped array + const newArray = [...newVersion]; + return newArray; +} + +console.log(dedupe([1, 2, 3])); +console.log(dedupe([10, 30, 20, 50, 20, 10])); +console.log(dedupe(5, 1, 1, 2, 3, 2, 5, 8)); From 839ea3f2a16d26d2a53b0e4fea4baddba5c068a7 Mon Sep 17 00:00:00 2001 From: Yonatan Teklemariam Date: Tue, 7 Jul 2026 08:46:26 +0100 Subject: [PATCH 5/6] fixed the dedupe implementation --- Sprint-1/implement/dedupe.js | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/Sprint-1/implement/dedupe.js b/Sprint-1/implement/dedupe.js index 781e8718a..704b72d87 100644 --- a/Sprint-1/implement/dedupe.js +++ b/Sprint-1/implement/dedupe.js @@ -1 +1,20 @@ -function dedupe() {} +function dedupe(arr) { + if (!Array.isArray(arr)) { + return null; + } + + const newVersion = new Set(arr); + + // If no duplicates, return the original array + if (newVersion.size === arr.length) { + return arr; + } + + // Otherwise return the deduped array + const newArray = [...newVersion]; + return newArray; +} + +console.log(dedupe([1, 2, 3])); +console.log(dedupe([10, 30, 20, 50, 20, 10])); +console.log(dedupe(5, 1, 1, 2, 3, 2, 5, 8)); From 692acc0e8475e63d01916ba277e5229a1add2e93 Mon Sep 17 00:00:00 2001 From: Yonatan Teklemariam Date: Wed, 8 Jul 2026 18:28:10 +0100 Subject: [PATCH 6/6] fixed the tests, implemented the functions and re-run the test to confirm everything goes smoothly --- Sprint-1/implement/dedupe.test.js | 5 ++++- Sprint-1/implement/max.js | 16 ++++++++++++++++ Sprint-1/implement/max.test.js | 27 +++++++++++++++++++++------ Sprint-1/implement/sum.js | 7 +++++++ Sprint-1/implement/sum.test.js | 24 ++++++++++++++++++------ Sprint-1/refactor/includes.js | 3 +-- 6 files changed, 67 insertions(+), 15 deletions(-) diff --git a/Sprint-1/implement/dedupe.test.js b/Sprint-1/implement/dedupe.test.js index d7c8e3d8e..d222b237a 100644 --- a/Sprint-1/implement/dedupe.test.js +++ b/Sprint-1/implement/dedupe.test.js @@ -24,5 +24,8 @@ test.todo("given an empty array, it returns an empty array"); // 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. +/* + +*/ diff --git a/Sprint-1/implement/max.js b/Sprint-1/implement/max.js index 6dd76378e..ca16c8942 100644 --- a/Sprint-1/implement/max.js +++ b/Sprint-1/implement/max.js @@ -1,4 +1,20 @@ function findMax(elements) { + let max = -Infinity; + for (const value of elements) { + if (typeof value === "number" && !Number.isNaN(value)) { + if (value > max) { + max = value; + } + } + } + return max; } +console.log(findMax([])); // -Infinity +console.log(findMax([42])); // 42 +console.log(findMax([-10, 20, -5, 7])); // 20 +console.log(findMax([-50, -3, -100])); // -3 +console.log(findMax([1.5, 3.2, 2.8])); // 3.2 +console.log(findMax(["hey", 10, "hi", 60, 10])); // 60 +console.log(findMax(["a", {}, null])); // -Infinity module.exports = findMax; diff --git a/Sprint-1/implement/max.test.js b/Sprint-1/implement/max.test.js index 82f18fd88..b61e50487 100644 --- a/Sprint-1/implement/max.test.js +++ b/Sprint-1/implement/max.test.js @@ -16,28 +16,43 @@ 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, it should return -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, it should return that number", () => { + expect(findMax([12])).toBe(12); +}); // 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, it should return the largest overall", () => { + expect(findMax([99, 86])).toBe(99); +}); // 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 negative numbers, it should return the closest number to zero", () => { + expect(findMax([-14, -24, -1120, -7])).toBe(-7); +}); // 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, it should return the largest decimal number", () => { + expect(findMax([0.8, 1.3, 1.79, 1.8])).toBe(1.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-numeric values, it should return the max without non-numeric values", () => { + expect(findMax(["yay", 12, "yey", 7, "yonatan", 31])).toBe(31); +}); // 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, it should return the least suprising value given", () => { + expect(findMax(["f", "r", {}, null, "$"])).toBe(-Infinity); +}); diff --git a/Sprint-1/implement/sum.js b/Sprint-1/implement/sum.js index 9062aafe3..11d4b1d03 100644 --- a/Sprint-1/implement/sum.js +++ b/Sprint-1/implement/sum.js @@ -1,4 +1,11 @@ function sum(elements) { + let total = 0; + for (const myNum of elements) { + if (typeof myNum === "number" && !Number.isNaN(myNum)) { + total += myNum; + } + } + return total; } module.exports = sum; diff --git a/Sprint-1/implement/sum.test.js b/Sprint-1/implement/sum.test.js index dd0a090ca..ed99f0341 100644 --- a/Sprint-1/implement/sum.test.js +++ b/Sprint-1/implement/sum.test.js @@ -9,28 +9,40 @@ E.g. sum(['hey', 10, 'hi', 60, 10]), target output: 80 (ignore any non-numerical const sum = require("./sum.js"); // Acceptance Criteria: - // 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 just one number, returns that number", () => { + expect(sum([54])).toBe(54); +}); // 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 with negative numbers, returns the correct total sum", () => { + expect(sum([-25, -100, 75])).toBe(-50); +}); // 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/float numbers, returns the correct total sum", () => { + expect(sum([1.5, 2.5, 3.1])).toBe(7.1); +}); // 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, returns the sum of the numerical elements", () => { + expect(sum(["yoyo", 32, 10, "aha"])).toBe(42); +}); // 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-numeric values, returns 0", () => { + expect(sum(["£", "&", null, undefined, "abc"])).toBe(0); +}); diff --git a/Sprint-1/refactor/includes.js b/Sprint-1/refactor/includes.js index 29dad81f0..8c9ae2e66 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 (const element of list) { if (element === target) { return true; }