diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index 5c5b796e1..390414624 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -6,9 +6,29 @@ // 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) === false) { + throw new Error("calculateMedian requires an array of numbers"); + } + + if (list.length === 0) { + throw new Error("calculateMedian requires a non-empty array"); + } + + if (list.some((item) => typeof item === "string" || item === null || item === undefined)) { + throw new Error("calculateMedian requires an array of numbers"); + } + + const sortedList = [...list].sort((a, b) => a - b); + + if (sortedList.length % 2 === 0) { + const evenMiddleIndex = sortedList.length / 2; + const sumOfEvenMiddleIndex = + sortedList[evenMiddleIndex - 1] + sortedList[evenMiddleIndex] + return sumOfEvenMiddleIndex / 2; + } else { + const oddMiddleIndex = Math.floor(sortedList.length / 2); + return sortedList[oddMiddleIndex]; + } } module.exports = calculateMedian; diff --git a/Sprint-1/implement/dedupe.js b/Sprint-1/implement/dedupe.js index 781e8718a..7abb5fb70 100644 --- a/Sprint-1/implement/dedupe.js +++ b/Sprint-1/implement/dedupe.js @@ -1 +1,5 @@ -function dedupe() {} +function dedupe(list) { + return [...new Set(list)]; +} + +module.exports = dedupe; diff --git a/Sprint-1/implement/dedupe.test.js b/Sprint-1/implement/dedupe.test.js index d7c8e3d8e..2e226dd99 100644 --- a/Sprint-1/implement/dedupe.test.js +++ b/Sprint-1/implement/dedupe.test.js @@ -1,28 +1,56 @@ const dedupe = require("./dedupe.js"); -/* -Dedupe Array -📖 Dedupe means **deduplicate** +//Dedupe Array -In this kata, you will need to deduplicate the elements of an array +//📖 Dedupe means **deduplicate** -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] -*/ +//In this kata, you will need to deduplicate the elements of an array -// Acceptance Criteria: +describe("dedupe", () => { + //E.g. dedupe(['a','a','a','b','b','c']) returns ['a','b','c'] + it("returns for ['a','a','a','b','b','c']", () => { + expect(dedupe(["a", "a", "a", "b", "b", "c"])).toEqual(["a", "b", "c"]); + }); -// 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"); + //E.g. dedupe([5, 1, 1, 2, 3, 2, 5, 8]) returns [5, 1, 2, 3, 8] + it("returns for [5, 1, 1, 2, 3, 2, 5, 8]", () => { + expect(dedupe([5, 1, 1, 2, 3, 2, 5, 8])).toEqual([5, 1, 2, 3, 8]); + }); + //E.g. dedupe([1, 2, 1]) returns [1, 2] + //*/ + it("returns for [1, 2, 1]", () => { + expect(dedupe([1, 2, 1])).toEqual([1, 2]); + }); -// Given an array with no duplicates -// When passed to the dedupe function -// Then it should return a copy of the original array + // Acceptance Criteria: -// 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 empty array + // When passed to the dedupe function + // Then it should return an empty array + + it("returns for []", () => { + 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("returns for [1, 2, 3, 4]", () => { + const original = [1, 2, 3, 4]; + const result = dedupe(original); + expect(result).not.toBe(original); + }); + + // 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("returns for ['apple', 'orange', 'banana', 'banana']", () => { + expect(dedupe(["apple", "orange", "banana", "banana"])).toEqual([ + "apple", + "orange", + "banana", + ]); + }); +}); diff --git a/Sprint-1/implement/describe-median.js b/Sprint-1/implement/describe-median.js index 3381cfc0a..ea3f8321d 100644 --- a/Sprint-1/implement/describe-median.js +++ b/Sprint-1/implement/describe-median.js @@ -1,14 +1,14 @@ // Don't change this function. function calculateMedian(list) { if (!Array.isArray(list)) { - throw new Error("calculateMedian requires an array of numbers"); + throw new Error("Could not calculate a median: calculateMedian requires an array of numbers"); } if (list.length === 0) { - throw new Error("calculateMedian requires a non-empty array"); + throw new Error("Could not calculate a median: calculateMedian requires a non-empty array"); } for (const item of list) { if (typeof item !== "number") { - throw new Error("calculateMedian requires an array of numbers"); + throw new Error("Could not calculate a median: calculateMedian requires an array of numbers"); } } const middleIndex = Math.floor(list.length / 2); @@ -20,6 +20,9 @@ function calculateMedian(list) { } // Implement this function. See describe-median.test.js for the acceptance criteria. -function describeMedian(list) {} +function describeMedian(list) { + const result = calculateMedian(list); + return `The median is ${result}` +} module.exports = describeMedian; diff --git a/Sprint-1/implement/describe-median.test.js b/Sprint-1/implement/describe-median.test.js index e7d18a264..59dcf2b57 100644 --- a/Sprint-1/implement/describe-median.test.js +++ b/Sprint-1/implement/describe-median.test.js @@ -21,12 +21,32 @@ 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" - -// 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", () => { + it("returns for [1, 2, 3, 4]", () => { + const array = [1, 2, 3, 4]; + const result = describeMedian(array); + expect(result).toBe("The median is 2.5"); + }); + + // 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 for []", () => { + expect(() => describeMedian([])).toThrow( + new Error( + "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 for ['banana', 'apple', 'orange']", () => { + expect(() => describeMedian(["banana", "apple", "orange"])).toThrow( + new Error( + "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..05562c338 100644 --- a/Sprint-1/implement/max.js +++ b/Sprint-1/implement/max.js @@ -1,4 +1,10 @@ function findMax(elements) { + if (!Array.isArray(elements)) { + throw new Error("findMax requires an array of numbers"); + } + if (elements.some((element) => typeof element === "string")) { + throw new Error("findMax requires an array of numbers"); + } + return Math.max(...elements); } - module.exports = findMax; diff --git a/Sprint-1/implement/max.test.js b/Sprint-1/implement/max.test.js index ff3fbb5e5..0623badf0 100644 --- a/Sprint-1/implement/max.test.js +++ b/Sprint-1/implement/max.test.js @@ -16,28 +16,72 @@ 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("returns for []", () => { + 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("returns for [17]", () => { + expect(findMax([17])).toEqual(17); + }); -// 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 -// Given an array with just negative numbers -// When passed to the max function -// Then it should return the closest one to zero + it("returns for [-6, 15, 30, 42, -90]", () => { + expect(findMax([-6, 15, 30, 42, -90])).toEqual(42); + }); -// Given an array with decimal numbers -// When passed to the max function -// Then it should return the largest decimal number + // 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 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("returns for [-6, 15, 30, 42, -90]", () => { + expect(findMax([-6, 15, 30, 42, -90])).toEqual(42); + }); -// 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") + // Given an array with decimal numbers + // When passed to the max function + // Then it should return the largest decimal number + + it("returns for [0.5, 1.5, 3.5, 5.5, 7.5]", () => { + expect(findMax([0.5, 1.5, 3.5, 5.5, 7.5])).toEqual(7.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") + + it("returns for [2, 4, 'hello', 6, 8, 10]", () => { + () => + expect(findMax([2, 4, "hello", 6, 8, 10])).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("returns for 'hey'", () => { + expect(() => findMax("hey")).toThrow( + new Error("findMax requires an array of numbers") + ); + }); + + it("returns for 42", () => { + expect(() => findMax(42)).toThrow( + new Error("findMax requires an array of numbers") + ); + }); + + it("returns for ''", () => { + 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..0e984b9ef 100644 --- a/Sprint-1/implement/mean.js +++ b/Sprint-1/implement/mean.js @@ -1,3 +1,24 @@ -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.some((item) => typeof item === "string")) { + throw new Error("calculateMean requires an array of numbers"); + } + + let sum = 0; + for (const item of list) { + 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..06f572107 100644 --- a/Sprint-1/implement/mean.test.js +++ b/Sprint-1/implement/mean.test.js @@ -23,28 +23,91 @@ 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("calculateMean", () => { + it("return [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 + // Given an array with a single number + // When passed to calculateMean + // Then it should return that number + it("returns for [6]", () => { + expect(calculateMean([6])).toEqual(6); + }); -// Given an array containing negative or decimal numbers -// When passed to calculateMean -// Then it should return the correct mean + // Given an array containing negative or decimal numbers + // When passed to calculateMean + // Then it should return the correct mean + it("returns for 10, 20.5, -10, 30.5]", () => { + expect(calculateMean([10, 20.5, -10, 30.5])).toEqual(12.75); + }); -// Given an empty array -// When passed to calculateMean -// Then it should throw Error("calculateMean requires a non-empty array") + it ("returns for [1.2, 0.6, 0.005]", () => { + expect(calculateMean([1.2, 0.6, 0.005])).toBeCloseTo(0.6017); -// 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 empty array + // When passed to calculateMean + // Then it should throw Error("calculateMean requires a non-empty array") -// 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("returns for []", () => { + () => + 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("returns for 'banana'", () => { + () => + expect(calculateMean("banana")).toThrow( + new Error("calculateMean requires an array of numbers") + ); + }); + + it("returns for 42", () => { + () => + expect(calculateMean(42)).toThrow( + new Error("calculateMean requires an array of numbers") + ); + }); + + it("returns for null", () => { + () => + expect(calculateMean(null)).toThrow( + new Error("calculateMean requires an array of numbers") + ); + }); + + it("returns for {}", () => { + () => + expect(calculateMean({})).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("returns for ''", () => { + () => + 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("returns for [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..03c5718dc 100644 --- a/Sprint-1/implement/sum.js +++ b/Sprint-1/implement/sum.js @@ -1,4 +1,17 @@ function sum(elements) { + if (!Array.isArray(elements) || elements === "") { + throw new Error("sum requires an array of numbers"); + } + + if (elements.some((element) => typeof element === "string")) { + throw new Error("sum requires an array of numbers"); + } + let result = 0; + for (const element of elements) { + result += element; + } + + return result; } module.exports = sum; diff --git a/Sprint-1/implement/sum.test.js b/Sprint-1/implement/sum.test.js index a288e0f0a..3298d4896 100644 --- a/Sprint-1/implement/sum.test.js +++ b/Sprint-1/implement/sum.test.js @@ -13,24 +13,57 @@ 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("returns for []", () => { + 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 for [8]", () => { + expect(sum([8])).toEqual(8); + }); + // Given an array containing negative numbers + // When passed to the sum function + // Then it should still return the correct total sum + it("returns for [8, -5, 10]", () => { + expect(sum([8, -5, 10])).toEqual(13); + }); -// Given an array with just one number -// When passed to the sum function -// Then it should return that number + // Given an array with decimal/float numbers + // When passed to the sum function + // Then it should return the correct total sum + it("returns for [10, 3.75, 2.25]", () => { + expect(sum([10, 3.75, 2.25])).toEqual(16); + }); + // 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 negative numbers -// When passed to the sum function -// Then it should still return the correct total sum + it("returns for [10, 20, '30']", () => { + () => + expect( + sum([10, 20, "30"]).toThrow( + new Error("sum requires an array of numbers") + ) + ); + }); -// Given an array with decimal/float numbers -// When passed to the sum function -// Then it should return the correct total sum + // 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("returns for 'hey'", () => { + () => + expect(sum("hey")).toThrow(new 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("returns for (42)", () => { + () => + expect(sum(42)).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("returns for ('')", () => { + () => + expect(sum("")).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..6aeac0b5d 100644 --- a/Sprint-1/refactor/includes.js +++ b/Sprint-1/refactor/includes.js @@ -1,9 +1,8 @@ // 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]; - if (element === target) { + for (const item of list) { + if (item === target) { return true; } }