diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index 5c5b796e1..4d7cf972b 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -6,9 +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]; - 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 (!Number.isFinite(item)) { + throw new Error("calculateMedian requires an array of numbers"); + } + } + + const listLength = list.length; + const copy = [...list]; + const middleIndex = Math.floor(listLength / 2); + copy.sort((a, b) => a - b); + if (listLength % 2 === 0) { + const median = (copy[middleIndex - 1] + copy[middleIndex]) / 2; + return median; + } else { + return copy[middleIndex]; + } } module.exports = calculateMedian; diff --git a/Sprint-1/implement/dedupe.js b/Sprint-1/implement/dedupe.js index 781e8718a..f4a04d8ba 100644 --- a/Sprint-1/implement/dedupe.js +++ b/Sprint-1/implement/dedupe.js @@ -1 +1,14 @@ -function dedupe() {} +function dedupe(list) { + if (list.length === 0) { + return []; + } + const newList = []; + for (const item of list) { + if (newList.includes(item)) { + continue; + } + newList.push(item); + } + return newList; +} +module.exports = dedupe; diff --git a/Sprint-1/implement/dedupe.test.js b/Sprint-1/implement/dedupe.test.js index d7c8e3d8e..319646cc8 100644 --- a/Sprint-1/implement/dedupe.test.js +++ b/Sprint-1/implement/dedupe.test.js @@ -4,7 +4,7 @@ Dedupe Array 📖 Dedupe means **deduplicate** -In this kata, you will need to deduplicate the elements of an array +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'] E.g. dedupe([5, 1, 1, 2, 3, 2, 5, 8]) returns [5, 1, 2, 3, 8] @@ -16,13 +16,43 @@ 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"); +describe("dedupe", () => { + it("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 + it("given an array, it returns an deduplicate array", () => { + 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]); + }); -// 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. + // Given an array with no duplicates + // When passed to the dedupe function + // Then it should return a copy of the original array + it("Given an array with no duplicates, it returns a copy of the original array", () => { + const originalArray = ["d", "s", "t", "c"]; + const result = dedupe(originalArray); + + expect(result).toEqual(originalArray); + expect(result).not.toBe(originalArray); + + expect(dedupe([5, 4, 3, 2, 1])).toEqual([5, 4, 3, 2, 1]); + expect(dedupe([7, 8, 9])).toEqual([7, 8, 9]); + }); + + // 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. + it("Given an array with duplicates, it removes duplicates while preserving the first occurrence", () => { + expect(dedupe(["d", "s", "s", "t", "c", "d", "s", "t", "c", "d"])).toEqual([ + "d", + "s", + "t", + "c", + ]); + expect(dedupe([5, 4, 5, 4, 2, 1, 5, 3, 4])).toEqual([5, 4, 2, 1, 3]); + expect(dedupe([7, 2, 7, 8, 9])).toEqual([7, 2, 8, 9]); + }); +}); diff --git a/Sprint-1/implement/describe-median.js b/Sprint-1/implement/describe-median.js index 3381cfc0a..c2a20efbf 100644 --- a/Sprint-1/implement/describe-median.js +++ b/Sprint-1/implement/describe-median.js @@ -20,6 +20,13 @@ function calculateMedian(list) { } // Implement this function. See describe-median.test.js for the acceptance criteria. -function describeMedian(list) {} +function describeMedian(list) { + try { + const result = `The median is ${calculateMedian(list)}`; + return result; + } 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..93a35f0aa 100644 --- a/Sprint-1/implement/describe-median.test.js +++ b/Sprint-1/implement/describe-median.test.js @@ -21,11 +21,34 @@ 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"'); - -// Given an empty array -// When passed to describeMedian -// Then it should return "Could not calculate a median: calculateMedian requires a non-empty array" +describe("describeMedian", () => { + it("Given array of positive numbers, it returns the correct message", () => { + expect(describeMedian([1, 2, 6])).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 an error message when given an empty array", () => { + 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("returns an error message when given an empty array", () => { + expect(describeMedian("banana")).toEqual( + "Could not calculate a median: calculateMedian requires an array of numbers" + ); + expect(describeMedian([3, "5", 8])).toEqual( + "Could not calculate a median: calculateMedian requires an array of numbers" + ); + expect(describeMedian(42)).toEqual( + "Could not calculate a median: calculateMedian requires an array of numbers" + ); + }); +}); // Given something that isn't an array of numbers, e.g. "banana" // When passed to describeMedian diff --git a/Sprint-1/implement/max.js b/Sprint-1/implement/max.js index 6dd76378e..99b1e3e58 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"); + } + for (const value of elements) { + if (!Number.isFinite(value)) { + throw new Error("findMax requires an array of numbers"); + } + } + + const arrLength = elements.length; + let maxNum = -Infinity; + if (arrLength === 0) { + return [-Infinity]; + } + for (const item of elements) { + if (maxNum < item) { + maxNum = item; + } + } + return maxNum; +} module.exports = findMax; diff --git a/Sprint-1/implement/max.test.js b/Sprint-1/implement/max.test.js index ff3fbb5e5..43264a7c4 100644 --- a/Sprint-1/implement/max.test.js +++ b/Sprint-1/implement/max.test.js @@ -16,28 +16,87 @@ 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"); +describe("findMax", () => { + it("given an empty array,returns -Infinity", () => { + expect(findMax([])).toEqual(-Infinity); + }); -// Given an array with one number -// When passed to the max function -// Then it should return that number + // Given an array with one number + // When passed to the max function + // Then it should return that number + it("given an array with one number,returns that number", () => { + expect(findMax([5])).toEqual(5); + expect(findMax([20])).toEqual(20); + }); -// 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 both positive and negative numbers + // When passed to the max function + // Then it should return the largest number overall + it("given an array with both positive and negative numbers, return the largest number overall", () => { + expect(findMax([5, 8, 3, 0, -1, 2, 16, -8, -16])).toEqual(16); + expect(findMax([-10, -5, -2, -20, 3, -1])).toEqual(3); + expect(findMax([-100, 50, -30, 25, 0, 75, -5])).toEqual(75); + }); -// 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 just negative numbers + // When passed to the max function + // Then it should return the closest one to zero + it("given an array with just negative numbers, return the closest one to zero", () => { + expect(findMax([-5, -8, -3, -76, -2, -16, -4])).toEqual(-2); + expect(findMax([-1, -2, -9, -100])).toEqual(-1); + }); -// Given an array with decimal numbers -// When passed to the max function -// Then it should return the largest decimal number + // Given an array with decimal numbers + // When passed to the max function + // Then it should return the largest decimal number + it("given an array with decimal numbers, returns the largest decimal number", () => { + expect(findMax([1.5, 3.7, 2.4, 5.9, 4.2])).toEqual(5.9); + expect(findMax([0.1, 0.9, 0.5])).toEqual(0.9); + expect(findMax([-1.5, -3.2, -0.5])).toEqual(-0.5); + }); -// 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 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 when given an array containing a value that isn't a number", () => { + expect(() => findMax(["banana"])).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") + expect(() => findMax([10, "hello", 20])).toThrow( + new Error("findMax requires an array of numbers") + ); + + expect(() => findMax([10, null, 20])).toThrow( + new Error("findMax requires an array of numbers") + ); + + expect(() => findMax([10, NaN, 20])).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 when given something that isn't an array", () => { + 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") + ); + + expect(() => findMax(null)).toThrow( + new Error("findMax requires an array of numbers") + ); + + expect(() => findMax({})).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..e4dd19110 100644 --- a/Sprint-1/implement/mean.js +++ b/Sprint-1/implement/mean.js @@ -1,3 +1,27 @@ -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 element of list) { + if (!Number.isFinite(element)) { + throw new Error("calculateMean requires an array of numbers"); + } + } + + let sum = 0; + + list.forEach((item) => { + sum += item; + }); + + const mean = sum / 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..ad374c553 100644 --- a/Sprint-1/implement/mean.test.js +++ b/Sprint-1/implement/mean.test.js @@ -23,28 +23,58 @@ 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"); +describe("mean", () => { + it("given [1, 2, 6], returns 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 - -// 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 an array with a single number + // When passed to calculateMean + // Then it should return that number + it("given an array with a single number, returns that number", () => { + expect(calculateMean([5])).toEqual(5); + }); + // Given an array containing negative or decimal numbers + // When passed to calculateMean + // Then it should return the correct mean + it("given an array with negative or decimal numbers, returns the correct mean", () => { + expect(calculateMean([-2, -6, -76])).toBeCloseTo(-28, 2); + expect(calculateMean([10, 0, -11])).toBeCloseTo(-0.33, 2); + expect(calculateMean([6.87, 3.12, -11.25])).toBeCloseTo(-0.42, 2); + }); -// 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 an empty array + // When passed to calculateMean + // Then it should throw Error("calculateMean requires a non-empty array") + it("throws when given an empty array", () => { + expect(() => calculateMean([])).toThrow( + new Error("calculateMean requires a non-empty array") + ); + }); -// Given no argument at all -// When passed to calculateMean -// Then it should throw Error("calculateMean requires an array of numbers") + // 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 a value that isn't an array", () => { + expect(() => calculateMean(["banana"])).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") + // Given no argument at all + // When passed to calculateMean + // Then it should throw Error("calculateMean requires an array of numbers") + it("throws when 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 when given an array containing a non-number value", () => { + 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..af9741b65 100644 --- a/Sprint-1/implement/sum.js +++ b/Sprint-1/implement/sum.js @@ -1,4 +1,25 @@ function sum(elements) { + if (!Array.isArray(elements)) { + throw new Error("sum requires an array of numbers"); + } + + for (const item of elements) { + if (!Number.isFinite(item)) { + throw new Error("sum requires an array of numbers"); + } + } + + if (elements.length === 0) { + return 0; + } + + let total = 0; + + elements.forEach((item) => { + total += item; + }); + + 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..52b22f1d2 100644 --- a/Sprint-1/implement/sum.test.js +++ b/Sprint-1/implement/sum.test.js @@ -13,24 +13,58 @@ 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") +describe("sum", () => { + it("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 + // Given an array with just one number + // When passed to the sum function + // Then it should return that number + it("Given an array one number, return that number", () => { + expect(sum([45])).toEqual(45); + }); -// Given an array containing negative numbers -// When passed to the sum function -// Then it should still return the correct total sum + // Given an array containing negative numbers + // When passed to the sum function + // Then it should still return the correct total sum + it("Given an array containing negative numbers, return the correct total sum", () => { + expect(sum([-5, -6, -12])).toEqual(-23); + expect(sum([-22, 17, -9])).toEqual(-14); + expect(sum([-99, 0, -1])).toEqual(-100); + }); -// Given an array with decimal/float numbers -// When passed to the sum function -// Then it should 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 + it("Given an array with decimal/float numbers, return the correct total sum", () => { + expect(sum([1.25, 2.75, 7.12])).toEqual(11.12); + expect(sum([4.12, 7.36, -1.52])).toEqual(9.96); + }); -// 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 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 when given something that isn't an number", () => { + expect(() => sum(["a", "b", "c"])).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") + // 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 when given something that isn't an array", () => { + expect(() => sum("hey")).toThrow( + new Error("sum requires an array of numbers") + ); + + expect(() => sum(null)).toThrow( + new Error("sum requires an array of numbers") + ); + + expect(() => sum(42)).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..8c9ae2e66 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 (const element of list) { if (element === target) { return true; }