From baed97a27ad6b67f8e4a856510f3d6fd3aeba61d Mon Sep 17 00:00:00 2001 From: ldfajardo10-tech Date: Sat, 22 Nov 2025 20:29:21 +0100 Subject: [PATCH 1/8] 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/8] 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/8] 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/8] 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/8] 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 42993dd671b67186f35e8f94fb5e5ceacaf21d51 Mon Sep 17 00:00:00 2001 From: ldfajardo10-tech Date: Wed, 7 Jan 2026 17:50:27 +0100 Subject: [PATCH 6/8] refactoring implementation and fixing the test syntax --- Sprint-1/implement/max.js | 20 +++++++++++++------- Sprint-1/implement/max.test.js | 14 +++++++------- 2 files changed, 20 insertions(+), 14 deletions(-) diff --git a/Sprint-1/implement/max.js b/Sprint-1/implement/max.js index 83def2214..417d666cb 100644 --- a/Sprint-1/implement/max.js +++ b/Sprint-1/implement/max.js @@ -1,12 +1,18 @@ function findMax(elements) { - const nums = elements.filter(i => typeof i === "number" && !isNaN(i)); + const arr = elements.filter(i => typeof i === "number" && !isNaN(i)); - if (nums.length === 0) { - return "-infinity"; - } else { - return Math.max(...nums); + if (arr.length === 0) { + return -Infinity; } - -} + let max = arr[0]; + + for (const num of arr) { + if (num > max) { + max = num; + } + } + + return max; +} module.exports = findMax; diff --git a/Sprint-1/implement/max.test.js b/Sprint-1/implement/max.test.js index d6e158716..7cdebeb19 100644 --- a/Sprint-1/implement/max.test.js +++ b/Sprint-1/implement/max.test.js @@ -17,7 +17,7 @@ const findMax = require("./max.js"); // Then it should return -Infinity // Delete this test.todo and replace it with a test. test("given an empty array, returns -Infinity", () => { - expect(findMax([])).toEqual("-infinity"); + expect(findMax([])).toBe(-Infinity); }); @@ -25,35 +25,35 @@ test("given an empty array, returns -Infinity", () => { // 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]); + expect(findMax([50])).toBe(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); + expect(findMax([-1, -3, 5, 7, 8])).toBe(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); + expect(findMax([-7, -5, -2])).toBe(-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); + expect(findMax([2.34, 2.35])).toBe(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); + expect(findMax(["hello", 2, 5, -1])).toBe(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"); + expect(findMax(["5", "hello", "a2"])).toBe(-Infinity); }); \ No newline at end of file From 4e5b955bd827e3669bf60b018242b99329452312 Mon Sep 17 00:00:00 2001 From: ldfajardo10-tech Date: Wed, 7 Jan 2026 18:01:43 +0100 Subject: [PATCH 7/8] sum test exercise fixed --- Sprint-1/implement/sum.test.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/Sprint-1/implement/sum.test.js b/Sprint-1/implement/sum.test.js index 055a09239..c0ce9f1e2 100644 --- a/Sprint-1/implement/sum.test.js +++ b/Sprint-1/implement/sum.test.js @@ -14,36 +14,36 @@ const sum = require("./sum.js"); // When passed to the sum function // Then it should return 0 test("given an empty array, returns 0", () => { - expect(sum([])).toEqual(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[10, 20, 35]).toEqual(85); + expect(sum([10, 20, 35])).toBe(65); }); // 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); + expect(sum([-10, -30, -45])).toBe(-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); + expect(sum([10.5, 30, 45])).toBe(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); + expect(sum([NaN, null, 45, 10])).toBe(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); + expect(sum([NaN, null, "hello"])).toBe(0); }); \ No newline at end of file From 13c1fe9171bee4b82f17ac8bc1122d9155079f85 Mon Sep 17 00:00:00 2001 From: ldfajardo10-tech Date: Wed, 7 Jan 2026 18:05:03 +0100 Subject: [PATCH 8/8] small correction with exports --- Sprint-1/implement/dedupe.js | 1 + 1 file changed, 1 insertion(+) diff --git a/Sprint-1/implement/dedupe.js b/Sprint-1/implement/dedupe.js index 8709f15ca..21a81f17f 100644 --- a/Sprint-1/implement/dedupe.js +++ b/Sprint-1/implement/dedupe.js @@ -6,3 +6,4 @@ console.log(dedupe([1, 1, 2, 3, 2, 3, 3])); console.log(dedupe([])); console.log(dedupe(["hello", undefined, 5, 5, "hello"])); +module.exports = dedupe; \ No newline at end of file