diff --git a/Sprint-1/fix/median.test.js b/Sprint-1/fix/median.test.js index 51d61cc86..c77096371 100644 --- a/Sprint-1/fix/median.test.js +++ b/Sprint-1/fix/median.test.js @@ -6,113 +6,82 @@ const calculateMedian = require("./median.js"); -describe("calculateMedian", () => { - it("returns the median for [1, 2, 3]", () => { - expect(calculateMedian([1, 2, 3])).toEqual(2); - }); - - it("returns the median for [1, 2, 3, 4, 5]", () => { - expect(calculateMedian([1, 2, 3, 4, 5])).toEqual(3); - }); - - it("returns the median for [1, 2, 3, 4]", () => { - expect(calculateMedian([1, 2, 3, 4])).toEqual(2.5); - }); - - it("returns the median for [1, 2, 3, 4, 5, 6]", () => { - expect(calculateMedian([1, 2, 3, 4, 5, 6])).toEqual(3.5); - }); - - it("returns the correct median for unsorted array [3, 1, 2]", () => { - expect(calculateMedian([3, 1, 2])).toEqual(2); - }); - - it("returns the correct median for unsorted array [5, 1, 3, 4, 2]", () => { - expect(calculateMedian([5, 1, 3, 4, 2])).toEqual(3); - }); - - it("returns the correct median for unsorted array [4, 2, 1, 3]", () => { - expect(calculateMedian([4, 2, 1, 3])).toEqual(2.5); - }); +// describe("calculateMedian", () => { +// [ +// { input: [1, 2, 3], expected: 2 }, +// { input: [1, 2, 3, 4, 5], expected: 3 }, +// { input: [1, 2, 3, 4], expected: 2.5 }, +// { input: [1, 2, 3, 4, 5, 6], expected: 3.5 }, +// ].forEach(({ input, expected }) => +// it(`returns the median for [${input}]`, () => expect(calculateMedian(input)).toEqual(expected)) +// ); + +// [ +// { input: [3, 1, 2], expected: 2 }, +// { input: [5, 1, 3, 4, 2], expected: 3 }, +// { input: [4, 2, 1, 3], expected: 2.5 }, +// { input: [6, 1, 5, 3, 2, 4], expected: 3.5 }, +// { input: [110, 20, 0], expected: 20 }, +// { input: [6, -2, 2, 12, 14], expected: 6 }, +// ].forEach(({ input, expected }) => +// it(`returns the correct median for unsorted array [${input}]`, () => expect(calculateMedian(input)).toEqual(expected)) +// ); + +// it("doesn't modify the input array [3, 1, 2]", () => { +// const list = [3, 1, 2]; +// calculateMedian(list); +// expect(list).toEqual([3, 1, 2]); +// }); + +// [ 'not an array', 123, null, undefined, {}, [], ["apple", null, undefined] ].forEach(val => +// it(`returns null for non-numeric array (${val})`, () => expect(calculateMedian(val)).toBe(null)) +// ); + +// [ +// { input: [1, 2, "3", null, undefined, 4], expected: 2 }, +// { input: ["apple", 1, 2, 3, "banana", 4], expected: 2.5 }, +// { input: [1, "2", 3, "4", 5], expected: 3 }, +// { input: [1, "apple", 2, null, 3, undefined, 4], expected: 2.5 }, +// { input: [3, "apple", 1, null, 2, undefined, 4], expected: 2.5 }, +// { input: ["banana", 5, 3, "apple", 1, 4, 2], expected: 3 }, +// ].forEach(({ input, expected }) => +// it(`filters out non-numeric values and calculates the median for [${input}]`, () => expect(calculateMedian(input)).toEqual(expected)) +// ); +// }); - it("returns the correct median for unsorted array [6, 1, 5, 3, 2, 4]", () => { - expect(calculateMedian([6, 1, 5, 3, 2, 4])).toEqual(3.5); - }); - - it("returns the correct median for unsorted array [110, 20, 0]", () => { - expect(calculateMedian([110, 20, 0])).toEqual(20); - }); - - it("returns the correct median for unsorted array [6, -2, 2, 12, 14]", () => { - expect(calculateMedian([6, -2, 2, 12, 14])).toEqual(6); - }); - - it("doesn't modify the input array [3, 1, 2]", () => { - const list = [3, 1, 2]; - calculateMedian(list); - expect(list).toEqual([3, 1, 2]); - }); - - // There is no median of an empty array, so calculateMedian should throw - it("throws when given an empty array", () => { - expect(() => calculateMedian([])).toThrow( - new Error("calculateMedian requires a non-empty array") - ); - }); - - // Input that isn't an array should throw - it("throws when given a string", () => { - expect(() => calculateMedian("banana")).toThrow( - new Error("calculateMedian requires an array of numbers") - ); - }); - - it("throws when given a number", () => { - expect(() => calculateMedian(123)).toThrow( - new Error("calculateMedian requires an array of numbers") - ); +describe("calculateMedian", () => { + test("returns null for an empty array", () => { + expect(calculateMedian([])).toBeNull(); }); - it("throws when given null", () => { - expect(() => calculateMedian(null)).toThrow( - new Error("calculateMedian requires an array of numbers") - ); + test("returns null when there are no numbers", () => { + expect(calculateMedian(["a", "b", null])).toBeNull(); }); - it("throws when given an object", () => { - expect(() => calculateMedian({})).toThrow( - new Error("calculateMedian requires an array of numbers") - ); + test("returns the median for an odd number of elements", () => { + expect(calculateMedian([1, 2, 3])).toEqual(2); + expect(calculateMedian([5, 1, 3])).toEqual(3); }); - it("throws when called with no argument", () => { - expect(() => calculateMedian()).toThrow( - new Error("calculateMedian requires an array of numbers") - ); + test("returns the median for an even number of elements", () => { + expect(calculateMedian([1, 2, 3, 4])).toEqual(2.5); + expect(calculateMedian([6, 2, 4, 8])).toEqual(5); }); - // Arrays containing any non-number value should throw, rather than filtering them out - it("throws for an array of strings", () => { - expect(() => calculateMedian(["ten", "twenty", "thirty"])).toThrow( - new Error("calculateMedian requires an array of numbers") - ); + test("ignores non-number values", () => { + expect(calculateMedian([1, "a", 2, 3])).toEqual(2); + expect(calculateMedian([5, "x", 1, 3])).toEqual(3); }); - it("throws for an array mixing numbers and strings", () => { - expect(() => calculateMedian([1, "2", 3, "4", 5])).toThrow( - new Error("calculateMedian requires an array of numbers") - ); + test("ignores NaN values", () => { + expect(calculateMedian([1, NaN, 2, 3])).toEqual(2); }); - it("throws for an array containing null", () => { - expect(() => calculateMedian([1, 2, null, 4])).toThrow( - new Error("calculateMedian requires an array of numbers") - ); + test("returns the single number when the array has one number", () => { + expect(calculateMedian([7])).toEqual(7); }); - it("throws for an array containing undefined", () => { - expect(() => calculateMedian([3, 1, undefined, 2])).toThrow( - new Error("calculateMedian requires an array of numbers") - ); + test("returns the single number when mixed with non-numbers", () => { + expect(calculateMedian(["a", 7, null])).toEqual(7); }); }); diff --git a/Sprint-1/implement/dedupe.js b/Sprint-1/implement/dedupe.js index 781e8718a..884881c16 100644 --- a/Sprint-1/implement/dedupe.js +++ b/Sprint-1/implement/dedupe.js @@ -1 +1,5 @@ -function dedupe() {} +function dedupe(arr) { + return [...new Set(arr)]; +} + +module.exports = dedupe; diff --git a/Sprint-1/implement/dedupe.test.js b/Sprint-1/implement/dedupe.test.js index d7c8e3d8e..fcff895ca 100644 --- a/Sprint-1/implement/dedupe.test.js +++ b/Sprint-1/implement/dedupe.test.js @@ -1,4 +1,3 @@ -const dedupe = require("./dedupe.js"); /* Dedupe Array @@ -16,13 +15,41 @@ 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"); +//test.todo("given an empty array, it returns an empty array"); + +const dedupe = require("./dedupe"); + +// Given an empty array +// When passed to the dedupe function +// Then it should return an empty array +test("should return an 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 +test("should return a copy of the original array when there are no duplicates", () => { + expect(dedupe([1, 2, 3, 4])).toEqual([1, 2, 3, 4]); +}); + +// Given an array of strings with duplicates +// When passed to the dedupe function +// Then it should remove duplicate strings while preserving the first occurrence +test("should remove duplicate strings while preserving the first occurrence", () => { + expect(dedupe(["a", "a", "a", "b", "b", "c"])).toEqual(["a", "b", "c"]); +}); + +// Given an array of numbers with duplicates +// When passed to the dedupe function +// Then it should remove duplicate numbers while preserving the first occurrence +test("should remove duplicate numbers while preserving the first occurrence", () => { + expect(dedupe([5, 1, 1, 2, 3, 2, 5, 8])).toEqual([5, 1, 2, 3, 8]); +}); -// Given an array of strings or numbers +// Given an array with one element // 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. +// Then it should return the same array +test("should return the same array when it contains only one element", () => { + expect(dedupe([42])).toEqual([42]); +}); diff --git a/Sprint-1/implement/max.js b/Sprint-1/implement/max.js index 6dd76378e..1877434f7 100644 --- a/Sprint-1/implement/max.js +++ b/Sprint-1/implement/max.js @@ -1,4 +1,13 @@ -function findMax(elements) { +function findMax(list) { + if (!Array.isArray(list)) { + throw new Error("findMax requires an array of numbers"); + } + + if (!list.every((item) => typeof item === "number" && !Number.isNaN(item))) { + throw new Error("findMax requires an array of numbers"); + } + + return Math.max(...list); } module.exports = findMax; diff --git a/Sprint-1/implement/max.test.js b/Sprint-1/implement/max.test.js index ff3fbb5e5..ad8ff0e0e 100644 --- a/Sprint-1/implement/max.test.js +++ b/Sprint-1/implement/max.test.js @@ -1,11 +1,11 @@ -/* Find the maximum element of an array of numbers +/* Find the findMaximum element of an array of numbers In this kata, you will need to implement a function that find the largest numerical element of an array. -E.g. max([30, 50, 10, 40]), target output: 50 -E.g. max(['hey', 10, 'hi', 60, 10]) throws Error("findMax requires an array of numbers") (max can't compare non-numerical elements, so it shouldn't guess) +E.g. findMax([30, 50, 10, 40]), target output: 50 +E.g. findMax(['hey', 10, 'hi', 60, 10]), target output: 60 (sum ignores any non-numerical elements) -You should implement this function in max.js, and add tests for it in this file. +You should implement this function in findMax.js, and add tests for it in this file. We have set things up already so that this file can see your function from the other file. */ @@ -13,31 +13,77 @@ 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 +// When passed to the findMax 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("returns -Infinity for an empty array", () => { + const currentOutput = findMax([]); + const targetOutput = -Infinity; + + expect(currentOutput).toEqual(targetOutput); +}); // Given an array with one number -// When passed to the max function +// When passed to the findMax function // Then it should return that number +test("returns the number when the array contains one number", () => { + const currentOutput = findMax([3]); + const targetOutput = 3; + + expect(currentOutput).toEqual(targetOutput); +}); // Given an array with both positive and negative numbers -// When passed to the max function +// When passed to the findMax function // Then it should return the largest number overall +test("returns the largest number", () => { + const currentOutput = findMax([3, -10]); + const targetOutput = 3; + + expect(currentOutput).toEqual(targetOutput); +}); // Given an array with just negative numbers -// When passed to the max function +// When passed to the findMax function // Then it should return the closest one to zero +test("returns the number closest to zero", () => { + const currentOutput = findMax([-7, -3, -10]); + const targetOutput = -3; + + expect(currentOutput).toEqual(targetOutput); +}); // Given an array with decimal numbers -// When passed to the max function +// When passed to the findMax function // Then it should return the largest decimal number +test("returns the largest decimal number", () => { + const currentOutput = findMax([2.5, 5.6]); + const targetOutput = 5.6; + + expect(currentOutput).toEqual(targetOutput); +}); // Given an array containing a value that isn't a number -// When passed to the max function +// When passed to the findMax function // Then it should throw Error("findMax requires an array of numbers") +test("throws an error when the array contains a non-number", () => { + expect(() => findMax(["hey", 10, "hi", 60, 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 +// Given something that isn't an array at all +// When passed to the findMax function // Then it should throw Error("findMax requires an array of numbers") +test("throws an error when the input is not 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") + ); +}); diff --git a/Sprint-1/implement/sum.js b/Sprint-1/implement/sum.js index 9062aafe3..a19b87715 100644 --- a/Sprint-1/implement/sum.js +++ b/Sprint-1/implement/sum.js @@ -1,4 +1,13 @@ -function sum(elements) { +function sum(list) { + if (!Array.isArray(list)) { + throw new Error("sum requires an array of numbers"); + } + + if (!list.every((item) => typeof item === "number" && !Number.isNaN(item))) { + throw new Error("sum requires an array of numbers"); + } + + return list.reduce((total, number) => total + number, 0); } module.exports = sum; diff --git a/Sprint-1/implement/sum.test.js b/Sprint-1/implement/sum.test.js index a288e0f0a..ca1f80285 100644 --- a/Sprint-1/implement/sum.test.js +++ b/Sprint-1/implement/sum.test.js @@ -1,36 +1,67 @@ -/* Sum the numbers in an array - -In this kata, you will need to implement a function that sums the numerical elements of an array - -E.g. sum([10, 20, 30]), target output: 60 -E.g. sum(['hey', 10, 'hi', 60, 10]) throws Error("sum requires an array of numbers") (sum can't add non-numerical elements, so it shouldn't guess) -*/ - 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") +test("returns 0 for an empty array", () => { + const list = []; + const currentOutput = sum(list); + const targetOutput = 0; + + expect(currentOutput).toEqual(targetOutput); +}); // Given an array with just one number // When passed to the sum function // Then it should return that number +test("returns the number when the array contains one number", () => { + const list = [1]; + const currentOutput = sum(list); + const targetOutput = 1; + + expect(currentOutput).toEqual(targetOutput); +}); // Given an array containing negative numbers // When passed to the sum function // Then it should still return the correct total sum +test("returns the correct sum with negative numbers", () => { + const list = [-5, -10, 3]; + const currentOutput = sum(list); + const targetOutput = -12; + + expect(currentOutput).toEqual(targetOutput); +}); // Given an array with decimal/float numbers // When passed to the sum function // Then it should return the correct total sum +test("returns the correct sum with decimal numbers", () => { + const list = [2.5, 3.5, 4]; + const currentOutput = sum(list); + const targetOutput = 10; + + expect(currentOutput).toEqual(targetOutput); +}); // 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("throws an error when the array contains a non-number", () => { + expect(() => sum(["hey", 10, "hi", 60, 10])).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 +// Given something that isn't an array at all // When passed to the sum function // Then it should throw Error("sum requires an array of numbers") +test("throws an error when the input is not an array", () => { + expect(() => sum("hey")).toThrow( + new Error("sum requires an array of numbers") + ); + + expect(() => sum(42)).toThrow(new Error("sum requires an array of numbers")); + + expect(() => sum()).toThrow(new Error("sum requires an array of numbers")); +});