From 9b10e033ea9f8bd83d1d948a0d3506635c286a0b Mon Sep 17 00:00:00 2001 From: Nik Bezdzenariy Date: Wed, 8 Apr 2026 20:17:06 +0200 Subject: [PATCH 1/3] fix: implement calculateMedian with input validation and sorting --- 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..9f0fd5b21 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; + // 1 check if input is valid non-empty array + if (!Array.isArray(list) || list.length === 0) { + return null; + } + + // 2 filter to keep only numeric values + const numbersOnly = list.filter((item) => typeof item === "number"); + + // 3 return null if no numbers remain after filtering + if (numbersOnly.length === 0) { + return null; + } + + // 4 create a copy and sort it numerically + const sorted = [...numbersOnly].sort((a, b) => a - b); + + const len = sorted.length; + const mid = Math.floor(len / 2); + + // 5 calculate median based on parity + if (len % 2 !== 0) { + return sorted[mid]; + } else { + return (sorted[mid - 1] + sorted[mid]) / 2; + } } module.exports = calculateMedian; From ab1341142ca4f284ef76b85b545e1064be7d8abb Mon Sep 17 00:00:00 2001 From: Nik Bezdzenariy Date: Thu, 9 Apr 2026 15:48:20 +0200 Subject: [PATCH 2/3] feat: implement max, sum and dedupe functions --- Sprint-1/implement/dedupe.js | 11 ++++++++++- Sprint-1/implement/dedupe.test.js | 11 +++++++++++ Sprint-1/implement/max.js | 14 +++++++++++++- Sprint-1/implement/max.test.js | 17 ++++++++++++++++- Sprint-1/implement/sum.js | 12 +++++++++++- Sprint-1/implement/sum.test.js | 15 ++++++++++++++- 6 files changed, 75 insertions(+), 5 deletions(-) diff --git a/Sprint-1/implement/dedupe.js b/Sprint-1/implement/dedupe.js index 781e8718a..5704441a1 100644 --- a/Sprint-1/implement/dedupe.js +++ b/Sprint-1/implement/dedupe.js @@ -1 +1,10 @@ -function dedupe() {} +function dedupe(list) { + // 1. array valid + if(!Array.isArray(list)) return []; + + // 2. Set to remove duplicates + // spread Set back into a new array [...] + return [...new Set(list)]; +} +module.exports = dedupe; + diff --git a/Sprint-1/implement/dedupe.test.js b/Sprint-1/implement/dedupe.test.js index 23e0f8638..d1b3f0a78 100644 --- a/Sprint-1/implement/dedupe.test.js +++ b/Sprint-1/implement/dedupe.test.js @@ -18,6 +18,17 @@ E.g. dedupe([1, 2, 1]) target output: [1, 2] // Then it should return an empty array test.todo("given an empty array, it returns an empty array"); +test("returns an empty array for empty input", () => { + expect(dedupe([])).toEqual([]); +}); + +test("removes duplicates from numbers and strings", () => { + expect(dedupe([1, 1, "b", "b", 2])).toEqual([1, "b", 2]); +}); + +test("preserves the first occurrence", () => { + expect(dedupe(["first", "second", "first"])).toEqual(["first", "second" ]); +}); // Given an array with no duplicates // When passed to the dedupe function // Then it should return a copy of the original array diff --git a/Sprint-1/implement/max.js b/Sprint-1/implement/max.js index 6dd76378e..98a3540ca 100644 --- a/Sprint-1/implement/max.js +++ b/Sprint-1/implement/max.js @@ -1,4 +1,16 @@ -function findMax(elements) { +function findMax(list) { + // 1. array check + if (!Array.isArray(list)) return null; + + // 2. cleaning + const numbersOnly = list.filter(item => typeof item === `number`); + + // 3. the -Infinity rule (for empty or non-numeric arrays) + if (numbersOnly.length === 0) return -Infinity; + + // 4. math magic + return Math.max(...numbersOnly); + } module.exports = findMax; diff --git a/Sprint-1/implement/max.test.js b/Sprint-1/implement/max.test.js index 82f18fd88..31740df73 100644 --- a/Sprint-1/implement/max.test.js +++ b/Sprint-1/implement/max.test.js @@ -16,7 +16,22 @@ 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 arra, returns -Infinity", () => { + expect(findMax([])).toBe(-Infinity); +}); + +test("given an array with one number, returns that number", () => { + expect(findMax([42])).toBe(42); +}); + +test("returns the largest number (closest to zero) for negative numbers", () => { + expect(findMax([-21, -5, -121])).toBe(-5); +}); + +test("ignores non-number values", () => { + expect(findMax(["apple", 10, null, 50, "orange"])).toBe(50); +}); // Given an array with one number // When passed to the max function diff --git a/Sprint-1/implement/sum.js b/Sprint-1/implement/sum.js index 9062aafe3..a8ba1e247 100644 --- a/Sprint-1/implement/sum.js +++ b/Sprint-1/implement/sum.js @@ -1,4 +1,14 @@ -function sum(elements) { +function sum(list) { + // 1. array valid + if (!Array.isArray(list)) return 0; + + // 2. filter and add them up + // acc = accumulator (sum so far), curr = current value + return list + .filter((item) => typeof item === "number") + .reduce((acc, curr) => acc + curr, 0); + // 0 is the starting point. + // If array empty after filter, returns 0. } module.exports = sum; diff --git a/Sprint-1/implement/sum.test.js b/Sprint-1/implement/sum.test.js index dd0a090ca..f91eb4eac 100644 --- a/Sprint-1/implement/sum.test.js +++ b/Sprint-1/implement/sum.test.js @@ -13,8 +13,21 @@ 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([])).toBe(0); +}); +test("sums up positive, negative and decimal numbers", () => { + expect(sum([10, -5, 2.5])).toBe(7.5); +}); + +test("ignores non-numeric values", () => { + expect(sum(["apple", 11, null, 21,])).toBe(32) +}); + +test("returns 0 for an array with only non-numbers", () => { + expect(sum(["a", "b"])).toBe(0); +}); // Given an array with just one number // When passed to the sum function // Then it should return that number From e3032b09718eed19dd67e770fcf741c5a358a661 Mon Sep 17 00:00:00 2001 From: Nik Bezdzenariy Date: Thu, 9 Apr 2026 17:37:33 +0200 Subject: [PATCH 3/3] refactor: convert includes to for...of loop and add AoC day 1 solution --- Sprint-1/refactor/includes.js | 10 ++++++---- Sprint-1/stretch/aoc-2018-day1/solution.js | 15 +++++++++++++++ 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/Sprint-1/refactor/includes.js b/Sprint-1/refactor/includes.js index 29dad81f0..a23cae407 100644 --- a/Sprint-1/refactor/includes.js +++ b/Sprint-1/refactor/includes.js @@ -1,13 +1,15 @@ // 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]; + // don't need 'index' or 'list.length' anymore + // just say: "for every item of the list..." + for (const element of list) { if (element === target) { - return true; + return true; // Found it! Stop and return } } - return false; + + return false; // Checked everything, found nothing } module.exports = includes; diff --git a/Sprint-1/stretch/aoc-2018-day1/solution.js b/Sprint-1/stretch/aoc-2018-day1/solution.js index e69de29bb..82acc93bc 100644 --- a/Sprint-1/stretch/aoc-2018-day1/solution.js +++ b/Sprint-1/stretch/aoc-2018-day1/solution.js @@ -0,0 +1,15 @@ +const fs = require("fs"); + +// read file from current direct using UTF-8 encoding +const input = fs.readFileSync("./input.txt", "utf8"); + +const changes = input + .split("\n") // split string into array by new lines + .filter((line) => line.trim() !== "") // remove empty string or space only lines + .map(Number); // convert string to values into numbers + +// calculate total frequency +const result = changes.reduce((total, change) => total + change, 0); + +console.log("Resulting frequency:", result); +