From 7f13c87e46e28ed158c6e7e1eacdc71a674d1a39 Mon Sep 17 00:00:00 2001 From: enjoy15 Date: Sun, 26 Oct 2025 09:03:26 +0000 Subject: [PATCH 1/4] fixed median.js --- 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..c67461d6c 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; + // Check if list is a valid array + if (!Array.isArray(list) || list.length === 0) { + return null; + } + + // Filter only numeric values (numbers, not NaN, not null, not undefined) + const numbers = list.filter(item => typeof item === 'number' && !isNaN(item)); + + // If no valid numbers found, return null + if (numbers.length === 0) { + return null; + } + + // Sort the numbers in ascending order (don't mutate original array) + const sorted = [...numbers].sort((a, b) => a - b); + + const middleIndex = Math.floor(sorted.length / 2); + + // If odd length, return the middle element + if (sorted.length % 2 === 1) { + return sorted[middleIndex]; + } + + // If even length, return the average of the two middle elements + return (sorted[middleIndex - 1] + sorted[middleIndex]) / 2; } module.exports = calculateMedian; From 7fca1ad866589f8026566bea54d94e32b6db2362 Mon Sep 17 00:00:00 2001 From: enjoy15 Date: Sun, 26 Oct 2025 09:48:45 +0000 Subject: [PATCH 2/4] update max.js --- Sprint-1/implement/max.js | 24 +++++++++++++++++++++++- Sprint-1/implement/max.test.js | 34 ++++++++++++++++++++++++++++++++-- 2 files changed, 55 insertions(+), 3 deletions(-) diff --git a/Sprint-1/implement/max.js b/Sprint-1/implement/max.js index 6dd76378e..364054559 100644 --- a/Sprint-1/implement/max.js +++ b/Sprint-1/implement/max.js @@ -1,4 +1,26 @@ function findMax(elements) { -} + // Filter only numeric values (numbers, not NaN, not null, not undefined) + const numbers = elements.filter(function(item) { + return typeof item === 'number' && !isNaN(item); + }); + + // Check if any numbers exist, Given an empty array, it should return -Infinity + if (numbers.length === 0) { + if (elements.length === 0) { + return -Infinity; + } else { + return null; + } + } + // Find the maximum number + let max = numbers[0]; + for (let i = 1; i < numbers.length; i++) { + if (numbers[i] > max) { + max = numbers[i]; + } +} +return max; +} + module.exports = findMax; diff --git a/Sprint-1/implement/max.test.js b/Sprint-1/implement/max.test.js index 82f18fd88..922a2b31f 100644 --- a/Sprint-1/implement/max.test.js +++ b/Sprint-1/implement/max.test.js @@ -15,29 +15,59 @@ 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([])).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, returns that number", () => { + expect(findMax([5])).toBe(5); + expect(findMax([42])).toBe(42); + expect(findMax([-10])).toBe(-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 positive and negative numbers, returns the largest", () => { + expect(findMax([30, 50, 10, 40])).toBe(50); + expect(findMax([10, -5, 20, -15, 8])).toBe(20); + expect(findMax([-3, 5, -10, 2])).toBe(5); +}); // 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, returns the closest to zero", () => { + expect(findMax([-5, -10, -3, -20])).toBe(-3); + expect(findMax([-100, -50, -1])).toBe(-1); +}); // 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", () => { + expect(findMax([3.5, 2.1, 4.8, 1.2])).toBe(4.8); + expect(findMax([0.1, 0.9, 0.5])).toBe(0.9); + expect(findMax([10.5, 10.7, 10.3])).toBe(10.7); +}); // 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-number values, ignores them and returns max", () => { + expect(findMax(["hey", 10, "hi", 60, 10])).toBe(60); + expect(findMax([5, "hello", 15, null, 10, undefined])).toBe(15); + expect(findMax([1, "test", 2, NaN, 3])).toBe(3); +}); // 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, returns null", () => { + expect(findMax(["hello", "world"])).toBe(null); + expect(findMax([null, undefined, NaN])).toBe(null); + expect(findMax(["a", "b", "c"])).toBe(null); +}); From 097ab825ca7959720de6eb47cf390b67d251b59c Mon Sep 17 00:00:00 2001 From: enjoy15 Date: Sun, 26 Oct 2025 10:08:04 +0000 Subject: [PATCH 3/4] Add dedupe function --- Sprint-1/implement/dedupe.js | 16 +++++++++++++++- Sprint-1/implement/dedupe.test.js | 15 ++++++++++++++- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/Sprint-1/implement/dedupe.js b/Sprint-1/implement/dedupe.js index 781e8718a..744be3fd2 100644 --- a/Sprint-1/implement/dedupe.js +++ b/Sprint-1/implement/dedupe.js @@ -1 +1,15 @@ -function dedupe() {} + +function dedupe(arr) { + if (!Array.isArray(arr)) return []; + const seen = new Set(); + const result = []; + for (const item of arr) { + if (!seen.has(item)) { + seen.add(item); + result.push(item); + } + } + return result; +} + +module.exports = dedupe; diff --git a/Sprint-1/implement/dedupe.test.js b/Sprint-1/implement/dedupe.test.js index 23e0f8638..d74c9f12f 100644 --- a/Sprint-1/implement/dedupe.test.js +++ b/Sprint-1/implement/dedupe.test.js @@ -13,15 +13,28 @@ E.g. dedupe([1, 2, 1]) target output: [1, 2] // Acceptance Criteria: + // 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, returns a copy of the original array", () => { + expect(dedupe([1, 2, 3])).toEqual([1, 2, 3]); + expect(dedupe(["a", "b", "c"])).toEqual(["a", "b", "c"]); +}); // 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 +test("given an array with duplicates, removes duplicates and preserves first occurrence", () => { + expect(dedupe(["a", "a", "a", "b", "b", "c"])).toEqual(["a", "b", "c"]); + expect(dedupe([5, 1, 1, 2, 3, 2, 5, 8])).toEqual([5, 1, 2, 3, 8]); + expect(dedupe([1, 2, 1])).toEqual([1, 2]); + expect(dedupe(["x", "y", "x", "z", "y", "x"])).toEqual(["x", "y", "z"]); +}); From 6366dffbf3c0405fe3777d1bf3b00c68dd852c02 Mon Sep 17 00:00:00 2001 From: enjoy15 Date: Sun, 26 Oct 2025 10:19:54 +0000 Subject: [PATCH 4/4] Add function includes.js --- 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..bb729c458 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; }