diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index 5c5b796e1..7928ccc95 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -6,8 +6,30 @@ // 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]; + 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( + (currentValue) => + typeof currentValue === "number" && !Number.isNaN(currentValue) + ) + ) { + throw new Error("calculateMedian requires an array of numbers"); + } + + const sortedList = list.toSorted((a, b) => a - b); + const middleIndex = Math.floor(sortedList.length / 2); + let median; + if (sortedList.length % 2 === 0) { + median = (sortedList[middleIndex - 1] + sortedList[middleIndex]) / 2; + } else { + median = sortedList[middleIndex]; + } return median; } diff --git a/Sprint-1/implement/dedupe.js b/Sprint-1/implement/dedupe.js index 781e8718a..5631aceef 100644 --- a/Sprint-1/implement/dedupe.js +++ b/Sprint-1/implement/dedupe.js @@ -1 +1,12 @@ -function dedupe() {} +function dedupe(elements) { + const dedupedArray = []; + + for (value of elements) { + if (!dedupedArray.includes(value)) { + dedupedArray.push(value); + } + } + return dedupedArray; +} + +module.exports = dedupe; diff --git a/Sprint-1/implement/dedupe.test.js b/Sprint-1/implement/dedupe.test.js index d7c8e3d8e..dec42086e 100644 --- a/Sprint-1/implement/dedupe.test.js +++ b/Sprint-1/implement/dedupe.test.js @@ -13,16 +13,20 @@ 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"); +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, should return a copy of the same array", () => { + expect(dedupe([1, 5, 9, 23])).toEqual([1, 5, 9, 23]); +}); -// 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 with strings or numbers, should return an array with duplicates removed and first occurrence preserved", () => { + expect(dedupe([1, 6, 1, "a", 5, 6, "a", "b", "b"])).toEqual([ + 1, + 6, + "a", + 5, + "b", + ]); +}); diff --git a/Sprint-1/implement/describe-median.js b/Sprint-1/implement/describe-median.js index 3381cfc0a..551c245f1 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 { + return "The median is " + calculateMedian(list); + } catch { + if (list.length === 0) { + return "Could not calculate a median: calculateMedian requires a non-empty array"; + } + + if (!Array.isArray(list)) { + return "Could not calculate a median: calculateMedian requires an array of numbers"; + } + } +} module.exports = describeMedian; diff --git a/Sprint-1/implement/describe-median.test.js b/Sprint-1/implement/describe-median.test.js index e7d18a264..e12a9cf7c 100644 --- a/Sprint-1/implement/describe-median.test.js +++ b/Sprint-1/implement/describe-median.test.js @@ -17,16 +17,18 @@ 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" +test('given [1, 2, 3], returns "The median is 2"', () => { + expect(describeMedian([1, 2, 3])).toEqual("The median is 2"); +}); + +test('given empty array, 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" + ); +}); + +test('given something that isnt an array of numbers, 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..886687cf6 100644 --- a/Sprint-1/implement/max.js +++ b/Sprint-1/implement/max.js @@ -1,4 +1,24 @@ function findMax(elements) { + if (!Array.isArray(elements)) { + throw new Error("findMax requires an array of numbers"); + } + + if (elements.length === 0) { + return -Infinity; + } + + if ( + !elements.every( + (currentValue) => + typeof currentValue === "number" && !Number.isNaN(currentValue) + ) + ) { + throw new Error("findMax requires an array of numbers"); + } + + sortedElements = elements.toSorted((a, b) => a - b); + + return sortedElements[sortedElements.length - 1]; } module.exports = findMax; diff --git a/Sprint-1/implement/max.test.js b/Sprint-1/implement/max.test.js index ff3fbb5e5..ec46b1a71 100644 --- a/Sprint-1/implement/max.test.js +++ b/Sprint-1/implement/max.test.js @@ -12,32 +12,34 @@ We have set things up already so that this file can see your function from the o 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") +test("given an empty array, returns -Infinity", () => { + expect(findMax([])).toEqual(-Infinity); +}); + +test("given an array with one number, returns that number", () => { + expect(findMax([10])).toEqual(10); +}); + +test("given an array with positive and negative numbers, returns largest number overall", () => { + expect(findMax([10, 58, -74, 984, -87, -995, 47])).toEqual(984); +}); + +test("given an array with only negative numbers, returns number closest to zero", () => { + expect(findMax([-58, -65, -7, -2, -96, -753])).toEqual(-2); +}); + +test("given an array with decimal numbers, returns largest decimal number", () => { + expect(findMax([10, 9.5, 65.6, 41, 36.8])).toEqual(65.6); +}); + +test("given an array with with value that isn't a number, throws error", () => { + expect(() => findMax([10, 23, 18, "hello", 89, 2])).toThrow( + new Error("findMax requires an array of numbers") + ); +}); + +test("given something that isn't an array, throws error", () => { + expect(() => findMax("Hello")).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..039c3507a 100644 --- a/Sprint-1/implement/mean.js +++ b/Sprint-1/implement/mean.js @@ -1,3 +1,29 @@ -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( + (currentValue) => + typeof currentValue === "number" && !Number.isNaN(currentValue) + ) + ) { + throw new Error("calculateMean requires an array of numbers"); + } + + let total = 0; + + for (number of list) { + total += number; + } + const mean = total / list.length; + + return mean; +} module.exports = calculateMean; diff --git a/Sprint-1/implement/mean.test.js b/Sprint-1/implement/mean.test.js index 847b88676..3cbf56a44 100644 --- a/Sprint-1/implement/mean.test.js +++ b/Sprint-1/implement/mean.test.js @@ -19,32 +19,38 @@ 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") +test("given [1, 2, 6], returns 3", () => { + expect(calculateMean([1, 2, 6])).toEqual(3); +}); + +test("given array with single number, return that number", () => { + expect(calculateMean([5])).toEqual(5); +}); + +test("given an array with positive and negative numbers, return the correct mean", () => { + expect(calculateMean([5, 8, -2, 7, -9])).toBeCloseTo(1.8); +}); + +test("given an empty array, throw an error", () => { + expect(() => calculateMean([])).toThrow( + "calculateMean requires a non-empty array" + ); +}); + +test("given something that isn't an array, throw an error", () => { + expect(() => calculateMean("banana")).toThrow( + "calculateMean requires an array of numbers" + ); +}); + +test("given no argument, throw an error", () => { + expect(() => calculateMean()).toThrow( + "calculateMean requires an array of numbers" + ); +}); + +test("given an array with non-number values, throw an error", () => { + expect(() => calculateMean([1, 5, 9, "hello", 4])).toThrow( + "calculateMean requires an array of numbers" + ); +}); diff --git a/Sprint-1/implement/sum.js b/Sprint-1/implement/sum.js index 9062aafe3..b7c76e9e3 100644 --- a/Sprint-1/implement/sum.js +++ b/Sprint-1/implement/sum.js @@ -1,4 +1,22 @@ function sum(elements) { + if (!Array.isArray(elements)) { + throw new Error("sum requires an array of numbers"); + } + + if ( + !elements.every( + (currentValue) => + typeof currentValue === "number" && !Number.isNaN(currentValue) + ) + ) { + throw new Error("sum requires an array of numbers"); + } + + let total = 0; + for (number of elements) { + total += number; + } + return total; } module.exports = sum; diff --git a/Sprint-1/implement/sum.test.js b/Sprint-1/implement/sum.test.js index a288e0f0a..a2cfb3389 100644 --- a/Sprint-1/implement/sum.test.js +++ b/Sprint-1/implement/sum.test.js @@ -10,27 +10,30 @@ 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") +test("given an empty array, returns 0", () => { + expect(sum([])).toEqual(0); +}); + +test("given an array with one number, return that number", () => { + expect(sum([5])).toEqual(5); +}); + +test("given an array with negative numbers, correctly return sum of numbers", () => { + expect(sum([-10, -45, -6, -74])).toEqual(-135); +}); + +test("given an array with decimal numbers, correctly return total sum", () => { + expect(sum([12.7, 3.6, 32.48, 8, 32.53])).toEqual(89.31); +}); + +test("given an array with some values that aren't numbers, throw an Error", () => { + expect(() => { + sum([10, 8, 32, "hello", 2, 53, 32]); + }).toThrow(new Error("sum requires an array of numbers")); +}); + +test("given something that isn't an array, throw an Error", () => { + 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..916bcae13 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 (element of list) { if (element === target) { return true; }