diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index 5c5b796e19..0315f28180 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -1,14 +1,20 @@ -// Fix this implementation -// 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 +function calculateMedian(list) { + if ( + !Array.isArray(list) || + list.length === 0 || + list.some((item) => typeof item !== "number" || Number.isNaN(item)) + ) { + throw new Error("calculateMedian requires an array of numbers"); + } -// 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). + const sorted = [...list].sort((a, b) => a - b); + const middleIndex = Math.floor(sorted.length / 2); -function calculateMedian(list) { - const middleIndex = Math.floor(list.length / 2); - const median = list.splice(middleIndex, 1)[0]; - return median; + if (sorted.length % 2 === 0) { + return (sorted[middleIndex - 1] + sorted[middleIndex]) / 2; + } + + return sorted[middleIndex]; } -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 781e8718a5..b0aa86afd8 100644 --- a/Sprint-1/implement/dedupe.js +++ b/Sprint-1/implement/dedupe.js @@ -1 +1,5 @@ -function dedupe() {} +function dedupe(elements) { + return [...new Set(elements)]; +} + +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 d7c8e3d8e2..73b1d5a524 100644 --- a/Sprint-1/implement/dedupe.test.js +++ b/Sprint-1/implement/dedupe.test.js @@ -1,28 +1,20 @@ const dedupe = require("./dedupe.js"); -/* -Dedupe Array -📖 Dedupe means **deduplicate** +describe("dedupe", () => { + test("given an empty array, it returns an empty array", () => { + expect(dedupe([])).toEqual([]); + }); -In this kata, you will need to deduplicate the elements of an 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); + expect(result).toEqual([1, 2, 3]); + expect(result).not.toBe(input); + }); -E.g. dedupe(['a','a','a','b','b','c']) returns ['a','b','c'] -E.g. dedupe([5, 1, 1, 2, 3, 2, 5, 8]) returns [5, 1, 2, 3, 8] -E.g. dedupe([1, 2, 1]) returns [1, 2] -*/ - -// Acceptance Criteria: - -// 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"); - -// Given an array with no duplicates -// When passed to the dedupe function -// Then it should return a copy of the original array - -// 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("given an array of strings or numbers, it returns a new array with duplicates removed while preserving the first occurrence", () => { + 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]); + }); +}); \ No newline at end of file diff --git a/Sprint-1/implement/describe-median.js b/Sprint-1/implement/describe-median.js index 3381cfc0aa..6704ecaa81 100644 --- a/Sprint-1/implement/describe-median.js +++ b/Sprint-1/implement/describe-median.js @@ -1,8 +1,4 @@ -// Don't change this function. 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"); } @@ -20,6 +16,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; +module.exports = describeMedian; \ No newline at end of file diff --git a/Sprint-1/implement/describe-median.test.js b/Sprint-1/implement/describe-median.test.js index e7d18a2644..d0c3352513 100644 --- a/Sprint-1/implement/describe-median.test.js +++ b/Sprint-1/implement/describe-median.test.js @@ -1,32 +1,22 @@ -/* Describe the median, or explain why there isn't one - -calculateMedian is given to you at the top of describe-median.js. It throws -when it can't calculate a median. Don't change it. - -Implement describeMedian, which calls calculateMedian and returns a sentence -instead of crashing. - -E.g. describeMedian([1, 2, 3]), target output: "The median is 2" -E.g. describeMedian([]), target output: "Could not calculate a median: calculateMedian requires a non-empty array" - -You'll need a try/catch block for this. The catch block must use the error's -message, so the caller can see what went wrong. -*/ - const describeMedian = require("./describe-median.js"); -// Acceptance criteria: - -// 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"'); - -// 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", () => { + test('given [1, 2, 3], returns "The median is 2"', () => { + expect(describeMedian([1, 2, 3])).toBe("The median is 2"); + }); + + test("given an empty array, returns an explanation using the error message", () => { + expect(describeMedian([])).toBe( + "Could not calculate a median: calculateMedian requires a non-empty array" + ); + }); + + test("given something that isn't an array of numbers, returns an explanation using the error message", () => { + expect(describeMedian("banana")).toBe( + "Could not calculate a median: calculateMedian requires an array of numbers" + ); + expect(describeMedian([1, "two", 3])).toBe( + "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 6dd76378e5..07185bd995 100644 --- a/Sprint-1/implement/max.js +++ b/Sprint-1/implement/max.js @@ -1,4 +1,12 @@ function findMax(elements) { + if ( + !Array.isArray(elements) || + elements.some((item) => typeof item !== "number" || Number.isNaN(item)) + ) { + throw new Error("findMax requires an array of numbers"); + } + + return Math.max(...elements); } -module.exports = findMax; +module.exports = findMax; \ No newline at end of file diff --git a/Sprint-1/implement/max.test.js b/Sprint-1/implement/max.test.js index ff3fbb5e5f..2d8739f47d 100644 --- a/Sprint-1/implement/max.test.js +++ b/Sprint-1/implement/max.test.js @@ -1,43 +1,41 @@ -/* Find the maximum element of an array of numbers - -In this kata, you will need to implement a function that find the largest numerical element of an array. - -E.g. max([30, 50, 10, 40]), target output: 50 -E.g. max(['hey', 10, 'hi', 60, 10]) throws Error("findMax requires an array of numbers") (max can't compare non-numerical elements, so it shouldn't guess) - -You should implement this function in max.js, and add tests for it in this file. - -We have set things up already so that this file can see your function from the other file. -*/ - 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"); - -// 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 - -// 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 decimal numbers -// When passed to the max function -// Then it should return the largest decimal number - -// 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 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") +describe("findMax", () => { + test("given an empty array, returns -Infinity", () => { + expect(findMax([])).toBe(-Infinity); + }); + + test("given an array with one number, returns that number", () => { + expect(findMax([42])).toBe(42); + }); + + test("given an array with both positive and negative numbers, returns the largest number overall", () => { + expect(findMax([-10, 50, 0, -25, 30])).toBe(50); + }); + + test("given an array with just negative numbers, returns the closest one to zero", () => { + expect(findMax([-30, -5, -100, -12])).toBe(-5); + }); + + test("given an array with decimal numbers, returns the largest decimal number", () => { + expect(findMax([1.5, 3.75, 2.1, 3.74])).toBe(3.75); + }); + + test("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") + ); + }); + + test("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") + ); + }); +}); \ No newline at end of file diff --git a/Sprint-1/implement/mean.js b/Sprint-1/implement/mean.js index 36909ea1f6..4618e846ae 100644 --- a/Sprint-1/implement/mean.js +++ b/Sprint-1/implement/mean.js @@ -1,3 +1,18 @@ -function calculateMean(list) {} +function calculateMean(list) { + if (!Array.isArray(list)) { + throw new Error("calculateMean requires an array of numbers"); + } -module.exports = calculateMean; + if (list.length === 0) { + throw new Error("calculateMean requires a non-empty array"); + } + + if (list.some((item) => typeof item !== "number" || Number.isNaN(item))) { + throw new Error("calculateMean requires an array of numbers"); + } + + const total = list.reduce((sum, num) => sum + num, 0); + return total / 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 847b88676f..287e4c4271 100644 --- a/Sprint-1/implement/mean.test.js +++ b/Sprint-1/implement/mean.test.js @@ -1,50 +1,49 @@ -/* Calculate the mean of an array of numbers - -In this kata, you will need to implement a function that calculates the mean of an array of numbers. - -E.g. calculateMean([1, 2, 6]), target output: 3 -E.g. calculateMean([]) throws Error("calculateMean requires a non-empty array") -E.g. calculateMean("banana") throws Error("calculateMean requires an array of numbers") - -There is no mean of an empty array, and a string isn't an array of numbers, so -calculateMean should throw rather than return a value. This is the same problem -calculateMedian solves in the prep and in the fix directory. - -You should implement this function in mean.js, and add tests for it in this file. - -We have set things up already so that this file can see your function from the other file. -*/ - 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"); - -// Given an array with a single number -// When passed to calculateMean -// Then it should return that number - -// Given an array containing negative or decimal numbers -// When passed to calculateMean -// Then it should return the correct mean - -// Given an empty array -// When passed to calculateMean -// Then it should throw 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") - -// Given no argument at all -// When passed to calculateMean -// Then it should throw 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") +describe("calculateMean", () => { + test("given an array of numbers, returns their mean", () => { + expect(calculateMean([1, 2, 6])).toBe(3); + }); + + test("given an array with a single number, returns that number", () => { + expect(calculateMean([10])).toBe(10); + }); + + test("given an array containing negative or decimal numbers, returns the correct mean", () => { + expect(calculateMean([-10, 20, -4])).toBe(2); + expect(calculateMean([1.5, 2.5, 3.5])).toBeCloseTo(2.5); + }); + + test("given an empty array, throws Error('calculateMean requires a non-empty array')", () => { + expect(() => calculateMean([])).toThrow( + new Error("calculateMean requires a non-empty array") + ); + }); + + test("given a value that isn't an array, throws Error('calculateMean requires an array of numbers')", () => { + 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") + ); + }); + + test("given no argument at all, throws Error('calculateMean requires an array of numbers')", () => { + expect(() => calculateMean()).toThrow( + new Error("calculateMean requires an array of numbers") + ); + }); + + test("given an array containing a non-number value, throws Error('calculateMean requires an array of numbers')", () => { + expect(() => calculateMean([1, "2", 3])).toThrow( + new Error("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 9062aafe38..65881d6e4c 100644 --- a/Sprint-1/implement/sum.js +++ b/Sprint-1/implement/sum.js @@ -1,4 +1,12 @@ function sum(elements) { + if ( + !Array.isArray(elements) || + elements.some((item) => typeof item !== "number" || Number.isNaN(item)) + ) { + throw new Error("sum requires an array of numbers"); + } + + return elements.reduce((total, num) => total + num, 0); } -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 a288e0f0aa..871f2389f7 100644 --- a/Sprint-1/implement/sum.test.js +++ b/Sprint-1/implement/sum.test.js @@ -1,36 +1,37 @@ -/* Sum the numbers in an array - -In this kata, you will need to implement a function that sums the numerical elements of an array - -E.g. sum([10, 20, 30]), target output: 60 -E.g. sum(['hey', 10, 'hi', 60, 10]) throws Error("sum requires an array of numbers") (sum can't add non-numerical elements, so it shouldn't guess) -*/ - const sum = require("./sum.js"); -// Acceptance Criteria: - -// Given an empty array -// When passed to the sum function -// Then it should return 0 -test.todo("given an empty array, returns 0") - -// Given an array with just one number -// When passed to the sum function -// Then it should return that number - -// Given an array containing negative numbers -// When passed to the sum function -// Then it should still return the correct total sum - -// 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 -// When passed to the sum function -// Then it should throw 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") +describe("sum", () => { + test("given an empty array, returns 0", () => { + expect(sum([])).toBe(0); + }); + + test("given an array with just one number, returns that number", () => { + expect(sum([15])).toBe(15); + }); + + test("given an array containing negative numbers, returns the correct total sum", () => { + expect(sum([-10, 25, -5, 10])).toBe(20); + }); + + test("given an array with decimal/float numbers, returns the correct total sum", () => { + expect(sum([1.5, 2.25, 3.25])).toBeCloseTo(7); + }); + + test("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") + ); + }); + + test("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") + ); + }); +}); \ No newline at end of file