diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index b22590bc6..7097cf84b 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -1,14 +1,21 @@ -// Fix this implementation -// Start by running the tests for this function -// If you're in the Sprint-1 directory, you can run `npm test -- fix` to run the tests in the fix directory +function calculateMedian(list) { + // Check if input is a valid array + if (!Array.isArray(list)) return null; -// Hint: Please consider scenarios when 'list' doesn't have numbers (the function is expected to return null) -// or 'list' has mixed values (the function is expected to sort only numbers). + // Filter out only numeric values + const nums = list.filter(val => typeof val === 'number' && !isNaN(val)); + if (nums.length === 0) return null; -function calculateMedian(list) { - const middleIndex = Math.floor(list.length / 2); - const median = list.splice(middleIndex, 1)[0]; - return median; + // Clone and sort the array numerically + const sorted = [...nums].sort((a, b) => a - b); + const mid = Math.floor(sorted.length / 2); + + // Return median based on even or odd length + if (sorted.length % 2 !== 0) { + return sorted[mid]; + } else { + return (sorted[mid - 1] + sorted[mid]) / 2; + } } module.exports = calculateMedian; diff --git a/Sprint-1/implement/dedupe.js b/Sprint-1/implement/dedupe.js index 781e8718a..bfedf4ed4 100644 --- a/Sprint-1/implement/dedupe.js +++ b/Sprint-1/implement/dedupe.js @@ -1 +1,17 @@ -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..b4935b8f0 100644 --- a/Sprint-1/implement/dedupe.test.js +++ b/Sprint-1/implement/dedupe.test.js @@ -16,12 +16,41 @@ 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.todo("given an empty array, it returns an empty array"); + test("returns an empty array when given 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 of numbers with no duplicates, it returns a copy of the original array", () => { + const input = [1, 2, 3]; + const result = dedupe(input); + expect(result).toEqual([1, 2, 3]); + expect(result).not.toBe(input); // Ensure it's a new array, not the same reference +}); + +test("given an array of strings with no duplicates, it returns a copy of the original array", () => { + const input = ["a", "b", "c"]; + const result = dedupe(input); + expect(result).toEqual(["a", "b", "c"]); + expect(result).not.toBe(input); +}); // 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("removes duplicate numbers, preserving the first occurrence", () => { + expect(dedupe([1, 2, 1])).toEqual([1, 2]); + expect(dedupe([5, 1, 1, 2, 3, 2, 5, 8])).toEqual([5, 1, 2, 3, 8]); +}); + +test("removes duplicate strings, preserving the first occurrence", () => { + expect(dedupe(["a", "a", "a", "b", "b", "c"])).toEqual(["a", "b", "c"]); + expect(dedupe(["x", "y", "x", "z", "y"])).toEqual(["x", "y", "z"]); +}); + +test("handles mixed types (strings and numbers)", () => { + expect(dedupe(["1", 1, "1", 2, 2, "2"])).toEqual(["1", 1, 2, "2"]); + expect(dedupe([1, "1", 1, "1"])).toEqual([1, "1"]); +}); diff --git a/Sprint-1/implement/max.js b/Sprint-1/implement/max.js index 6dd76378e..65d9662b8 100644 --- a/Sprint-1/implement/max.js +++ b/Sprint-1/implement/max.js @@ -1,4 +1,11 @@ + + function findMax(elements) { + if (!Array.isArray(elements)) return -Infinity; + + const nums = elements.filter(val => typeof val === 'number' && !isNaN(val)); + + return nums.length > 0 ? Math.max(...nums) : -Infinity; } module.exports = findMax; diff --git a/Sprint-1/implement/max.test.js b/Sprint-1/implement/max.test.js index 82f18fd88..6aa7a184a 100644 --- a/Sprint-1/implement/max.test.js +++ b/Sprint-1/implement/max.test.js @@ -16,28 +16,48 @@ 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.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([42])).toBe(42); +}); // 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([-10, 5, 20, -3])).toBe(20); +}); // 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 only negative numbers, returns the closest to zero", () => { + expect(findMax([-100, -20, -3, -50])).toBe(-3); +}); // 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([1.2, 3.5, 2.9, 3.6])).toBe(3.6); +}); // 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, returns the max of valid numbers", () => { + expect(findMax(["a", 10, "b", 20, null, undefined, NaN, 5])).toBe(20); + expect(findMax([false, 3, true, 7, "hello"])).toBe(7); +}); // 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 -Infinity", () => { + expect(findMax(["a", null, undefined, NaN, {}, [], "b"])).toBe(-Infinity); + expect(findMax([false, true, "zero", "1", {}, []])).toBe(-Infinity); +}); \ No newline at end of file diff --git a/Sprint-1/implement/sum.js b/Sprint-1/implement/sum.js index 9062aafe3..47ba2ea1d 100644 --- a/Sprint-1/implement/sum.js +++ b/Sprint-1/implement/sum.js @@ -1,4 +1,9 @@ function sum(elements) { + if (!Array.isArray(elements)) return 0; + + return elements.reduce((acc, val) => { + return typeof val === "number" && !isNaN(val) ? acc + val : acc; + }, 0); } -module.exports = sum; +module.exports = sum; \ No newline at end of file diff --git a/Sprint-1/implement/sum.test.js b/Sprint-1/implement/sum.test.js index dd0a090ca..d346480b9 100644 --- a/Sprint-1/implement/sum.test.js +++ b/Sprint-1/implement/sum.test.js @@ -13,24 +13,39 @@ 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.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 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 array with negative numbers", () => { + expect(sum([-1, -2, 3, 4])).toBe(4); + }); // Given an array with decimal/float numbers // When passed to the sum function // Then it should return the correct total sum + test("sums array with decimals", () => { + expect(sum([1.5, 2.5, 3])).toBe(7); + }); // 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(["hello", 10, null, 20, undefined, 5])).toBe(35); + }); // 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("returns 0 for array with only non-number values", () => { + expect(sum(["hello", null, undefined, {}, [], "42"])).toBe(0); +}); \ No newline at end of file diff --git a/Sprint-1/refactor/includes.js b/Sprint-1/refactor/includes.js index 29dad81f0..7010de4e9 100644 --- a/Sprint-1/refactor/includes.js +++ b/Sprint-1/refactor/includes.js @@ -1,8 +1,5 @@ -// 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; }