diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index 5c5b796e1..beeebd932 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) { + 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) { + throw new Error("calculateMedian requires an array of numbers"); + } + for (const item of list) { + if (typeof item !== "number") { + throw new Error("calculateMedian requires an array of numbers"); + } + } const middleIndex = Math.floor(list.length / 2); - const median = list.splice(middleIndex, 1)[0]; - return median; + const sortedList = [...list].sort((a, b) => a - b); + if (list.length % 2 === 0) { + const median = (sortedList[middleIndex] + sortedList[middleIndex - 1]) / 2; + return median; + } else { + //const median = sortedList.splice(middleIndex, 1)[0]; + return sortedList[middleIndex]; + } } module.exports = calculateMedian; diff --git a/Sprint-1/implement/dedupe.js b/Sprint-1/implement/dedupe.js index 781e8718a..4cd6d23e7 100644 --- a/Sprint-1/implement/dedupe.js +++ b/Sprint-1/implement/dedupe.js @@ -1 +1,12 @@ -function dedupe() {} +function dedupe(array) { + const withOutDup = []; + for (let i = 0; i < array.length; i++) { + const item = array[i]; + if (!withOutDup.includes(item)) { + withOutDup.push(item); + } + } + + return withOutDup; +} +module.exports = dedupe; diff --git a/Sprint-1/implement/dedupe.test.js b/Sprint-1/implement/dedupe.test.js index d7c8e3d8e..6ead2f900 100644 --- a/Sprint-1/implement/dedupe.test.js +++ b/Sprint-1/implement/dedupe.test.js @@ -16,13 +16,44 @@ 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"); +test("when passed an emptyarray", () => { + const array = []; + dedupe(array); + expect(dedupe(array)).toEqual([]); +}); // Given an array with no duplicates // When passed to the dedupe function // Then it should return a copy of the original array +test("when passed an array with no duplicate", () => { + const array = [1, 2, 3, 4]; + dedupe(array); + expect(dedupe(array)).toEqual([1, 2, 3, 4]); +}); // 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 +// Then it should return a new array with duplicates removed while preserving the // first occurrence of each element from the original array. +test("when passed an array of duplicate numbers", () => { + expect(dedupe([1, 1, 2])).toEqual([1, 2]); + expect(dedupe([1, 1, 2, 2, 2, 3])).toEqual([1, 2, 3]); + expect(dedupe([1, 1, 1, 3, 3, 4, 4])).toEqual([1, 3, 4]); +}); + +test("when passed an array of duplicate characters", () => { + expect(dedupe(["a", "a", "b", "b", "b", "c"])).toEqual(["a", "b", "c"]); + expect(dedupe(["c", "c", "e", "e", "z", "z"])).toEqual(["c", "e", "z"]); +}); +test("when passed an array of duplicate unordered numbers ", () => { + expect(dedupe([5, 1, 1, 2, 3, 2, 5, 8])).toEqual([5, 1, 2, 3, 8]); +}); +test("when passed an array of duplicate unordered characters ", () => { + expect(dedupe(["f", "a", "a", "k", "k", "c", "c"])).toEqual([ + "f", + "a", + "k", + "c", + ]); +}); diff --git a/Sprint-1/implement/describe-median.js b/Sprint-1/implement/describe-median.js index 3381cfc0a..d21712eec 100644 --- a/Sprint-1/implement/describe-median.js +++ b/Sprint-1/implement/describe-median.js @@ -20,6 +20,15 @@ function calculateMedian(list) { } // Implement this function. See describe-median.test.js for the acceptance criteria. -function describeMedian(list) {} +function describeMedian(list) { + try { + let result = calculateMedian(list); + return result; + } catch (error) { + // console.error(`Could not calculate a median:${error.message}`); + return `Could not calculate a median:${error.message}`; + //throw error; + } +} module.exports = describeMedian; diff --git a/Sprint-1/implement/describe-median.test.js b/Sprint-1/implement/describe-median.test.js index e7d18a264..6acf2be0e 100644 --- a/Sprint-1/implement/describe-median.test.js +++ b/Sprint-1/implement/describe-median.test.js @@ -21,12 +21,33 @@ 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("returns the correct median when given [1,2,3]", () => { + expect(describeMedian([1, 2, 3])).toEqual(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("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" +test("returns an error message when given a string of array", () => { + expect(describeMedian(["banana"])).toEqual( + "Could not calculate a median:calculateMedian requires an array of numbers" + ); +}); +test("return an error message when given a number", () => { + expect(describeMedian(123)).toEqual( + "Could not calculate a median:calculateMedian requires an array of numbers" + ); +}); +test("return an error message when given list of strings of array", () => { + expect(describeMedian(["one", "two", "three"])).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..251ff4cd6 100644 --- a/Sprint-1/implement/max.js +++ b/Sprint-1/implement/max.js @@ -1,4 +1,13 @@ function findMax(elements) { + if (!Array.isArray(elements)) { + throw new Error("findMax requires an array of numbers"); + } + for (let num of elements) { + if (typeof num !== "number") { + 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..7f595b755 100644 --- a/Sprint-1/implement/max.test.js +++ b/Sprint-1/implement/max.test.js @@ -16,28 +16,81 @@ 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("return -Infinity when given an Empty array", () => { + expect(findMax([])).toEqual(-Infinity); +}); // Given an array with one number // When passed to the max function // Then it should return that number +test("return the number when given one number", () => { + expect(findMax([10])).toEqual(10); + expect(findMax([1])).toEqual(1); +}); // Given an array with both positive and negative numbers // When passed to the max function // Then it should return the largest number overall +test("return the largest number when given negative and posetive numbers", () => { + expect(findMax([10, -10, -3, 4])).toEqual(10); + expect(findMax([-10, 2, -5, 6, 9])).toEqual(9); +}); // Given an array with just negative numbers // When passed to the max function // Then it should return the closest one to zero +test("returns closest one to zero when given array of negative numbers", () => { + expect(findMax([-1, -5, -2, -10])).toEqual(-1); + expect(findMax([-5, -10, -100, -20])).toEqual(-5); +}); // Given an array with decimal numbers // When passed to the max function // Then it should return the largest decimal number +test("return the largest decimal number when given decimal numbers in an array", () => { + expect(findMax([0.01, 0.02, 2.5, 2.9])).toEqual(2.9); + expect(findMax([3.5, 2.5, 2.9])).toEqual(3.5); + expect(findMax([10.1, 9, 3, 11])).toEqual(11); +}); // 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("return error when given an array containg non number value", () => { + expect(() => findMax(["a", 2, "c"])).toThrow( + "findMax requires an array of numbers" + ); +}); +test("return error when given an array containg non number value", () => { + expect(() => findMax(["a", "b", "c"])).toThrow( + "findMax requires an array of numbers" + ); +}); +test("return error when given an array containg non number value", () => { + expect(() => findMax([1, "hallo", 2])).toThrow( + "findMax requires an array of numbers" + ); +}); +test("return error when given an array containg non number value", () => { + expect(() => findMax(["a", , "c"])).toThrow( + "findMax requires an array of numbers" + ); +}); +test("return error when given an array containg non number value", () => { + expect(() => findMax([1, , 2])).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("return error when given non array values", () => { + expect(() => findMax("hey")).toThrow("findMax requires an array of numbers"); +}); +test("return error when given non array values that is number", () => { + expect(() => findMax(42)).toThrow("findMax requires an array of numbers"); +}); +test("return error when given no argument", () => { + expect(() => findMax()).toThrow("findMax requires an array of numbers"); +}); diff --git a/Sprint-1/implement/mean.js b/Sprint-1/implement/mean.js index 36909ea1f..2e14ef629 100644 --- a/Sprint-1/implement/mean.js +++ b/Sprint-1/implement/mean.js @@ -1,3 +1,20 @@ -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 item of list) { + if (typeof item !== "number") { + throw new Error("calculateMean requires an array of numbers"); + } + } + let total = 0; + for (const num of list) { + total += num; + } + return (mean = total / list.length); +} module.exports = calculateMean; diff --git a/Sprint-1/implement/mean.test.js b/Sprint-1/implement/mean.test.js index 847b88676..f4b731c9f 100644 --- a/Sprint-1/implement/mean.test.js +++ b/Sprint-1/implement/mean.test.js @@ -23,28 +23,75 @@ 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("returns calculated mean when given array of numbers", () => { + 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("returns calculated mean when given array of numbers", () => { + expect(calculateMean([1])).toEqual(1); + expect(calculateMean([10])).toEqual(10); +}); // Given an array containing negative or decimal numbers // When passed to calculateMean // Then it should return the correct mean +test("returns calculated mean when given array of +ve and -ve numbers and decimal numbers are given", () => { + expect(calculateMean([5, -2, -3, 4])).toEqual(1); + expect(calculateMean([-2, -1, 5])).toEqual(0.6666666666666666); + expect(calculateMean([1.5, 2.4, 5.6])).toEqual(3.1666666666666665); +}); // Given an empty array // When passed to calculateMean // Then it should throw Error("calculateMean requires a non-empty array") +test("returns an error when given an empty array", () => { + 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("throws an error when given an empty array", () => { + expect(() => calculateMean("banana")).toThrow( + "calculateMean requires an array of numbers" + ); +}); +test("throws an error when given a number", () => { + expect(() => calculateMean(42)).toThrow( + "calculateMean requires an array of numbers" + ); +}); +test("throws an error when given a null", () => { + expect(() => calculateMean(null)).toThrow( + "calculateMean requires an array of numbers" + ); +}); +test("throws an error when given a {}", () => { + expect(() => calculateMean({})).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") +test("throws an error when given no argument", () => { + 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("throws an error when given an array containing a non number value", () => { + 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..3cbe2891b 100644 --- a/Sprint-1/implement/sum.js +++ b/Sprint-1/implement/sum.js @@ -1,4 +1,17 @@ function sum(elements) { + if (!Array.isArray(elements)) { + throw new Error("sum requires an array of numbers"); + } + for (const num of elements) { + if (typeof num !== "number") { + throw new Error("sum requires an array of numbers"); + } + } + let total = 0; + for (const num of elements) { + total += num; + } + return total; } module.exports = sum; diff --git a/Sprint-1/implement/sum.test.js b/Sprint-1/implement/sum.test.js index a288e0f0a..dfa22cc7d 100644 --- a/Sprint-1/implement/sum.test.js +++ b/Sprint-1/implement/sum.test.js @@ -13,24 +13,54 @@ 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("should return 0 when given an empty array", () => { + expect(sum([])).toEqual(0); +}); // Given an array with just one number // When passed to the sum function // Then it should return that number - +test("should return the number when given one number", () => { + expect(sum([10])).toEqual(10); + expect(sum([-1])).toEqual(-1); + expect(sum([0])).toEqual(0); +}); // Given an array containing negative numbers // When passed to the sum function // Then it should still return the correct total sum +test("should return the number when given negative numbers ", () => { + expect(sum([10, -4, 5, 7])).toEqual(18); + expect(sum([-3, -4, -6, -9])).toEqual(-22); + expect(sum([7, -9, 2, 1])).toEqual(1); +}); // Given an array with decimal/float numbers // When passed to the sum function // Then it should return the correct total sum +test("should return the number when given decimal number", () => { + expect(sum([2.5, 3.4, 5.5, 2])).toEqual(13.4); + expect(sum([1, 1.5, 2.5, 6.3])).toEqual(11.3); + expect(sum([2.3, 2.7, 7.4, 5.5])).toEqual(17.9); +}); // 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("should throw an error when argument is passed that is non number", () => { + expect(() => sum([1, 2, "hi", 3, "hey"])).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("should throw an error when argument 'hey'is passed", () => { + expect(() => sum("hey")).toThrow("sum requires an array of numbers"); +}); +test("should throw an error when argument 42 is passed", () => { + expect(() => sum(42)).toThrow("sum requires an array of numbers"); +}); +test("should throw an error when no argument is passed", () => { + expect(() => sum()).toThrow("sum requires an array of numbers"); +}); diff --git a/Sprint-1/refactor/includes.js b/Sprint-1/refactor/includes.js index 29dad81f0..e78b21d70 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 (let num of list) { + if (num === target) { return true; } }