diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index 5c5b796e1..341149049 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -6,9 +6,23 @@ // 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"); + } + } + let arr = [...list]; + arr.sort((a, b) => a - b); + const middleIndex = Math.floor(arr.length / 2); + if (arr.length % 2 === 0) { + return (arr[middleIndex - 1] + arr[middleIndex]) / 2; + } + return arr[middleIndex]; } - module.exports = calculateMedian; diff --git a/Sprint-1/implement/dedupe.js b/Sprint-1/implement/dedupe.js index 781e8718a..9802cae9e 100644 --- a/Sprint-1/implement/dedupe.js +++ b/Sprint-1/implement/dedupe.js @@ -1 +1,5 @@ -function dedupe() {} +function dedupe(a) { + return (a.length === 0) ? [] : [...new Set(a)] +} + +module.exports = dedupe; \ No newline at end of file diff --git a/Sprint-1/implement/dedupe.test.js b/Sprint-1/implement/dedupe.test.js index d7c8e3d8e..ad3a9b09f 100644 --- a/Sprint-1/implement/dedupe.test.js +++ b/Sprint-1/implement/dedupe.test.js @@ -16,13 +16,33 @@ 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", () => { + const arr = []; + const actual = dedupe(arr); + expect(actual).toEqual([]); +}); // Given an array with no duplicates // When passed to the dedupe function // Then it should return a copy of the original array +test("array with no dupes returns original array", () => { + const arr = [1, 2, 3]; + const actual = dedupe(arr); + expect(actual).toEqual([1, 2, 3]); +}) // 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("array with dupe numbers returns a copy with no dupes", () => { + const arr = [1, 2, 2, 3]; + const actual = dedupe(arr); + expect(actual).toEqual([1, 2, 3]); +}) + +test("array with dupe words returns a copy with no dupes", () => { + const arr = ['hi', 'bye', 'bye', 'sigh']; + const actual = dedupe(arr); + expect(actual).toEqual(['hi', 'bye', 'sigh']); +}) diff --git a/Sprint-1/implement/describe-median.js b/Sprint-1/implement/describe-median.js index 3381cfc0a..ca4436819 100644 --- a/Sprint-1/implement/describe-median.js +++ b/Sprint-1/implement/describe-median.js @@ -20,6 +20,14 @@ 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..32d6801a1 100644 --- a/Sprint-1/implement/describe-median.test.js +++ b/Sprint-1/implement/describe-median.test.js @@ -21,12 +21,26 @@ 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"', () => { + const input = [1, 2, 3]; + const result = describeMedian(input); + expect(result).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"', () => { + const input = []; + const result = describeMedian(input); + expect(result).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 "banana", returns "Could not calculate a median: calculateMedian requires an array of numbers"', () => { + const input = 'banana'; + const result = describeMedian(input); + expect(result).toEqual("Could not calculate a median: calculateMedian requires an array of numbers"); +}); \ No newline at end of file diff --git a/Sprint-1/implement/max.js b/Sprint-1/implement/max.js index 6dd76378e..d9fe8b938 100644 --- a/Sprint-1/implement/max.js +++ b/Sprint-1/implement/max.js @@ -1,4 +1,13 @@ function findMax(elements) { + if (!Array.isArray(elements)) { + throw new Error("findMax requires an array of numbers"); + } + for (const item of elements) { + if (typeof item !== "number") { + throw new Error("findMax requires an array of numbers"); + } + } + 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..427bf7fd5 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"); +test("given an empty array, returns -Infinity", () => { + const input = []; + const result = findMax(input); + expect(result).toEqual(-Infinity) +}); // Given an array with one number // When passed to the max function // Then it should return that number +test("given [3], returns 3", () => { + const input = [3]; + const result = findMax(input); + expect(result).toEqual(3) +}); // 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 [3, -2, 4, -10], returns 4", () => { + const input = [3, -2, 4, -10]; + const result = findMax(input); + expect(result).toEqual(4) +}); // Given an array with just negative numbers // When passed to the max function // Then it should return the closest one to zero +test("given [-3, -2, -4, -10], returns -2", () => { + const input = [-3, -2, -4, -10]; + const result = findMax(input); + expect(result).toEqual(-2) +}); // Given an array with decimal numbers // When passed to the max function // Then it should return the largest decimal number - +test("given [0.3, 0.22, 0.64, 0.1], returns 0.64", () => { + const input = [0.3, 0.22, 0.64, 0.1]; + const result = findMax(input); + expect(result).toEqual(0.64) +}); // 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('given ["hi", 2, 3], throws Error:"findMax requires an array of numbers"', () => { + const input = ['hi', 2, 3]; + //const result = findMax(input); + expect(() => findMax(input)).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") +test('given "big snake", throws Error:"findMax requires an array of numbers"', () => { + const input = "big snake"; + //const result = findMax(input); + expect(() => findMax(input)).toThrow("findMax requires an array of numbers") +}); \ No newline at end of file diff --git a/Sprint-1/implement/mean.js b/Sprint-1/implement/mean.js index 36909ea1f..930a78a78 100644 --- a/Sprint-1/implement/mean.js +++ b/Sprint-1/implement/mean.js @@ -1,3 +1,24 @@ -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"); + } + }; + let sum = 0; + for (let i=0; i { + const input = [1, 2, 6]; + const result = calculateMean(input); + expect(result).toEqual(3); +}); // Given an array with a single number // When passed to calculateMean // Then it should return that number +test("given [69], returns 69", () => { + const input = [69]; + const result = calculateMean(input); + expect(result).toEqual(69); +}); // Given an array containing negative or decimal numbers // When passed to calculateMean // Then it should return the correct mean +test("given [-1, 0.2, 6], returns 1.73", () => { + const input = [-1, 0.2, 6]; + const result = calculateMean(input); + expect(result).toEqual(1.7333333333333334); +}); // Given an empty array // When passed to calculateMean // Then it should throw Error("calculateMean requires a non-empty array") +test('given [], throw Error("calculateMean requires a non-empty array"', () => { + const input = []; + expect(() => calculateMean(input)).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") +test('given "catfish cabin", throw Error("calculateMean requires an array of numbers"', () => { + const input = "catfish cabin"; + expect(() => calculateMean(input)).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") - +test('given "catfish cabin", throw Error("calculateMean requires an array of numbers"', () => { + 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") +test('given [1, "2", 3], throw Error("calculateMean requires an array of numbers"', () => { + const input = [1, "2", 3]; + expect(() => calculateMean(input)).toThrow("calculateMean requires an array of numbers"); +}); \ No newline at end of file diff --git a/Sprint-1/implement/sum.js b/Sprint-1/implement/sum.js index 9062aafe3..864633a27 100644 --- a/Sprint-1/implement/sum.js +++ b/Sprint-1/implement/sum.js @@ -1,4 +1,17 @@ function sum(elements) { + if (!Array.isArray(elements)){ + throw new Error("sum requires an array of numbers"); + } + for (const item of elements){ + if (typeof item !== 'number'){ + throw new Error("sum requires an array of numbers"); + } + } + let result = 0; + for (let i = 0; i < elements.length; i++){ + result += elements[i] + } + return result } module.exports = sum; diff --git a/Sprint-1/implement/sum.test.js b/Sprint-1/implement/sum.test.js index a288e0f0a..7ceae5eef 100644 --- a/Sprint-1/implement/sum.test.js +++ b/Sprint-1/implement/sum.test.js @@ -13,24 +13,51 @@ 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", () => { + const input = []; + result = sum(input); + expect(result).toEqual(0); +}) // Given an array with just one number // When passed to the sum function // Then it should return that number +test("given [12], returns 12", () => { + const input = [12]; + result = sum(input); + expect(result).toEqual(12); +}) // Given an array containing negative numbers // When passed to the sum function // Then it should still return the correct total sum +test("given [-1, -2, -3], returns -6", () => { + const input = [-1, -2, -3]; + result = sum(input); + expect(result).toEqual(-6); +}) // Given an array with decimal/float numbers // When passed to the sum function // Then it should return the correct total sum +test("given [0.1, 0.2, 0.34], returns 0.6000000000000001", () => { + const input = [0.1, 0.2, 0.3]; + result = sum(input); + expect(result).toEqual(0.6000000000000001); +}) // 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("given ['grace', 'peter', 'steph', 'ruth', 'richie'], throw Error:'sum requires an array of numbers'", () => { + const input = ['grace', 'peter', 'steph', 'ruth', 'richie']; + expect(() => sum(input)).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") +test("given 'stink stank stunk', throw Error:'sum requires an array of numbers'", () => { + const input = 'stink stank stunk'; + expect(() => sum(input)).toThrow("sum requires an array of numbers"); +}) diff --git a/Sprint-1/refactor/includes.js b/Sprint-1/refactor/includes.js index 29dad81f0..36d9dfb1f 100644 --- a/Sprint-1/refactor/includes.js +++ b/Sprint-1/refactor/includes.js @@ -1,12 +1,11 @@ // 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]; - if (element === target) { + for (item of list) { + if (target === item) return true; } - } + return false; }