diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index b22590bc6..eb62ed801 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -6,9 +6,22 @@ // 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; + // Return null for non arrays + if (!Array.isArray(list)) return null; + // Filter to only numeric values + const nums = list.filter((x) => typeof x === "number" && isFinite(x)); + // Return null if no valid numbers remain + if (nums.length === 0) return null; + // Sort the numbers in ascending order + // filter already returns a new array so need to spread before spreading + const sorted = nums.sort((a, b) => a - b); + // Find the middle index of the sorted array + const middleIndex = Math.floor(sorted.length / 2); + // If odd length, return the middle index + // If even length, return the average of the two middle elements + return sorted.length % 2 !== 0 + ? sorted[middleIndex] + : (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..f152f8185 100644 --- a/Sprint-1/implement/dedupe.js +++ b/Sprint-1/implement/dedupe.js @@ -1 +1,9 @@ -function dedupe() {} +function dedupe(arr) { + // Return null if the input is not an array + if (!Array.isArray(arr)) return null; + // Use Set to remove duplicates, + // then spread back into a new array + return [...new Set(arr)]; +} + +module.exports = dedupe; diff --git a/Sprint-1/implement/dedupe.test.js b/Sprint-1/implement/dedupe.test.js index 23e0f8638..6f5ef6565 100644 --- a/Sprint-1/implement/dedupe.test.js +++ b/Sprint-1/implement/dedupe.test.js @@ -16,12 +16,27 @@ 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("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, it returns a copy of the original array", () => { + const input = [1, 2, 3]; + const result = dedupe(input); + // Check the contents match a hardcoded expected value, not the input itself + expect(result).toEqual([1, 2, 3]); + // Check it's a different array, not the same reference + 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("given an array with duplicate numbers or strings, it removes duplicates preserving first occurence", () => { + expect(dedupe([5, 1, 1, 2, 3, 2, 5, 8])).toEqual([5, 1, 2, 3, 8]); + expect(dedupe(["a", "a", "a", "b", "b", "c"])).toEqual(["a", "b", "c"]); + expect(dedupe([1, 2, 1])).toEqual([1, 2]); +}); diff --git a/Sprint-1/implement/max.js b/Sprint-1/implement/max.js index 6dd76378e..6e1ccee50 100644 --- a/Sprint-1/implement/max.js +++ b/Sprint-1/implement/max.js @@ -1,4 +1,8 @@ function findMax(elements) { + // Filter to only numeric values + const nums = elements.filter((x) => typeof x === "number" && isFinite(x)); + // Math.max with no arguments returns -Infinity, which handles empty arrays as well + return Math.max(...nums); } module.exports = findMax; diff --git a/Sprint-1/implement/max.test.js b/Sprint-1/implement/max.test.js index 82f18fd88..f5b52ee9c 100644 --- a/Sprint-1/implement/max.test.js +++ b/Sprint-1/implement/max.test.js @@ -16,28 +16,51 @@ 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 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, returns that number", () => { + expect(findMax([42])).toEqual(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([30, 50, 10, 40])).toEqual(50); + expect(findMax([-10, 5, 3, -2])).toEqual(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 only negative numbers, returns the closest to zero", () => { + expect(findMax([-10, -3, -7])).toEqual(-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, return the largest decimal number", () => { + expect(findMax([1.5, 2.7, 0.3])).toEqual(2.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-numeric values, returns the max and ignores non-numerics", () => { + expect(findMax(["hey", 10, "hi", 60, 10])).toEqual(60); + // "300" looks numeric but is a string, so it should be ignored + expect(findMax([10, "300", 20])).toEqual(20); +}); // 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-numeric values, returns -Infinity", () => { + expect(findMax(["hey", "hi", null, undefined])).toEqual(-Infinity); +}); diff --git a/Sprint-1/implement/sum.js b/Sprint-1/implement/sum.js index 9062aafe3..3fa5cedf9 100644 --- a/Sprint-1/implement/sum.js +++ b/Sprint-1/implement/sum.js @@ -1,4 +1,9 @@ function sum(elements) { + // Filter to only numeric values, + // then reduce to a total + return elements + .filter((x) => typeof x === "number" && isFinite(x)) + .reduce((total, x) => total + x, 0); } module.exports = sum; diff --git a/Sprint-1/implement/sum.test.js b/Sprint-1/implement/sum.test.js index dd0a090ca..436f4b3c4 100644 --- a/Sprint-1/implement/sum.test.js +++ b/Sprint-1/implement/sum.test.js @@ -13,24 +13,44 @@ 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([])).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 one number, returns that number", () => { + expect(sum([42])).toEqual(42); +}); // 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 with negative numbers, returns the correct sum", () => { + expect(sum([-10, -20, -30])).toEqual(-60); + expect(sum([-5, -3, -2])).toEqual(-10); +}); // 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 numbers, returns the correct sum", () => { + expect(sum([1.5, 2.5, 3.0])).toEqual(7); + // toBeCloseTo is used for floating point calculations to avoid precision issues + expect(sum([1.2, 0.6, 0.005])).toBeCloseTo(1.805); +}); // 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("given an array with non-numeric values, ignores them and returns the sum", () => { + expect(sum(["hey", 10, "hi", 60, 10])).toEqual(80); +}); // 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("given an array with only non-numeric values, returns 0", () => { + expect(sum(["hey", "hi", null, undefined])).toEqual(0); +}); diff --git a/Sprint-1/refactor/includes.js b/Sprint-1/refactor/includes.js index 29dad81f0..2e60f61a6 100644 --- a/Sprint-1/refactor/includes.js +++ b/Sprint-1/refactor/includes.js @@ -1,12 +1,14 @@ // 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]; + // Iterate directly over each element in the list + for (const element of list) { + // Return true as soon as we find a match if (element === target) { return true; } } + // Target was not found in the list return false; } diff --git a/Sprint-1/refactor/includes.test.js b/Sprint-1/refactor/includes.test.js index 812158470..3d6babe53 100644 --- a/Sprint-1/refactor/includes.test.js +++ b/Sprint-1/refactor/includes.test.js @@ -2,6 +2,9 @@ const includes = require("./includes.js"); +// Given an array containing the target value +// When passed to the includes function +// Then it should return true test("returns true when target is in array", () => { const currentOutput = includes(["a", "b", "c", "d"], "c"); const targetOutput = true; @@ -9,6 +12,9 @@ test("returns true when target is in array", () => { expect(currentOutput).toEqual(targetOutput); }); +// Given an array that does not contain the target value +// When passed to the includes function +// Then it should return false test("returns false when target not in array", () => { const currentOutput = includes([1, 2, 3, 4], "a"); const targetOutput = false; @@ -16,6 +22,9 @@ test("returns false when target not in array", () => { expect(currentOutput).toEqual(targetOutput); }); +// Given an array where the target appears more than once +// When passed to the includes function +// Then it should still return true test("returns true when the target is in array multiple times", () => { const currentOutput = includes([1, 2, 2, 3], 2); const targetOutput = true; @@ -23,6 +32,9 @@ test("returns true when the target is in array multiple times", () => { expect(currentOutput).toEqual(targetOutput); }); +// Given an empty array +// When passed to the includes function +// Then it should return false test("returns false for empty array", () => { const currentOutput = includes([]); const targetOutput = false; @@ -30,6 +42,9 @@ test("returns false for empty array", () => { expect(currentOutput).toEqual(targetOutput); }); +// Given an array containing null as an element +// When passed to the includes function with null as the target +// Then it should return true test("searches for null", () => { const currentOutput = includes(["b", "z", null, "a"], null); const targetOutput = true;