diff --git a/.gitignore b/.gitignore index 8ee70353f..ac74be127 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ node_modules **/.DS_Store .idea package-lock.json +/prep/ diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index 5c5b796e1..8a58f7c90 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -6,9 +6,26 @@ // 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"); + } + + if (!list.every((number) => typeof number === "number")) { + throw new Error("calculateMedian requires an array of numbers"); + } + const sortedList = [...list]; + sortedList.sort((a, b) => a - b); + const middleIndex = Math.floor(sortedList.length / 2); + + if (sortedList.length % 2 === 0) { + return (sortedList[middleIndex - 1] + sortedList[middleIndex]) / 2; + } else { + return sortedList[middleIndex]; + } } module.exports = calculateMedian; diff --git a/Sprint-1/implement/dedupe.js b/Sprint-1/implement/dedupe.js index 781e8718a..16b3eb20b 100644 --- a/Sprint-1/implement/dedupe.js +++ b/Sprint-1/implement/dedupe.js @@ -1 +1,18 @@ -function dedupe() {} +function dedupe(numbers) { + //function starts + const uniqueNumbers = []; + + for (let i = 0; i < numbers.length; i++) { + // for loop starts + const currentValue = numbers[i]; + + if (!uniqueNumbers.includes(currentValue)) { + // if statement starts + uniqueNumbers.push(currentValue); + } // if statement ends + } // for loop ends + + return uniqueNumbers; +} //function ends + +module.exports = dedupe; diff --git a/Sprint-1/implement/describe-median.js b/Sprint-1/implement/describe-median.js index 3381cfc0a..a56e3d7ba 100644 --- a/Sprint-1/implement/describe-median.js +++ b/Sprint-1/implement/describe-median.js @@ -1,5 +1,7 @@ // Don't change this function. function calculateMedian(list) { + + if (!Array.isArray(list)) { throw new Error("calculateMedian requires an array of numbers"); } @@ -20,6 +22,13 @@ function calculateMedian(list) { } // Implement this function. See describe-median.test.js for the acceptance criteria. -function describeMedian(list) {} +function describeMedian(list) { // opens function + try { // opens try statement + const median = calculateMedian(list); + return `The median is ${median}`; + } catch (error) { // opens catch + return "Could not calculate a median: " + error.message; + } +} //closes function module.exports = describeMedian; diff --git a/Sprint-1/implement/describe-median.test.js b/Sprint-1/implement/describe-median.test.js index e7d18a264..0696112ee 100644 --- a/Sprint-1/implement/describe-median.test.js +++ b/Sprint-1/implement/describe-median.test.js @@ -21,12 +21,25 @@ 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 an empty array returns an error message", () => { + 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 a string returns an error message", () => { + 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..663218659 100644 --- a/Sprint-1/implement/max.js +++ b/Sprint-1/implement/max.js @@ -1,4 +1,20 @@ function findMax(elements) { + if ( + !Array.isArray(elements) || + !elements.every((element) => typeof element === "number") + ) { + throw new Error("findMax requires an array of numbers"); + } + + let max = -Infinity; + + for (let i = 0; i < elements.length; i++) { + if (elements[i] > max) { + max = elements[i]; + } + } + + return max; } module.exports = findMax; diff --git a/Sprint-1/implement/max.test.js b/Sprint-1/implement/max.test.js index ff3fbb5e5..21f25ffe7 100644 --- a/Sprint-1/implement/max.test.js +++ b/Sprint-1/implement/max.test.js @@ -16,28 +16,56 @@ 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, should return -Infinity", () => { + expect(findMax([])).toEqual(-Infinity); +}); // Given an array with one number // When passed to the max function // Then it should return that number +test("expects findMax with an array containing 7 to return 7", () => { + expect(findMax([7])).toEqual(7); +}); // 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 largest number given an array with both positive and negative numbers", () => { + expect(findMax([-5, -6, 10, 4])).toEqual(10); +}); // Given an array with just negative numbers // When passed to the max function // Then it should return the closest one to zero - +test("returns largest number when given an array with negative numbers", () => { + expect(findMax([-4, -7, -9, -14])).toEqual(-4); +}); // Given an array with decimal numbers // When passed to the max function // Then it should return the largest decimal number - +test("returns largest number when given an array of decimal numbers", () => { + expect(findMax([2.2, 4.3, 6.8, 9.4])).toEqual(9.4); +}); // 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 an array containing a value that isn't a number", () => { + expect(() => findMax([4, "hello", 5])).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 something that isn't an array at all", () => { + expect(() => findMax("hey")).toThrow( + "findMax requires an array of numbers" + ); + + expect(() => findMax(42)).toThrow( + "findMax requires an array of numbers" + ); + + expect(() => findMax()).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..f8e710d29 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"); + } + if (list.length === 0) { + throw new Error("calculateMean requires a non-empty array"); + } + if (!list.every((number) => typeof number === "number")) { + throw new Error("calculateMean requires an array of numbers"); + } + const sum = list.reduce((sum, number) => { + return sum + number; + }, 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..1b716ae38 100644 --- a/Sprint-1/implement/mean.test.js +++ b/Sprint-1/implement/mean.test.js @@ -23,28 +23,63 @@ 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 the array [1,2,6], should return the number 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 an array with a single number, should return 7", () => { + expect(calculateMean([7])).toEqual(7); +}); // Given an array containing negative or decimal numbers // When passed to calculateMean // Then it should return the correct mean - +test("given an array of negative numbers, should return -4", () => { + expect(calculateMean([-2, -4, -6])).toEqual(-4); +}); +test("given an array of decimal numbers, should return 2.5", () => { + expect(calculateMean([1.5, 2.5, 3.5])).toEqual(2.5); +}); // Given an empty array // When passed to calculateMean // Then it should throw Error("calculateMean requires a non-empty array") - +test("given an empty array, should throw an error", () => { + 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") +test("given a value that isn't an array, should throw an error", () => { + expect(() => calculateMean("banana")).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") + expect(() => calculateMean(42)).toThrow( + "calculateMean requires an array of numbers" + ); + + expect(() => calculateMean(null)).toThrow( + "calculateMean requires an array of numbers" + ); + + expect(() => calculateMean({})).toThrow( + "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 an array containing a non-number value, should throw an error", () => { + expect(() => calculateMean([1, "2", 3])).toThrow( + "calculateMean requires an array of numbers" + ); +}); diff --git a/Sprint-1/implement/sum.js b/Sprint-1/implement/sum.js index 9062aafe3..9fde47645 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.every((element) => typeof element === "number")) { + throw new Error("sum requires an array of numbers"); + } +const sum = elements.reduce((sum, number) => { + return number + sum; + }, 0); + return sum; } - module.exports = sum; diff --git a/Sprint-1/implement/sum.test.js b/Sprint-1/implement/sum.test.js index a288e0f0a..94a78e0af 100644 --- a/Sprint-1/implement/sum.test.js +++ b/Sprint-1/implement/sum.test.js @@ -13,24 +13,49 @@ 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 an array with one number, 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 an array with negative numbers, returns the correct sum", () => { + expect(sum([-4, -6, -2])).toEqual(-12); +}); // Given an array with decimal/float numbers // When passed to the sum function // Then it should return the correct total sum +test("given an array with decimal numbers, returns the correct sum", () => { + expect(sum([2.5, 3.5, 5.5])).toEqual(11.5); +}); // 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 an array containing a non-number, throws an error", () => { + expect(() => sum([10, "hello", 2])).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 a non-array input, throws an error", () => { + expect(() => sum("hello")).toThrow("sum requires an array of numbers"); +}); + +test("given no argument, throws an error", () => { + expect(() => sum()).toThrow("sum requires an array of numbers"); +}); +test("given a number instead of an array, throws an error", () => { + expect(() => sum(42)).toThrow("sum requires an array of numbers"); +}); diff --git a/Sprint-1/refactor/includes.js b/Sprint-1/refactor/includes.js index 29dad81f0..78bbc70b0 100644 --- a/Sprint-1/refactor/includes.js +++ b/Sprint-1/refactor/includes.js @@ -1,8 +1,6 @@ // 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; }