From baed97a27ad6b67f8e4a856510f3d6fd3aeba61d Mon Sep 17 00:00:00 2001 From: ldfajardo10-tech Date: Sat, 22 Nov 2025 20:29:21 +0100 Subject: [PATCH 1/7] fix median exercise solution --- 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..eb45b6a3e 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; + } + + const nums = list.filter(v => typeof v === "number" && !isNaN(v)); + + if (nums.length === 0) { + return null; + } + + const sorted = [...nums].sort((a, b) => a - b); + + const len = sorted.length; + const mid = Math.floor(len / 2); + + + if (len % 2 === 1) { + return sorted[mid]; + } + + return (sorted[mid - 1] + sorted[mid]) / 2; } + + + module.exports = calculateMedian; From cb8d00627dafbb3c6d81c107c7f4a68366fc4232 Mon Sep 17 00:00:00 2001 From: ldfajardo10-tech Date: Tue, 25 Nov 2025 20:37:20 +0100 Subject: [PATCH 2/7] solution median exercise --- Sprint-1/fix/median.js | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index eb45b6a3e..5883f864a 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -11,7 +11,7 @@ function calculateMedian(list) { return null; } - const nums = list.filter(v => typeof v === "number" && !isNaN(v)); + const nums = list.filter(i => typeof i === "number" && !isNaN(i)); if (nums.length === 0) { return null; @@ -30,7 +30,4 @@ function calculateMedian(list) { return (sorted[mid - 1] + sorted[mid]) / 2; } - - - module.exports = calculateMedian; From efdf47b92f60edfce19ee392f0a13ab27747c175 Mon Sep 17 00:00:00 2001 From: ldfajardo10-tech Date: Tue, 25 Nov 2025 20:41:22 +0100 Subject: [PATCH 3/7] max exercise solution --- Sprint-1/implement/max.js | 8 ++++++++ Sprint-1/implement/max.test.js | 28 ++++++++++++++++++++++------ 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/Sprint-1/implement/max.js b/Sprint-1/implement/max.js index 6dd76378e..83def2214 100644 --- a/Sprint-1/implement/max.js +++ b/Sprint-1/implement/max.js @@ -1,4 +1,12 @@ function findMax(elements) { + const nums = elements.filter(i => typeof i === "number" && !isNaN(i)); + + if (nums.length === 0) { + return "-infinity"; + } else { + return Math.max(...nums); + } + } module.exports = findMax; diff --git a/Sprint-1/implement/max.test.js b/Sprint-1/implement/max.test.js index 82f18fd88..d6e158716 100644 --- a/Sprint-1/implement/max.test.js +++ b/Sprint-1/implement/max.test.js @@ -16,28 +16,44 @@ 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[50]).toEqual([50]); +}) // 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 positive and negative numbers; returns largest overall", () => { + expect(findMax[-1, -3, 5, 7, 8]).toEqual(8); +}); // 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, returns closest to zero", () => { + expect(findMax[-7, -5, -2]).toEqual(-2); +}); // 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, returns the largest decimal number", () => { + expect(findMax[2.34, 2.35]).toEqual(2.35); +}); // 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-numbers, returns the max and ignore non-numeric values", () => { + expect(findMax["hello", 2, 5, -1]).toEqual(5); +}); // 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-numbers values, returns the largest surprising ", () => { + expect(findMax["5", "hello", "a2"]).toEqual("-infinity"); +}); \ No newline at end of file From 6ae9e834e915441c2f510455689092bbe2cf69ff Mon Sep 17 00:00:00 2001 From: ldfajardo10-tech Date: Wed, 26 Nov 2025 14:13:08 +0100 Subject: [PATCH 4/7] sum exercise solution --- Sprint-1/implement/sum.js | 17 ++++++++++++++++- Sprint-1/implement/sum.test.js | 23 ++++++++++++++++++----- 2 files changed, 34 insertions(+), 6 deletions(-) diff --git a/Sprint-1/implement/sum.js b/Sprint-1/implement/sum.js index 9062aafe3..d4c6fd8dc 100644 --- a/Sprint-1/implement/sum.js +++ b/Sprint-1/implement/sum.js @@ -1,4 +1,19 @@ -function sum(elements) { +function sum(elements) { + const numbers = elements.filter((i) => typeof i === "number" && !isNaN(i)); + + if (numbers.length === 0) { + return 0; + } + + return numbers.reduce((total, num) => total + num, 0); } +console.log(sum([10, 30, 45])); +console.log(sum([10])); +console.log(sum([-10, -30, -45])); +console.log(sum([])); +console.log(sum([10.5, 30, 45])); +console.log(sum([NaN, null, 45, 10])); +console.log(sum([NaN, null, "hello"])); + module.exports = sum; diff --git a/Sprint-1/implement/sum.test.js b/Sprint-1/implement/sum.test.js index dd0a090ca..055a09239 100644 --- a/Sprint-1/implement/sum.test.js +++ b/Sprint-1/implement/sum.test.js @@ -13,24 +13,37 @@ 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 just one number, returns that number", () => { + expect(sum[10, 20, 35]).toEqual(85); +}); // 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 containing negative numbers, returns the correct total sum", () => { + expect(sum[-10, -30, -45]).toEqual(-85); +}); // 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[10.5, 30, 45]).toEqual(85.5); +}); // 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 containing non-number values, ignore the non-numerical values and return the sum of the numerical elements", () => { + expect(sum[NaN, null, 45, 10]).toEqual(55); +}); // 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-number values, returns the least surprising value", () => { + expect(sum[NaN, null, "hello"]).toEqual(55); +}); \ No newline at end of file From 86a348ea5e2ec36b9474813aff65aaf0c2bdf306 Mon Sep 17 00:00:00 2001 From: ldfajardo10-tech Date: Wed, 26 Nov 2025 14:42:07 +0100 Subject: [PATCH 5/7] dedupe exercise solution --- Sprint-1/implement/dedupe.js | 9 ++++++++- Sprint-1/implement/dedupe.test.js | 12 ++++++++++-- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/Sprint-1/implement/dedupe.js b/Sprint-1/implement/dedupe.js index 781e8718a..8709f15ca 100644 --- a/Sprint-1/implement/dedupe.js +++ b/Sprint-1/implement/dedupe.js @@ -1 +1,8 @@ -function dedupe() {} +function dedupe(arr) { + return [...new Set (arr)]; +} +console.log(dedupe([1, 2, 3])); +console.log(dedupe([1, 1, 2, 3, 2, 3, 3])); +console.log(dedupe([])); +console.log(dedupe(["hello", undefined, 5, 5, "hello"])); + diff --git a/Sprint-1/implement/dedupe.test.js b/Sprint-1/implement/dedupe.test.js index 23e0f8638..bc6c95743 100644 --- a/Sprint-1/implement/dedupe.test.js +++ b/Sprint-1/implement/dedupe.test.js @@ -16,12 +16,20 @@ 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("given an array with no duplicates, it returns a copy of the original array", () => { + expect(dedupe([1, 2, 3])).toEqual([1, 2, 3]); +}); // 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 +// Then it should remove the duplicate values, preserving the first occurrence of each element +test("Given an array with strings or numbers, remove the duplicate values, preserving the first occurrence of each element", () => { + expect(dedupe(["hello", undefined, 5, 5, "hello"])).toEqual(["hello", undefined, 5,]); +}); \ No newline at end of file From 053f5580a3a939707be75fbbc9a84c83e21179b6 Mon Sep 17 00:00:00 2001 From: ldfajardo10-tech Date: Wed, 26 Nov 2025 14:57:40 +0100 Subject: [PATCH 6/7] refactor exercise solution --- Sprint-1/refactor/includes.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Sprint-1/refactor/includes.js b/Sprint-1/refactor/includes.js index 29dad81f0..3f65fd8f4 100644 --- a/Sprint-1/refactor/includes.js +++ b/Sprint-1/refactor/includes.js @@ -1,8 +1,8 @@ // 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; } From ea1ff44aa2d74780de098788c08f1086fb4bc425 Mon Sep 17 00:00:00 2001 From: ldfajardo10-tech Date: Wed, 7 Jan 2026 18:18:09 +0100 Subject: [PATCH 7/7] refactor includes exercise updated --- Sprint-1/refactor/includes.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sprint-1/refactor/includes.js b/Sprint-1/refactor/includes.js index 3f65fd8f4..8ddbb4dcc 100644 --- a/Sprint-1/refactor/includes.js +++ b/Sprint-1/refactor/includes.js @@ -7,7 +7,7 @@ function includes(list, target) { return true; } } - return false; + return false; // return false added } module.exports = includes;