diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index 5c5b796e1..4cbaa1d2d 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -6,8 +6,27 @@ // or contains values that aren't numbers (the function is expected to throw - see the tests). function calculateMedian(list) { + 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"); + } const middleIndex = Math.floor(list.length / 2); - const median = list.splice(middleIndex, 1)[0]; + let median; + const copyList = list.slice(); + copyList.sort((a, b) => a - b); + + for (const elements of copyList) { + if (typeof elements !== "number") { + throw new Error("calculateMedian requires an array of numbers"); + } + } + if (copyList.length % 2 === 0) { + median = (copyList[middleIndex] + copyList[middleIndex - 1]) / 2; + } else { + median = copyList[middleIndex]; + } return median; } diff --git a/Sprint-1/implement/dedupe.js b/Sprint-1/implement/dedupe.js index 781e8718a..0af9cf74b 100644 --- a/Sprint-1/implement/dedupe.js +++ b/Sprint-1/implement/dedupe.js @@ -1 +1,14 @@ -function dedupe() {} +function dedupe(elements) { + if (elements.length === 0) { + return []; + } + let newArray = []; + for (const items of elements) { + if (!newArray.includes(items)) { + newArray.push(items); + } + } + return newArray; +} + +module.exports = dedupe; diff --git a/Sprint-1/implement/dedupe.test.js b/Sprint-1/implement/dedupe.test.js index d7c8e3d8e..47c833a08 100644 --- a/Sprint-1/implement/dedupe.test.js +++ b/Sprint-1/implement/dedupe.test.js @@ -16,13 +16,26 @@ 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"); +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 copy of original array", () => { + expect(dedupe([1, 2, 3, 4])).toEqual([1, 2, 3, 4]); +}); // 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. +test("given an array with duplicates of strings and numbers it returns first occurrence of each element", () => { + expect(dedupe([1, "ball", "ball", 3, 1, "egg"])).toEqual([ + 1, + "ball", + 3, + "egg", + ]); +}); diff --git a/Sprint-1/implement/describe-median.js b/Sprint-1/implement/describe-median.js index 3381cfc0a..13c77d4ce 100644 --- a/Sprint-1/implement/describe-median.js +++ b/Sprint-1/implement/describe-median.js @@ -20,6 +20,18 @@ 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) { + if (error.message === "calculateMedian requires a non-empty array") { + return `Could not calculate a median: ${error.message}`; + } + if (error.message === "calculateMedian requires an array of numbers") { + 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..4f2b9ed62 100644 --- a/Sprint-1/implement/describe-median.test.js +++ b/Sprint-1/implement/describe-median.test.js @@ -21,12 +21,24 @@ const describeMedian = require("./describe-median.js"); // 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"'); +test('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" +test('given [], returns "Could not calculate a median: calculateMedian requires a non-empty array"', () => { + 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" +test('given not an array, returns "Could not calculate a median: calculateMedian requires an array of numbers"', () => { + 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..c143005c5 100644 --- a/Sprint-1/implement/max.js +++ b/Sprint-1/implement/max.js @@ -1,4 +1,25 @@ function findMax(elements) { + if (!Array.isArray(elements)) { + throw new Error("findMax requires an array of numbers"); + } + for (const items of elements) { + if (typeof items !== "number") { + throw new Error("findMax requires an array of numbers"); + } + } + if (elements.length === 0) { + return -Infinity; + } + if (elements.length === 1) { + return elements[0]; + } + let max = elements[0]; + for (const items of elements) { + if (items > max) { + max = items; + } + } + return max; } module.exports = findMax; diff --git a/Sprint-1/implement/max.test.js b/Sprint-1/implement/max.test.js index ff3fbb5e5..5984fd905 100644 --- a/Sprint-1/implement/max.test.js +++ b/Sprint-1/implement/max.test.js @@ -16,28 +16,52 @@ 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 one element number in an array, returns that number", () => { + expect(findMax([23])).toEqual(23); +}); // Given an array with both positive and negative numbers // When passed to the max function // Then it should return the largest number overall +test("returns the largest number in an array", () => { + expect(findMax([2, -4, 0, -3, 5])).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("returns the closest number to zero in an array", () => { + expect(findMax([-22, -4, -2, -3, -5])).toEqual(-2); +}); // Given an array with decimal numbers // When passed to the max function // Then it should return the largest decimal number +test("returns the largest decimal number in an array", () => { + expect(findMax([0.34, 6.3, 2.2, 7.1, 5.0])).toEqual(7.1); +}); // 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") +test("throws for an array mixing numbers and strings", () => { + expect(() => findMax([2, -4, "hi", -3, 5])).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") +test("throws when not given an array type", () => { + expect(() => findMax("hey")).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..b35e852b4 100644 --- a/Sprint-1/implement/mean.js +++ b/Sprint-1/implement/mean.js @@ -1,3 +1,21 @@ -function calculateMean(list) {} +function calculateMean(list) { + if (list === undefined) { + throw new Error("calculateMean requires an array of numbers"); + } + 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"); + } + let total = 0; + for (const elements of list) { + if (typeof elements !== "number") { + throw new Error("calculateMean requires an array of numbers"); + } + total = total + elements; + } + return total / list.length; +} module.exports = calculateMean; diff --git a/Sprint-1/implement/mean.test.js b/Sprint-1/implement/mean.test.js index 847b88676..007aa7ade 100644 --- a/Sprint-1/implement/mean.test.js +++ b/Sprint-1/implement/mean.test.js @@ -23,28 +23,56 @@ const calculateMean = require("./mean.js"); // 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"); +test("given [1, 2, 6], returns 3", () => { + expect(calculateMean([1, 2, 6])).toEqual(3); +}); // Given an array with a single number // When passed to calculateMean // Then it should return that number +test("given [1], returns 1", () => { + expect(calculateMean([1])).toEqual(1); +}); // Given an array containing negative or decimal numbers // When passed to calculateMean // Then it should return the correct mean +test("given [1, 2, 6], returns 3", () => { + expect(calculateMean([-2, 2.8, 4.6])).toBeCloseTo(1.8); +}); // Given an empty array // When passed to calculateMean // Then it should throw Error("calculateMean requires a non-empty array") +test("throws an error when given empty array", () => { + 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") +test("throws an error when not given an array", () => { + expect(() => { + calculateMean("hello"); + }).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") +test("throws an error when no argument is given", () => { + 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") +test("throws an error when an array contains non-number values", () => { + 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..8bbe73f1a 100644 --- a/Sprint-1/implement/sum.js +++ b/Sprint-1/implement/sum.js @@ -1,4 +1,21 @@ function sum(elements) { + if (!Array.isArray(elements)) { + throw new Error("sum requires an array of numbers"); + } + for (const items of elements) { + if (typeof items !== "number") { + throw new Error("sum requires an array of numbers"); + } + } + if (elements.length === 0) { + return 0; + } + + let sum = 0; + for (const items of elements) { + sum = sum + items; + } + return sum; } module.exports = sum; diff --git a/Sprint-1/implement/sum.test.js b/Sprint-1/implement/sum.test.js index a288e0f0a..ff9926eed 100644 --- a/Sprint-1/implement/sum.test.js +++ b/Sprint-1/implement/sum.test.js @@ -13,24 +13,45 @@ 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 one element in an array, returns that number", () => { + expect(sum([7])).toEqual(7); +}); // Given an array containing negative numbers // When passed to the sum function // Then it should still return the correct total sum +test("given negative elements in an array, returns sum of negative numbers", () => { + expect(sum([-1, -4, -7, -3])).toEqual(-15); +}); // Given an array with decimal/float numbers // When passed to the sum function // Then it should return the correct total sum +test("given decimal elements in an array, returns sum of decimal numbers", () => { + expect(sum([1.0, 4.4, 2.7, 0.6])).toBeCloseTo(8.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") +test("throws error when elements of an array are not numbers", () => { + expect(() => { + sum(["hi", 7, "hello", "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") +test("throws error when parameter is not an array", () => { + expect(() => { + sum("hello"); + }).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..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; }