From 6dcfc89b48b8601972fc3189d61acd58d9b079bd Mon Sep 17 00:00:00 2001 From: SalahAlsabhi Date: Fri, 30 Oct 2026 21:18:46 +0000 Subject: [PATCH 1/9] Fix calculateMedian: sort, even length, no mutation, validation --- Sprint-1/fix/median.js | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index 5c5b796e1..53b578809 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -6,9 +6,28 @@ // or contains values that aren't numbers (the function is expected to throw - see the tests). function calculateMedian(list) { - const middleIndex = Math.floor(list.length / 2); - const median = list.splice(middleIndex, 1)[0]; - return median; + if (!Array.isArray(list)) { + throw new Error("calculateMedian requires an array of numbers"); + } + + if (list.length === 0) { + throw new Error("calculateMedian requires a non-empty array"); + } + + for (const item of list) { + if (typeof item !== "number") { + throw new Error("calculateMedian requires an array of numbers"); + } + } + + const sorted = list.slice().sort((a, b) => a - b); + const middleIndex = Math.floor(sorted.length / 2); + + if (sorted.length % 2 === 0) { + return (sorted[middleIndex - 1] + sorted[middleIndex]) / 2; + } + + return sorted[middleIndex]; } module.exports = calculateMedian; From 553bc808948c9a9d39bdd604af9a5979bc6a263a Mon Sep 17 00:00:00 2001 From: SalahAlsabhi Date: Fri, 30 Oct 2026 22:58:02 +0000 Subject: [PATCH 2/9] Implement findMax with tests; fix calculateMedian --- Sprint-1/implement/max.js | 22 +++++++++++++++++++ Sprint-1/implement/max.test.js | 40 +++++++++++++++++++++++++++++++--- 2 files changed, 59 insertions(+), 3 deletions(-) diff --git a/Sprint-1/implement/max.js b/Sprint-1/implement/max.js index 6dd76378e..0e33836b7 100644 --- a/Sprint-1/implement/max.js +++ b/Sprint-1/implement/max.js @@ -1,4 +1,26 @@ + function findMax(elements) { + if (!Array.isArray(elements)) { + throw new Error("findMax requires an array of numbers"); + } + + for (const item of elements) { + if (typeof item !== "number") { + throw new Error("findMax requires an array of numbers"); + } + } + + if (elements.length === 0) { + return -Infinity; + } + + let max = elements[0]; + for (const item of elements) { + if (item > max) { + max = item; + } + } + return max; } module.exports = findMax; diff --git a/Sprint-1/implement/max.test.js b/Sprint-1/implement/max.test.js index ff3fbb5e5..8156ef9f7 100644 --- a/Sprint-1/implement/max.test.js +++ b/Sprint-1/implement/max.test.js @@ -15,29 +15,63 @@ const findMax = require("./max.js"); // Given an empty array // 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, should return thant number", () => { + expect(findMax([10])).toEqual(10); +}); // 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, should return the largest number overall", () => { + expect(findMax([30, -7, 1, 45, -70, 10, -10])).toEqual(45); +}); // 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 just negative numbers, should return the closest one to zero", () => { + expect(findMax([-15, -10, -7, -20, -70])).toEqual(-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, should return the largest decimal number", () => { + expect(findMax([3.5, 2.7, 1.9, 5.1])).toEqual(5.1); +}); // Given an array containing a value that isn't a number // When passed to the max function // Then it should throw Error("findMax requires an array of numbers") - +test("Throws an error when array contains a value that isn't a number", () => { + expect(() => findMax(["plane", 10, "car", 60, 10])).toThrow( + new Error("findMax requires an array of numbers") + ); +}); // Given something that isn't an array at all, such as "hey", 42 or no argument // When passed to the max function // Then it should throw Error("findMax requires an array of numbers") +test("Throws an error when the input is not an array at all", () => { + expect(() => findMax("hey")).toThrow( + new Error("findMax requires an array of numbers") + ); +}); + +test("Throws an error when given a number", () => { + expect(() => findMax(36)).toThrow( + new Error("findMax requires an array of numbers") + ); +}); + +test("Throws an error when called with no argument", () => { + expect(() => findMax()).toThrow( + new Error("findMax requires an array of numbers") + ); +}); From 8e213b2eb312625aca643a7517a29c753958a09e Mon Sep 17 00:00:00 2001 From: SalahAlsabhi Date: Fri, 30 Oct 2026 23:39:18 +0000 Subject: [PATCH 3/9] Implement sum with tests --- Sprint-1/implement/sum.js | 15 +++++++++++++++ Sprint-1/implement/sum.test.js | 24 +++++++++++++++++++++++- 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/Sprint-1/implement/sum.js b/Sprint-1/implement/sum.js index 9062aafe3..a1474f1f0 100644 --- a/Sprint-1/implement/sum.js +++ b/Sprint-1/implement/sum.js @@ -1,4 +1,19 @@ function sum(elements) { + if (!Array.isArray(elements)) { + throw new Error("sum requires an array of numbers"); + } + + for (const item of elements) { + if (typeof item !== "number") { + throw new Error("sum requires an array of numbers"); + } + } + + let total = 0; + for (const item of elements) { + total += item; + } + return total; } module.exports = sum; diff --git a/Sprint-1/implement/sum.test.js b/Sprint-1/implement/sum.test.js index a288e0f0a..ef591a15f 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, should return 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, should return that number", () => { + expect(sum([10])).toEqual(10); +}); // 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, should still return the correct total sum", () => { + expect(sum([-3, -7, -2])).toEqual(-12); +}); // 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, should return the correct total sum", () => { + expect(sum([1.3, 2.7, 3.5])).toEqual(7.5); +}); // Given an array containing a value that isn't a number // When passed to the sum function // Then it should throw Error("sum requires an array of numbers") +test("Throws an error when an array containing a value that isn't a number", () => { + expect(() => sum([15, "car", 11])).toThrow( + new Error("sum requires an array of numbers") + ); +}); // Given something that isn't an array at all, such as "hey", 42 or no argument // When passed to the sum function // Then it should throw Error("sum requires an array of numbers") +test("Throws an error when given something that isn't an array at all", () => { + expect(() => sum("string")).toThrow( + new Error("sum requires an array of numbers") + ); +}); + From 1fff2564db0a65772f4da937c290b7d9a81dd3ad Mon Sep 17 00:00:00 2001 From: SalahAlsabhi Date: Sat, 31 Oct 2026 15:07:51 +0000 Subject: [PATCH 4/9] Implement dedupe with tests --- Sprint-1/implement/dedupe.js | 6 +++++- Sprint-1/implement/dedupe.test.js | 24 ++++++++++++++++++++++-- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/Sprint-1/implement/dedupe.js b/Sprint-1/implement/dedupe.js index 781e8718a..47c5ef797 100644 --- a/Sprint-1/implement/dedupe.js +++ b/Sprint-1/implement/dedupe.js @@ -1 +1,5 @@ -function dedupe() {} +function dedupe(arr) { + return arr.filter((item, index) => arr.indexOf(item) === index); +} + +module.exports = dedupe; diff --git a/Sprint-1/implement/dedupe.test.js b/Sprint-1/implement/dedupe.test.js index d7c8e3d8e..569e72fd2 100644 --- a/Sprint-1/implement/dedupe.test.js +++ b/Sprint-1/implement/dedupe.test.js @@ -16,13 +16,33 @@ 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"); +test("Given an empty array, should return 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, should return a copy of the original array", () => { + expect([5, 1, 2, 3, 8]).toEqual([5, 1, 2, 3, 8]); +}); // 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. +test("Given an array of strings, removes duplicates and keeps first occurrence", () => { + expect(dedupe(["car", "plane", "car", "train", "plane"])).toEqual([ + "car", + "plane", + "train", + ]); +}); + +test("Given an array of numbers, removes duplicates and keeps first occurrence", () => { + expect(dedupe([5, 1, 1, 2, 3, 2, 5, 8])).toEqual([5, 1, 2, 3, 8]); +}); + +test("Given [1, 2, 1], returns [1, 2]", () => { + expect(dedupe([1, 2, 1])).toEqual([1, 2]); +}); From de04a51de81165aff6dd9c1359d1a5955ca61503 Mon Sep 17 00:00:00 2001 From: SalahAlsabhi Date: Sat, 31 Oct 2026 18:17:57 +0000 Subject: [PATCH 5/9] Implement calculateMean with tests --- Sprint-1/implement/mean.js | 23 ++++++++++++++++++- Sprint-1/implement/mean.test.js | 40 +++++++++++++++++++++++++++++++-- 2 files changed, 60 insertions(+), 3 deletions(-) diff --git a/Sprint-1/implement/mean.js b/Sprint-1/implement/mean.js index 36909ea1f..d80973ade 100644 --- a/Sprint-1/implement/mean.js +++ b/Sprint-1/implement/mean.js @@ -1,3 +1,24 @@ -function calculateMean(list) {} +function calculateMean(list) { + if (!Array.isArray(list)) { + throw new Error("calculateMean requires an array of numbers"); + } + + if (list.length === 0) { + throw new Error("calculateMean requires a non-empty array"); + } + + for (const item of list) { + if (typeof item !== "number") { + throw new Error("calculateMean requires an array of numbers"); + } + } + + let sum = 0; + for (const num of list) { + sum += num; + } + return sum / list.length; +} module.exports = calculateMean; + diff --git a/Sprint-1/implement/mean.test.js b/Sprint-1/implement/mean.test.js index 847b88676..ff82a2112 100644 --- a/Sprint-1/implement/mean.test.js +++ b/Sprint-1/implement/mean.test.js @@ -22,29 +22,65 @@ const calculateMean = require("./mean.js"); // Given an array of numbers // When passed to calculateMean // Then it should return their mean -// Delete this test.todo and replace it with a test. -test.todo("given [1, 2, 6], returns 3"); + +test("Given an array of numbers, should return their mean", () => { + expect(calculateMean([3, 7, 50,40])).toEqual(25); +}); // Given an array with a single number // When passed to calculateMean // Then it should return that number +test("Given an array with a single number, should return that number", () => { + expect(calculateMean([10])).toEqual(10); +}); // Given an array containing negative or decimal numbers // When passed to calculateMean // Then it should return the correct mean +test("Given an array containing negative numbers, should return the correct mean", () => { + expect(calculateMean([-3, -50, -7, -40])).toEqual(-25); +}); + +test("Given an array containing decimal numbers, should return the correct mean", () => { + expect(calculateMean([2.2, 1.9, 3.5, 4.1])).toEqual(2.925); +}); + +test("Given an array containing both negative and decimal numbers, should return the correct mean", () => { + expect(calculateMean([2.2, -1.9, 3.5, -4.1])).toEqual(-0.07499999999999984); +}); // Given an empty array // When passed to calculateMean // Then it should throw Error("calculateMean requires a non-empty array") +test("Throws an error when given an empty array", () => { + expect(() => calculateMean([])).toThrow( + new Error("calculateMean requires a non-empty array") + ); +}); // Given a value that isn't an array, e.g. "banana", 42, null or {} // When passed to calculateMean // Then it should throw Error("calculateMean requires an array of numbers") +test("Throws an error when given a value that isn't an array", () => { + expect(() => calculateMean(["banana"])).toThrow( + new Error("calculateMean requires an array of numbers") + ); +}); // Given no argument at all // When passed to calculateMean // Then it should throw Error("calculateMean requires an array of numbers") +test("Throws an error when given no argument at all", () => { + expect(() => calculateMean()).toThrow( + new Error("calculateMean requires an array of numbers") + ); +}); // Given an array containing a non-number value, e.g. [1, "2", 3] // When passed to calculateMean // Then it should throw Error("calculateMean requires an array of numbers") +test("Throws an error when given an array containing a non-number value", () => { + expect(() => calculateMean([1, "2", 3])).toThrow( + new Error("calculateMean requires an array of numbers") + ); +}); From a3704f20f58497588b064592268b60185c1af412 Mon Sep 17 00:00:00 2001 From: SalahAlsabhi Date: Sat, 31 Oct 2026 23:22:25 +0000 Subject: [PATCH 6/9] Implement dedupe, mean, and describeMedian with tests --- Sprint-1/implement/describe-median.js | 9 ++++++++- Sprint-1/implement/describe-median.test.js | 22 ++++++++++++++++++++-- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/Sprint-1/implement/describe-median.js b/Sprint-1/implement/describe-median.js index 3381cfc0a..f6fdd5660 100644 --- a/Sprint-1/implement/describe-median.js +++ b/Sprint-1/implement/describe-median.js @@ -20,6 +20,13 @@ function calculateMedian(list) { } // Implement this function. See describe-median.test.js for the acceptance criteria. -function describeMedian(list) {} +function describeMedian(list) { + try { + const median = calculateMedian(list); + return `The median is ${median}`; + } catch (error) { + return `Could not calculate a median: ${error.message}`; + } +} module.exports = describeMedian; diff --git a/Sprint-1/implement/describe-median.test.js b/Sprint-1/implement/describe-median.test.js index e7d18a264..f04bc5929 100644 --- a/Sprint-1/implement/describe-median.test.js +++ b/Sprint-1/implement/describe-median.test.js @@ -20,13 +20,31 @@ const describeMedian = require("./describe-median.js"); // Given an array of numbers // When passed to describeMedian // Then it should return "The median is " followed by the median -// Delete this test.todo and replace it with a test. -test.todo('given [1, 2, 3], returns "The median is 2"'); +test('Given an array of numbers, returns "The median is " followed by the median', () => { + expect(describeMedian([5, 6, 7])).toEqual("The median is 6"); +}); // Given an empty array // When passed to describeMedian // Then it should return "Could not calculate a median: calculateMedian requires a non-empty array" +test("Given an empty array, returns could-not-calculate message", () => { + expect(describeMedian([])).toEqual( + "Could not calculate a median: calculateMedian requires a non-empty array" + ); +}); // Given something that isn't an array of numbers, e.g. "banana" // When passed to describeMedian // Then it should return "Could not calculate a median: calculateMedian requires an array of numbers" +test("Given non-numbers, returns could-not-calculate message", () => { + expect(describeMedian(["banana"])).toEqual( + "Could not calculate a median: calculateMedian requires an array of numbers" + ); +}); + +test("Given a string, returns could-not-calculate message", () => { + expect(describeMedian("banana")).toEqual( + "Could not calculate a median: calculateMedian requires an array of numbers" + ); +}); + From f384ff55903eaa66955453463da15b6e11a93c83 Mon Sep 17 00:00:00 2001 From: SalahAlsabhi Date: Sat, 31 Oct 2026 23:36:19 +0000 Subject: [PATCH 7/9] Refactor includes to use for...of --- 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..07a0e6877 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; } @@ -11,3 +10,4 @@ function includes(list, target) { } module.exports = includes; + From a6fcf212813166c3634867df8f1ef1823ea7c0d2 Mon Sep 17 00:00:00 2001 From: SalahAlsabhi Date: Sun, 1 Nov 2026 10:23:15 +0000 Subject: [PATCH 8/9] Solve AoC 2018 day 1 frequency calibration --- Sprint-1/stretch/aoc-2018-day1/solution.js | 42 ++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/Sprint-1/stretch/aoc-2018-day1/solution.js b/Sprint-1/stretch/aoc-2018-day1/solution.js index e69de29bb..410f18be8 100644 --- a/Sprint-1/stretch/aoc-2018-day1/solution.js +++ b/Sprint-1/stretch/aoc-2018-day1/solution.js @@ -0,0 +1,42 @@ + +import fs from "node:fs"; + +// Puzzle input +const changes = fs + .readFileSync(new URL("./input.txt", import.meta.url), "utf8") + .trim() + .split("\n") + .map(Number); + +// Part One +function calculateFrequency(changes) { + let frequency = 0; + + for (let change of changes) { + frequency += change; + } + + return frequency; +} + +// Part Two +function findRepeatedFrequency(changes) { + let frequency = 0; + const visited = new Set([0]); + + while (true) { + for (let change of changes) { + frequency += change; + + if (visited.has(frequency)) { + return frequency; + } + + visited.add(frequency); + } + } +} + +// Display answers +console.log("Part One:", calculateFrequency(changes)); +console.log("Part Two:", findRepeatedFrequency(changes)); From 244ea8600ea0df5781a45d4eda08292557428a3e Mon Sep 17 00:00:00 2001 From: SalahAlsabhi Date: Thu, 5 Nov 2026 10:14:01 +0000 Subject: [PATCH 9/9] Address PR review: copy check, NaN/finite validation, toBeCloseTo, AOC style --- Sprint-1/implement/dedupe.test.js | 6 +++++- Sprint-1/implement/max.js | 2 +- Sprint-1/implement/max.test.js | 6 ++++++ Sprint-1/implement/mean.js | 2 +- Sprint-1/implement/mean.test.js | 16 ++++++++++++++-- Sprint-1/stretch/aoc-2018-day1/solution.js | 4 +++- 6 files changed, 30 insertions(+), 6 deletions(-) diff --git a/Sprint-1/implement/dedupe.test.js b/Sprint-1/implement/dedupe.test.js index 569e72fd2..eddd4e9d4 100644 --- a/Sprint-1/implement/dedupe.test.js +++ b/Sprint-1/implement/dedupe.test.js @@ -24,7 +24,11 @@ test("Given an empty array, should return an empty array", () => { // When passed to the dedupe function // Then it should return a copy of the original array test("Given an array with no duplicates, should return a copy of the original array", () => { - expect([5, 1, 2, 3, 8]).toEqual([5, 1, 2, 3, 8]); + const original = [5, 1, 2, 3, 8]; + const result = dedupe(original); + + expect(result).toEqual([5, 1, 2, 3, 8]); + expect(result).not.toBe(original); }); // Given an array of strings or numbers diff --git a/Sprint-1/implement/max.js b/Sprint-1/implement/max.js index 0e33836b7..ddf6b202d 100644 --- a/Sprint-1/implement/max.js +++ b/Sprint-1/implement/max.js @@ -5,7 +5,7 @@ function findMax(elements) { } for (const item of elements) { - if (typeof item !== "number") { + if (typeof item !== "number" || Number.isNaN(item)) { throw new Error("findMax requires an array of numbers"); } } diff --git a/Sprint-1/implement/max.test.js b/Sprint-1/implement/max.test.js index 8156ef9f7..0485062c7 100644 --- a/Sprint-1/implement/max.test.js +++ b/Sprint-1/implement/max.test.js @@ -75,3 +75,9 @@ test("Throws an error when called with no argument", () => { new Error("findMax requires an array of numbers") ); }); + +test("Throws an error when array contains NaN", () => { + expect(() => findMax([1, NaN, 3])).toThrow( + new Error("findMax requires an array of numbers") + ); +}); diff --git a/Sprint-1/implement/mean.js b/Sprint-1/implement/mean.js index d80973ade..a048fdf08 100644 --- a/Sprint-1/implement/mean.js +++ b/Sprint-1/implement/mean.js @@ -8,7 +8,7 @@ function calculateMean(list) { } for (const item of list) { - if (typeof item !== "number") { + if (typeof item !== "number" || !Number.isFinite(item)) { throw new Error("calculateMean requires an array of numbers"); } } diff --git a/Sprint-1/implement/mean.test.js b/Sprint-1/implement/mean.test.js index ff82a2112..388c0fa73 100644 --- a/Sprint-1/implement/mean.test.js +++ b/Sprint-1/implement/mean.test.js @@ -42,11 +42,11 @@ test("Given an array containing negative numbers, should return the correct mean }); test("Given an array containing decimal numbers, should return the correct mean", () => { - expect(calculateMean([2.2, 1.9, 3.5, 4.1])).toEqual(2.925); + expect(calculateMean([2.2, 1.9, 3.5, 4.1])).toBeCloseTo(2.925); }); test("Given an array containing both negative and decimal numbers, should return the correct mean", () => { - expect(calculateMean([2.2, -1.9, 3.5, -4.1])).toEqual(-0.07499999999999984); + expect(calculateMean([2.2, -1.9, 3.5, -4.1])).toBeCloseTo(-0.075); }); // Given an empty array @@ -84,3 +84,15 @@ test("Throws an error when given an array containing a non-number value", () => new Error("calculateMean requires an array of numbers") ); }); + +test("Throws an error when array contains Infinity or -Infinity", () => { + expect(() => calculateMean([Infinity, 0, -Infinity])).toThrow( + new Error("calculateMean requires an array of numbers") + ); +}); + +test("Throws an error when array contains NaN", () => { + expect(() => calculateMean([0, NaN])).toThrow( + new Error("calculateMean requires an array of numbers") + ); +}); diff --git a/Sprint-1/stretch/aoc-2018-day1/solution.js b/Sprint-1/stretch/aoc-2018-day1/solution.js index 410f18be8..e45f52fcd 100644 --- a/Sprint-1/stretch/aoc-2018-day1/solution.js +++ b/Sprint-1/stretch/aoc-2018-day1/solution.js @@ -2,6 +2,8 @@ import fs from "node:fs"; // Puzzle input +// trim() removes leading/trailing whitespace so split("\n") +// does not create empty lines that become 0 after Number() const changes = fs .readFileSync(new URL("./input.txt", import.meta.url), "utf8") .trim() @@ -12,7 +14,7 @@ const changes = fs function calculateFrequency(changes) { let frequency = 0; - for (let change of changes) { + for (const change of changes) { frequency += change; }