diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index 5c5b796e1..968154022 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -5,10 +5,31 @@ // 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; +// } + +// module.exports = calculateMedian; + 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"); + } else if (list.some((item) => typeof item !== "number")) { + throw new Error("calculateMedian requires an array of numbers"); + } else if (list.length === 0) { + throw new Error("calculateMedian requires a non-empty array"); + } + const copyList = Array.from(list); + let sortList = copyList.sort((a, b) => a - b); + if (sortList.length % 2 === 1) { + const middleIndex = Math.floor(sortList.length / 2); + return sortList[middleIndex]; + } + const rightMiddleIndex = Math.floor(sortList.length / 2 - 1); + const leftMiddleIndex = Math.floor(sortList.length / 2); + return (sortList[rightMiddleIndex] + sortList[leftMiddleIndex]) / 2; } module.exports = calculateMedian; diff --git a/Sprint-1/implement/dedupe.js b/Sprint-1/implement/dedupe.js index 781e8718a..298230541 100644 --- a/Sprint-1/implement/dedupe.js +++ b/Sprint-1/implement/dedupe.js @@ -1 +1,5 @@ -function dedupe() {} +function dedupe(array) { + return [...new Set(array)]; +} + +module.exports = dedupe; diff --git a/Sprint-1/implement/dedupe.test.js b/Sprint-1/implement/dedupe.test.js index d7c8e3d8e..cc3805a97 100644 --- a/Sprint-1/implement/dedupe.test.js +++ b/Sprint-1/implement/dedupe.test.js @@ -1,9 +1,10 @@ const dedupe = require("./dedupe.js"); /* Dedupe Array - 📖 Dedupe means **deduplicate** - +*/ +describe("deduplicate", () => { + /* In this kata, you will need to deduplicate the elements of an array E.g. dedupe(['a','a','a','b','b','c']) returns ['a','b','c'] @@ -11,18 +12,47 @@ 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"); + // Acceptance Criteria: + // Given an empty array + // When passed to the dedupe function + // Then it should return an empty array + it(`returns the 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("doesn't modify the input array [3, 1, 1, 2]", () => { + const array = [3, 1, 1, 2]; + dedupe(array); + expect(array).toEqual([3, 1, 1, 2]); +}); // 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(`returns the deduplicate for string letters`, () => { + expect(dedupe(["a", "a", "a", "b", "b", "c"])).toEqual(["a", "b", "c"]); +}); + +it(`returns the deduplicate for numbers`, () => { + expect(dedupe([5, 1, 1, 2, 3, 2, 5, 8])).toEqual([5, 1, 2, 3, 8]); +}); + +it(`returns the deduplicate while not numerical in order`, () => { + expect(dedupe([1, 2, 1])).toEqual([1, 2]); +}); + +it(`returns the deduplicate for string and numbers `, () => { + expect(dedupe([5, 1, 1, 2, 3, "a", "a", "a", "c"])).toEqual([ + 5, + 1, + 2, + 3, + "a", + "c", + ]); +}); diff --git a/Sprint-1/implement/describe-median.js b/Sprint-1/implement/describe-median.js index 3381cfc0a..c9ce2385a 100644 --- a/Sprint-1/implement/describe-median.js +++ b/Sprint-1/implement/describe-median.js @@ -20,6 +20,12 @@ function calculateMedian(list) { } // Implement this function. See describe-median.test.js for the acceptance criteria. -function describeMedian(list) {} +function describeMedian(list) { + try { + return `The median is ${calculateMedian(list)}`; + } 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..52540b428 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"'); +it("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("returns error 'Could not calculate a median: calculateMedian requires a non-empty array' when array empty", () => { + 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("Throws error 'calculateMedian requires an array of numbers' when it isn't 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..7559139ab 100644 --- a/Sprint-1/implement/max.js +++ b/Sprint-1/implement/max.js @@ -1,4 +1,19 @@ function findMax(elements) { + if (elements.length === 0) { + return "Infinity"; + } + if (!Array.isArray(elements)) { + throw new Error("findMax requires an array of numbers"); + } + + const copyElements = Array.from(elements); + for (const item of copyElements) { + if (typeof item !== "number") { + throw new Error("findMax requires an array of numbers"); + } + } + let sortElements = copyElements.sort((a, b) => a - b); + return sortElements.at(-1); } module.exports = findMax; diff --git a/Sprint-1/implement/max.test.js b/Sprint-1/implement/max.test.js index ff3fbb5e5..fd966d067 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"); +it("returns the Infinity for []", () => { + expect(findMax([])).toEqual("Infinity"); +}); // Given an array with one number // When passed to the max function // Then it should return that number +it("returns the median for [40]", () => { + expect(findMax([40])).toEqual(40); +}); // 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 the median for [20, 30, 10, 40]", () => { + expect(findMax([20, 30, 10, 40])).toEqual(40); +}); // Given an array with just negative numbers // When passed to the max function // Then it should return the closest one to zero +it("returns the median for [-40, -20, -30, -70]", () => { + expect(findMax([-40, -20, -30, -70])).toEqual(-20); +}); // Given an array with decimal numbers // When passed to the max function // Then it should return the largest decimal number +it("returns the median for [-40.4, -20.7, -30.3, -70.6]", () => { + expect(findMax([-40.4, -20.7, -30.3, -70.6])).toEqual(-20.7); +}); // 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 error 'findMax requires an array of numbers' when containing a value that isn't a number", () => { + expect(() => findMax([10, 40, "hello", 30])).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("Throws error 'findMax requires an array of numbers' Given something that isn't an array at all", () => { + expect(() => findMax(42)).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..a48136c6e 100644 --- a/Sprint-1/implement/mean.js +++ b/Sprint-1/implement/mean.js @@ -1,3 +1,18 @@ -function calculateMean(list) {} +function calculateMean(list) { + if (list.length === 0) { + throw new Error("calculateMean requires a non-empty array"); + } + + if (!Array.isArray(list)) { + throw new Error("calculateMean requires an array of numbers"); + } + + const copyList = Array.from(list); + let total = 0; + for (let i = 0; i < copyList.length; i++) { + total += copyList[i]; + } + return total / copyList.length; +} module.exports = calculateMean; diff --git a/Sprint-1/implement/mean.test.js b/Sprint-1/implement/mean.test.js index 847b88676..45783bac0 100644 --- a/Sprint-1/implement/mean.test.js +++ b/Sprint-1/implement/mean.test.js @@ -23,28 +23,62 @@ 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"); +it("Returns correct value of 3 for mean of numbers in array of [1, 2, 6]", () => { + expect(calculateMean([1, 2, 6])).toEqual(3); +}); // Given an array with a single number // When passed to calculateMean // Then it should return that number +it("returns mean of single number in array", () => { + expect(calculateMean([6])).toEqual(6); +}); // Given an array containing negative or decimal numbers // When passed to calculateMean // Then it should return the correct mean +it("returns mean containing negative or decimal numbers in array", () => { + expect(calculateMean([-6, 8.5, 23])).toEqual(8.5); +}); // Given an empty array // When passed to calculateMean // Then it should throw Error("calculateMean requires a non-empty array") +it("Throws error 'calculateMean requires a non-empty array' Given empty", () => { + 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("Throws error 'calculateMean requires an array of numbers' Given value that isn't an array", () => { + expect(() => + calculateMean("Hello bob").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("Throws error 'calculateMean requires an array of numbers' Given Given no argument at all", () => { + 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("Throws error 'calculateMean requires an array of numbers' an array containing a non-number value, e.g. [1, '2', 3]", () => { + 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..64fcb4441 100644 --- a/Sprint-1/implement/sum.js +++ b/Sprint-1/implement/sum.js @@ -1,4 +1,26 @@ function sum(elements) { + if (elements.length === 0) { + return 0; + } + 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"); + } + + const copyElements = Array.from(elements); + let total = 0; + for (let i = 0; i < copyElements.length; i++) { + total += copyElements[i]; + } + if (Number.isInteger(total)) { + return total; + } else { + return Number(total.toFixed(2)); + } + } } module.exports = sum; diff --git a/Sprint-1/implement/sum.test.js b/Sprint-1/implement/sum.test.js index a288e0f0a..e0fd7cfc1 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") +it("returns 0 when given an empty array", () => { + expect(sum([])).toEqual(0); +}); // Given an array with just one number // When passed to the sum function // Then it should return that number +it("returns same number when given an array with just one number", () => { + expect(sum([5])).toEqual(5); +}); // Given an array containing negative numbers // When passed to the sum function // Then it should still return the correct total sum +it("returns total when given an array with negative numbers", () => { + expect(sum([-5, -2, -10])).toEqual(-17); +}); // Given an array with decimal/float numbers // When passed to the sum function // Then it should return the correct total sum +it("returns total when given an array with decimal/float numbers", () => { + expect(sum([2.6, 4.6, 8.12])).toEqual(15.32); +}); // 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 an error 'sum requires an array of numbers' when value isn't a number", () => { + expect(()=> sum(["hello", "bob"])).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("throws an error 'sum requires an array of numbers' when value isn't an array", () => { + expect(() => sum(["2", 2, 5, "bob"])).toThrow( + new Error("sum requires an array of numbers") + ); +});