diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index 5c5b796e1..bdf5b07f3 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -6,9 +6,28 @@ // 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; + if (!Array.isArray(list)) { + throw new Error("calculateMedian requires an array of numbers"); + } + + if (list.length === 0) { + throw new Error("calculateMedian requires a non-empty array"); + } + + for (const item of list) { + if (typeof item !== "number") { + throw new Error("calculateMedian requires an array of numbers"); + } + } + + const sortedList = [...list].sort((a, b) => a - b); + const middleIndex = Math.floor(sortedList.length / 2); + + if (sortedList.length % 2 === 1) { + return sortedList[middleIndex]; + } + + return (sortedList[middleIndex - 1] + sortedList[middleIndex]) / 2; } module.exports = calculateMedian; diff --git a/Sprint-1/implement/dedupe.js b/Sprint-1/implement/dedupe.js index 781e8718a..935d8a708 100644 --- a/Sprint-1/implement/dedupe.js +++ b/Sprint-1/implement/dedupe.js @@ -1 +1,5 @@ -function dedupe() {} +function dedupe(values) { + return [...new Set(values)]; +} + +module.exports = dedupe; diff --git a/Sprint-1/implement/dedupe.test.js b/Sprint-1/implement/dedupe.test.js index d7c8e3d8e..83157f903 100644 --- a/Sprint-1/implement/dedupe.test.js +++ b/Sprint-1/implement/dedupe.test.js @@ -16,13 +16,37 @@ E.g. dedupe([1, 2, 1]) returns [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"); +it("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 +it("returns a copy of an array with no duplicates", () => { + const values = [1, "a", 2, "b"]; + const result = dedupe(values); + + expect(result).toEqual(values); + expect(result).not.toBe(values); +}); // 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 +// Then it should return a new array with duplicates removed while preserving the // first occurrence of each element from the original array. +it("removes duplicate numbers while preserving their first occurrences", () => { + const values = [5, 1, 1, 2, 3, 2, 5, 8]; + const result = dedupe(values); + + expect(result).toEqual([5, 1, 2, 3, 8]); + expect(result).not.toBe(values); +}); + +it("removes duplicate strings while preserving their first occurrences", () => { + const values = ["a", "b", "a", "c", "b"]; + const result = dedupe(values); + + expect(result).toEqual(["a", "b", "c"]); + expect(result).not.toBe(values); +}); diff --git a/Sprint-1/implement/describe-median.js b/Sprint-1/implement/describe-median.js index 3381cfc0a..8b5c9d237 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..ec087ae2b 100644 --- a/Sprint-1/implement/describe-median.test.js +++ b/Sprint-1/implement/describe-median.test.js @@ -20,13 +20,32 @@ const describeMedian = require("./describe-median.js"); // Given an array of numbers // When passed to describeMedian // Then it should return "The median is " followed by the median -// Delete this test.todo and replace it with a test. -test.todo('given [1, 2, 3], returns "The median is 2"'); +it("returns the median for [1, 2, 3]", () => { + 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("returns an error description when given an empty array", () => { + try { + expect(describeMedian([])).toBe( + "Could not calculate a median: calculateMedian requires a non-empty array" + ); + } catch (error) { + throw new Error(`Empty-array case failed: ${error.message}`); + } +}); // 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("returns an error description for an array of strings", () => { + try { + expect(describeMedian(["banana", "apple", "orange"])).toBe( + "Could not calculate a median: calculateMedian requires an array of numbers" + ); + } catch (error) { + throw new Error(`String-array case failed: ${error.message}`); + } +}); diff --git a/Sprint-1/implement/max.js b/Sprint-1/implement/max.js index 6dd76378e..c223d65bf 100644 --- a/Sprint-1/implement/max.js +++ b/Sprint-1/implement/max.js @@ -1,4 +1,14 @@ function findMax(elements) { + if ( + !Array.isArray(elements) || + elements.some((element) => typeof element !== "number") + ) { + throw new Error("findMax requires an array of numbers"); + } + return elements.reduce( + (max, element) => (element > max ? element : max), + -Infinity + ); } module.exports = findMax; diff --git a/Sprint-1/implement/max.test.js b/Sprint-1/implement/max.test.js index ff3fbb5e5..d35f25cff 100644 --- a/Sprint-1/implement/max.test.js +++ b/Sprint-1/implement/max.test.js @@ -15,29 +15,71 @@ 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.todo("given an empty array, returns -Infinity"); +it("returns -Infinity for an empty array", () => { + expect(findMax([])).toBe(-Infinity); +}); // Given an array with one number // When passed to the max function // Then it should return that number +it("returns 10 for a single positive number", () => { + expect(findMax([10])).toBe(10); +}); + +it("returns the largest value in a positive-number array", () => { + expect(findMax([10, 20, 5])).toBe(20); +}); // Given an array with both positive and negative numbers // When passed to the max function // Then it should return the largest number overall +it("returns 200 for an array of positive and negative numbers", () => { + expect(findMax([10, -10, 200, -15])).toEqual(200); +}); + +it("returns Infinity when Infinity is the largest value", () => { + expect(findMax([10, Infinity, -15])).toBe(Infinity); +}); // Given an array with just negative numbers // When passed to the max function // Then it should return the closest one to zero +it("returns -1 for an array of negative numbers", () => { + expect(findMax([-1, -10, -200, -15, -30])).toEqual(-1); +}); + +it("returns the number for a single negative value", () => { + expect(findMax([-10])).toBe(-10); +}); // Given an array with decimal numbers // When passed to the max function // Then it should return the largest decimal number +it("returns 200.99 for an array of decimal numbers", () => { + expect(findMax([1.5, 2.5, 200.99, 15.01, 30.1])).toBe(200.99); +}); // 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("throws for an array of strings", () => { + expect(() => findMax(["Hello", "There", "Weekend"])).toThrow( + "findMax requires an array of numbers" + ); +}); + +it("throws for an array mixing strings and numbers", () => { + expect(() => findMax(["Hello", 10, 20])).toThrow( + "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("throws when the input is not an array", () => { + expect(() => findMax("Hello")).toThrow( + "findMax requires an array of numbers" + ); +}); diff --git a/Sprint-1/implement/mean.js b/Sprint-1/implement/mean.js index 36909ea1f..fc69f13c6 100644 --- a/Sprint-1/implement/mean.js +++ b/Sprint-1/implement/mean.js @@ -1,3 +1,21 @@ -function calculateMean(list) {} +function calculateMean(list) { + if (!Array.isArray(list)) { + throw new Error("calculateMean requires an array of numbers"); + } + + if (list.length === 0) { + throw new Error("calculateMean requires a non-empty array"); + } + + for (const item of list) { + if (typeof item !== "number") { + throw new Error("calculateMean requires an array of numbers"); + } + } + + const sum = list.reduce((a, b) => a + b, 0); + + return sum / list.length; +} module.exports = calculateMean; diff --git a/Sprint-1/implement/mean.test.js b/Sprint-1/implement/mean.test.js index 847b88676..247a26ec0 100644 --- a/Sprint-1/implement/mean.test.js +++ b/Sprint-1/implement/mean.test.js @@ -22,29 +22,56 @@ const calculateMean = require("./mean.js"); // 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"); +it("returns the mean in a positive-number array", () => { + expect(calculateMean([1, 2, 6])).toBe(3); +}); // Given an array with a single number // When passed to calculateMean // Then it should return that number +it("returns 10 for a single positive number", () => { + expect(calculateMean([10])).toBe(10); +}); // Given an array containing negative or decimal numbers // When passed to calculateMean // Then it should return the correct mean +it("returns -3 for an array of negative numbers", () => { + expect(calculateMean([-1, -2, -6])).toEqual(-3); +}); // Given an empty array // When passed to calculateMean // Then it should throw Error("calculateMean requires a non-empty array") +it("throws for an empty array", () => { + expect(() => calculateMean([])).toThrow( + "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("throws when the input is not an array", () => { + expect(() => calculateMean("Hello")).toThrow( + "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("throws when the input has no argument", () => { + expect(() => calculateMean("")).toThrow( + "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("throws for an array containing a non-number value", () => { + expect(() => calculateMean(["2", 10, 20])).toThrow( + "calculateMean requires an array of numbers" + ); +}); diff --git a/Sprint-1/implement/sum.js b/Sprint-1/implement/sum.js index 9062aafe3..839065de3 100644 --- a/Sprint-1/implement/sum.js +++ b/Sprint-1/implement/sum.js @@ -1,4 +1,13 @@ function sum(elements) { + if (!Array.isArray(elements)) { + throw new Error("sum requires an array of numbers"); + } + + if (elements.some((element) => typeof element !== "number")) { + throw new Error("sum requires an array of numbers"); + } + + return elements.reduce((total, element) => total + element, 0); } module.exports = sum; diff --git a/Sprint-1/implement/sum.test.js b/Sprint-1/implement/sum.test.js index a288e0f0a..dc940172e 100644 --- a/Sprint-1/implement/sum.test.js +++ b/Sprint-1/implement/sum.test.js @@ -13,24 +13,41 @@ 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") +it("returns 0 for an empty array", () => { + expect(sum([])).toBe(0); +}); // Given an array with just one number // When passed to the sum function // Then it should return that number +it("returns 10 for a single positive number", () => { + expect(sum([10])).toBe(10); +}); // Given an array containing negative numbers // When passed to the sum function // Then it should still return the correct total sum +it("returns -9 for an array of negative numbers", () => { + expect(sum([-1, -2, -6])).toEqual(-9); +}); // Given an array with decimal/float numbers // When passed to the sum function // Then it should return the correct total sum +it("returns 220 for an array of decimal numbers", () => { + expect(sum([1.5, 2.5, 200.99, 15.01])).toBe(220); +}); // 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("throws when the input is not an array", () => { + expect(() => sum("Hello")).toThrow("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("throws when the input is not an array", () => { + expect(() => sum("Hey", 42, "")).toThrow("sum requires an array of numbers"); +}); diff --git a/Sprint-1/refactor/includes.js b/Sprint-1/refactor/includes.js index 29dad81f0..8c9ae2e66 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 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; }