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; 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..9d7b6a64f 100644 --- a/Sprint-1/implement/dedupe.test.js +++ b/Sprint-1/implement/dedupe.test.js @@ -13,15 +13,35 @@ 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"]); + + // Reference copy check + const original = [7, 8, 9]; + const result = dedupe(original); + expect(result).toEqual(original); // same values + expect(result).not.toBe(original); // different reference + +}); // 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"]); +}); diff --git a/Sprint-1/implement/max.js b/Sprint-1/implement/max.js index 6dd76378e..4a6513bc8 100644 --- a/Sprint-1/implement/max.js +++ b/Sprint-1/implement/max.js @@ -1,4 +1,22 @@ 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); + }); + + // Treat no numbers the same as empty input + if (numbers.length === 0) { + return -Infinity; + } + // 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); +}); diff --git a/Sprint-1/implement/sum.js b/Sprint-1/implement/sum.js index 9062aafe3..e73c36f8e 100644 --- a/Sprint-1/implement/sum.js +++ b/Sprint-1/implement/sum.js @@ -1,4 +1,12 @@ -function sum(elements) { +function sum(list) { + if (!Array.isArray(list)) return 0; + let total = 0; + for (const item of list) { + if (typeof item === 'number' && !isNaN(item)) { + total += item; + } + } + return total; } module.exports = sum; diff --git a/Sprint-1/implement/sum.test.js b/Sprint-1/implement/sum.test.js index dd0a090ca..d5355d318 100644 --- a/Sprint-1/implement/sum.test.js +++ b/Sprint-1/implement/sum.test.js @@ -8,29 +8,44 @@ E.g. sum(['hey', 10, 'hi', 60, 10]), target output: 80 (ignore any non-numerical const sum = require("./sum.js"); -// Acceptance Criteria: - // 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); +}); // Given an array with just one number // When passed to the sum function // Then it should return that number +test("given an array with one number, returns that number", () => { + expect(sum([42])).toBe(42); +}); // Given an array containing negative numbers // When passed to the sum function // Then it should still return the correct total sum +test("sums arrays with negative numbers", () => { + expect(sum([10, -5, -15, 20])).toBe(10); +}); // Given an array with decimal/float numbers // When passed to the sum function // Then it should return the correct total sum +test("sums arrays with decimal numbers", () => { + expect(sum([1.5, 2.25, 3.25])).toBeCloseTo(7.0); +}); // 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("ignores non-number values", () => { + expect(sum(["a", 10, null, 5, "7", undefined, NaN, 3])).toBe(18); +}); // 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("array with only non-number values returns 0", () => { + expect(sum(["x", null, undefined, "y"])).toBe(0); +}); 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; }