diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index 5c5b796e1..2f00b07ba 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -2,13 +2,39 @@ // 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 + + // Hint: Please consider scenarios when 'list' isn't an array, is empty, // 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; + // 1. Check if 'list' is a valid array and not null/undefined + if (!Array.isArray(list)) { + throw new Error("calculateMedian requires an array of numbers"); + } + + // 2. Check if the array is empty + if (list.length === 0) { + throw new Error("calculateMedian requires a non-empty array"); + } + + // 3. Check if every item in the array is a valid number (no strings, null, undefined, etc.) + for (const item of list) { + if (typeof item !== "number" || Number.isNaN(item)) { + throw new Error("calculateMedian requires an array of numbers"); + } + } + + // 4. Create a sorted copy of the array so we don't mutate the original input + const sorted = [...list].sort((a, b) => a - b); + const mid = Math.floor(sorted.length / 2); + + // 5. Calculate median for odd vs even length arrays + if (sorted.length % 2 !== 0) { + return sorted[mid]; + } else { + return (sorted[mid - 1] + sorted[mid]) / 2; + } } -module.exports = calculateMedian; +module.exports = calculateMedian; \ No newline at end of file diff --git a/Sprint-1/implement/dedupe.js b/Sprint-1/implement/dedupe.js index 781e8718a..20f2603e0 100644 --- a/Sprint-1/implement/dedupe.js +++ b/Sprint-1/implement/dedupe.js @@ -1 +1,4 @@ -function dedupe() {} +function dedupe(arr) { + return [... new Set(arr)] +} +module.exports = dedupe; diff --git a/Sprint-1/implement/dedupe.test.js b/Sprint-1/implement/dedupe.test.js index d7c8e3d8e..73fd8841e 100644 --- a/Sprint-1/implement/dedupe.test.js +++ b/Sprint-1/implement/dedupe.test.js @@ -18,11 +18,33 @@ E.g. dedupe([1, 2, 1]) returns [1, 2] // Then it should return an empty array test.todo("given an empty array, it returns an empty array"); +it("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.todo("given an array, it returns a copy of the original array"); + +it("given an array, 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); +}); // 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 // first occurrence of each element from the original array. + +test.todo("given an array, it returns a duplicate while preserving the first occurrence of each element from the original array"); + +it("given an array, it removes duplicates while preserving the first occurrence of elements", () => { +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]); +}); diff --git a/Sprint-1/implement/describe-median.js b/Sprint-1/implement/describe-median.js index 3381cfc0a..23764c40a 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..abdce10b8 100644 --- a/Sprint-1/implement/describe-median.test.js +++ b/Sprint-1/implement/describe-median.test.js @@ -23,10 +23,27 @@ const describeMedian = require("./describe-median.js"); // Delete this test.todo and replace it with a test. test.todo('given [1, 2, 3], returns "The median is 2"'); -// Given an empty array -// When passed to describeMedian -// Then it should return "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" +describe("describeMedian", () => { + it("given [1, 2, 3], returns {The median is 2}", () => { + expect(describeMedian([1, 2, 3])).toEqual("The median is 2"); + }); + + // Given an empty array + // When passed to describeMedian + // Then it should return "Could not calculate a median: calculateMedian requires a non-empty array" + + it("given an empty array, returns the correct error 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" + it("given something that isn't an array, returns the correct error message", () => { + expect(describeMedian("banana")).toEqual( + "Could not calculate a median: calculateMedian requires an array of numbers" + ); + }); +}); diff --git a/Sprint-1/implement/max.js b/Sprint-1/implement/max.js index 6dd76378e..ea0b63558 100644 --- a/Sprint-1/implement/max.js +++ b/Sprint-1/implement/max.js @@ -1,4 +1,23 @@ function findMax(elements) { + // 1. Check if input is a valid array and not null/undefined + if (!Array.isArray(elements)) { + throw new Error("findMax requires an array of numbers"); + } + + // 2. Handle empty array edge case per requirements + if (elements.length === 0) { + return -Infinity; + } + + // 3. Validate that every item in the array is strictly a number (no strings, null, etc.) + for (const item of elements) { + if (typeof item !== "number" || Number.isNaN(item)) { + throw new Error("findMax requires an array of numbers"); + } + } + + // 4. Return the maximum number using Math.max + return Math.max(...elements); } module.exports = findMax; diff --git a/Sprint-1/implement/max.test.js b/Sprint-1/implement/max.test.js index ff3fbb5e5..8b136e896 100644 --- a/Sprint-1/implement/max.test.js +++ b/Sprint-1/implement/max.test.js @@ -16,28 +16,61 @@ 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"); +describe("findMax", () => { + it("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 + // Given an array with one number + // When passed to the max function + // Then it should return that number -// Given an array with both positive and negative numbers -// When passed to the max function -// Then it should return the largest number overall + it("given an array with one number, returns that number", () => { + expect(findMax([42])).toEqual(42); + }); -// Given an array with just negative numbers -// When passed to the max function -// Then it should return the closest one to zero + // Given an array with both positive and negative numbers + // When passed to the max function + // Then it should return the largest number overall + it("given an array with both positive and negative numbers, returns the largest number overall", () => { + expect(findMax([30, -50, 10, 40, -5])).toEqual(40); + }); -// Given an array with decimal numbers -// When passed to the max function -// Then it should return the largest decimal number + // Given an array with just negative numbers + // When passed to the max function + // Then it should return the closest one to zero + it("given an array with just negative numbers, returns the closest one to zero", () => { + expect(findMax([-10, -3, -50, -5])).toEqual(-3); + }); -// 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") + // Given an array with decimal numbers + // When passed to the max function + // Then it should return the largest decimal number + it("given an array with decimal numbers, returns the largest decimal number", () => { + expect(findMax([1.5, 2.7, 2.1, 0.4])).toEqual(2.7); + }); -// 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") + // 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") + it("given an array containing a value that isn't a number, throws an error", () => { + expect(() => findMax(["hey", 10, "hi", 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") + it("given something that isn't an array at all, throws an error", () => { + expect(() => findMax("hey")).toThrow( + new Error("findMax requires an array of numbers") + ); + expect(() => findMax(42)).toThrow( + new Error("findMax requires an array of numbers") + ); + expect(() => findMax()).toThrow( + new Error("findMax requires an array of numbers") + ); + }); +}); diff --git a/Sprint-1/implement/mean.js b/Sprint-1/implement/mean.js index 36909ea1f..b7ac32e55 100644 --- a/Sprint-1/implement/mean.js +++ b/Sprint-1/implement/mean.js @@ -1,3 +1,25 @@ -function calculateMean(list) {} +function calculateMean(list) { + // 1. Check if input is a valid array + if (!Array.isArray(list)) { + throw new Error("calculateMean requires an array of numbers"); + } -module.exports = calculateMean; + // 2. Check if the array is empty + if (list.length === 0) { + throw new Error("calculateMean requires a non-empty array"); + } + + // 3. Validate that every item in the array is strictly a number + let sum = 0; + for (const item of list) { + if (typeof item !== "number" || Number.isNaN(item)) { + throw new Error("calculateMean requires an array of numbers"); + } + sum += item; + } + + // 4. Return the calculated mean + return sum / list.length; +} + +module.exports = calculateMean; \ No newline at end of file diff --git a/Sprint-1/implement/mean.test.js b/Sprint-1/implement/mean.test.js index 847b88676..8fd9b4bff 100644 --- a/Sprint-1/implement/mean.test.js +++ b/Sprint-1/implement/mean.test.js @@ -19,32 +19,79 @@ const calculateMean = require("./mean.js"); // Acceptance criteria: -// 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"); +describe("calculateMean", () => { + // Given an array of numbers + // When passed to calculateMean + // Then it should return their mean -// Given an array with a single number -// When passed to calculateMean -// Then it should return that number + it("given [1, 2, 6], returns 3", () => { + expect(calculateMean([1, 2, 6])).toEqual(3); + }); -// Given an array containing negative or decimal numbers -// When passed to calculateMean -// Then it should return the correct mean + // Given an array with a single number + // When passed to calculateMean + // Then it should return that number -// Given an empty array -// When passed to calculateMean -// Then it should throw Error("calculateMean requires a non-empty array") + it("given an array with a single number, returns that number", () => { + expect(calculateMean([10])).toEqual(10); + }); -// 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") + // Given an array containing negative or decimal numbers + // When passed to calculateMean + // Then it should return the correct mean -// Given no argument at all -// When passed to calculateMean -// Then it should throw Error("calculateMean requires an array of numbers") + it("given an array containing negative or decimal numbers, returns the correct mean", () => { + expect(calculateMean([6, -2, 8])).toEqual(4); // 6 + (-2) + 8 = 12 / 3 = 4 + expect(calculateMean([1.5, 2.5])).toEqual(2); + }); -// 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") + // Given an empty array + // When passed to calculateMean + // Then it should throw Error("calculateMean requires a non-empty array") + + it("given an empty array, throws the correct error", () => { + 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") + + it("given a value that isn't an array (string, number, null, object), throws the correct error", () => { + expect(() => calculateMean("banana")).toThrow( + new Error("calculateMean requires an array of numbers") + ); + expect(() => calculateMean(42)).toThrow( + new Error("calculateMean requires an array of numbers") + ); + expect(() => calculateMean(null)).toThrow( + new Error("calculateMean requires an array of numbers") + ); + expect(() => calculateMean({})).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") + + it("given no argument at all, throws the correct error", () => { + 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") + + it("given an array containing a non-number value, throws the correct error", () => { + expect(() => calculateMean([1, "2", 3])).toThrow( + new Error("calculateMean requires an array of numbers") + ); + }); +}); diff --git a/Sprint-1/implement/sum.js b/Sprint-1/implement/sum.js index 9062aafe3..b263048d7 100644 --- a/Sprint-1/implement/sum.js +++ b/Sprint-1/implement/sum.js @@ -1,4 +1,25 @@ function sum(elements) { + // 1. Check if input is a valid array + if (!Array.isArray(elements)) { + throw new Error("sum requires an array of numbers"); + } + + // 2. Handle empty array edge case (sum of empty array is 0) + if (elements.length === 0) { + return 0; + } + + // 3. Validate that every item in the array is strictly a number + let total = 0; + for (const item of elements) { + if (typeof item !== "number" || Number.isNaN(item)) { + throw new Error("sum requires an array of numbers"); + } + total += item; + } + + // 4. Return the calculated sum + return total; } -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 a288e0f0a..6725eac57 100644 --- a/Sprint-1/implement/sum.test.js +++ b/Sprint-1/implement/sum.test.js @@ -10,27 +10,73 @@ const sum = require("./sum.js"); // Acceptance Criteria: + +describe("sum", () => { // Given an empty array // When passed to the sum function // Then it should return 0 -test.todo("given an empty array, returns 0") + +it("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 +it("given an array with just 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 +it("given an array containing negative numbers, returns the correct total sum", () => { + expect(sum([10, -20, 30])).toEqual(20); + }); + // Given an array with decimal/float numbers // When passed to the sum function // Then it should return the correct total sum -// Given an array containing a value that isn't a number +it("given an array with decimal/float numbers, returns the correct total sum", () => { + expect(sum([1.5, 2.5, 3.0])).toEqual(7); + }); + + // 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") +it("given an array containing a value that isn't a number, throws an error", () => { + expect(() => sum(["hey", 10, "hi", 60, 10])).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") + +it("given something that isn't an array at all, throws an error", () => { + expect(() => sum("hey")).toThrow( + new Error("sum requires an array of numbers") + ); + expect(() => sum(42)).toThrow( + new Error("sum requires an array of numbers") + ); + expect(() => sum()).toThrow( + new Error("sum requires an array of numbers") + ); +}) +}) + + + + + + + + + + diff --git a/Sprint-1/refactor/includes.js b/Sprint-1/refactor/includes.js index 29dad81f0..8474eb211 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 +// Refactored implementation of includes using 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; } @@ -10,4 +9,4 @@ function includes(list, target) { return false; } -module.exports = includes; +module.exports = includes; \ No newline at end of file