diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index b22590bc6..ccbf9717a 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -6,9 +6,28 @@ // or 'list' has mixed values (the function is expected to sort only numbers). function calculateMedian(list) { - const middleIndex = Math.floor(list.length / 2); - const median = list.splice(middleIndex, 1)[0]; - return median; + + // first of all, checking if 'list' is an array + if (Array.isArray(list)) { + + const filteredArr = list.filter(x => typeof x === 'number'); + + if (filteredArr.length < 2) { + return null; + } + + // using 'spread operator' copies the array without mutating it + const sortedList = [...filteredArr].sort((a, b) => a - b); + + const middleIndex = Math.floor(sortedList.length / 2); + + if (sortedList.length % 2 === 0) { + return (sortedList[middleIndex - 1] + sortedList[middleIndex]) / 2; + } else { + return sortedList[middleIndex] + } + } + return null; } module.exports = calculateMedian; diff --git a/Sprint-1/implement/dedupe.js b/Sprint-1/implement/dedupe.js index 781e8718a..f47d5ce9c 100644 --- a/Sprint-1/implement/dedupe.js +++ b/Sprint-1/implement/dedupe.js @@ -1 +1,13 @@ -function dedupe() {} +function dedupe(array) { + let arr = array.filter((item, index) => { + + // indexOf() returns the first index where that value appears in the array. + if (array.indexOf(item) === index) { + return item; + } + }) + + return arr; +} + +module.exports = dedupe; \ No newline at end of file diff --git a/Sprint-1/implement/dedupe.test.js b/Sprint-1/implement/dedupe.test.js index 23e0f8638..c05c8721b 100644 --- a/Sprint-1/implement/dedupe.test.js +++ b/Sprint-1/implement/dedupe.test.js @@ -16,12 +16,23 @@ E.g. dedupe([1, 2, 1]) target output: [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("return an empty array for 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("if no duplicates, return the copy of the original array", () => { + expect(dedupe([1, 3, 5, 7])).toEqual([1, 3, 5, 7]); +}) + // Given an array with strings or numbers // When passed to the dedupe function // Then it should remove the duplicate values, preserving the first occurence of each element + +test("an array with strings strings or numbers", () => { + expect(dedupe([2, 2, 3, 3, 3, "Black", "Black", "Black", "Green"])).toEqual([2, 3, "Black", "Green"]); +}) diff --git a/Sprint-1/implement/max.js b/Sprint-1/implement/max.js index 6dd76378e..7a5c0054e 100644 --- a/Sprint-1/implement/max.js +++ b/Sprint-1/implement/max.js @@ -1,4 +1,12 @@ function findMax(elements) { + const filteredArr = elements.filter(x => typeof x === 'number'); + + if (filteredArr.length == 0) { + return -Infinity; + } + + // using 'spread sytax' + return Math.max(...filteredArr); } module.exports = findMax; diff --git a/Sprint-1/implement/max.test.js b/Sprint-1/implement/max.test.js index 82f18fd88..7c2f86699 100644 --- a/Sprint-1/implement/max.test.js +++ b/Sprint-1/implement/max.test.js @@ -16,28 +16,58 @@ 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("returns -Infinity for 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("given an array with one number, should return that number", () => { + expect(findMax([8])).toEqual(8); + expect(findMax([-5])).toEqual(-5); + expect(findMax([0])).toEqual(0); +}) + // 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 overall", () => { + expect(findMax([-8, -4, 0, 4, 8])).toEqual(8); + expect(findMax([-3, -2, -1, 4, 2, 3])).toEqual(4); +}) + // Given an array with just negative numbers // When passed to the max function // Then it should return the closest one to zero +test("given an array with only negative numbers, should return closest to 0", () => { + expect(findMax([-2, -4, -1, -3, -100])).toEqual(-1); +}) + // Given an array with decimal numbers // When passed to the max function // Then it should return the largest decimal number +test("an array with decimal numbers, should return the largest decimal number", () => { + expect(findMax([0.1, 0.2, 0.9, 0.8, 0.3, 0.7, 0.4, 0.6])).toEqual(0.9); +}) + // Given an array with non-number values // When passed to the max function // Then it should return the max and ignore non-numeric values +test("ignore the non-numeric values", () => { + expect(findMax(["Blue", 3, "White", "Orange", "Pink"])).toEqual(3); +}) + // Given an array with only non-number values // When passed to the max function // Then it should return the least surprising value given how it behaves for all other inputs + +test("an array with only non-number values", () => { + expect(findMax(["Blue", "White", "Orange", "Pink"])).toEqual(-Infinity); +}) diff --git a/Sprint-1/implement/sum.js b/Sprint-1/implement/sum.js index 9062aafe3..51a3a0a9e 100644 --- a/Sprint-1/implement/sum.js +++ b/Sprint-1/implement/sum.js @@ -1,4 +1,9 @@ function sum(elements) { + + // const filteredArr = elements.filter(x => typeof x === 'number'); + // return filteredArr.reduce((a, b) => a + b, 0); + + return elements.filter(x => typeof x === 'number').reduce((a, b) => a + b, 0); } module.exports = sum; diff --git a/Sprint-1/implement/sum.test.js b/Sprint-1/implement/sum.test.js index dd0a090ca..a6993d202 100644 --- a/Sprint-1/implement/sum.test.js +++ b/Sprint-1/implement/sum.test.js @@ -13,24 +13,49 @@ 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("return 0 for 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("given an array with just one number, return that number", () => { + expect(sum([3])).toEqual(3); + expect(sum([-3])).toEqual(-3); + 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("return the correct total when passed negative numbers", () => { + expect(sum([-3, -6, -1])).toEqual(-10); +}) + // Given an array with decimal/float numbers // When passed to the sum function // Then it should return the correct total sum +test("decimal number arrays", () => { + expect(sum([0.2, 0.4, 0.2])).toEqual(0.8); +}) // Given an array containing non-number values // When passed to the sum function // Then it should ignore the non-numerical values and return the sum of the numerical elements +test("an array with non-numbers", () => { + expect(sum([2, "Blue", 3, "Black", "Green"])).toEqual(5); +}) + // Given an array with only non-number values // When passed to the sum function // Then it should return the least surprising value given how it behaves for all other inputs + +test("an array with non-number values only", () => { + expect(sum([true, false, "Black"])).toEqual(0); + expect(sum([undefined, null, "Black"])).toEqual(0); +}) diff --git a/Sprint-1/refactor/includes.js b/Sprint-1/refactor/includes.js index 29dad81f0..2e1678917 100644 --- a/Sprint-1/refactor/includes.js +++ b/Sprint-1/refactor/includes.js @@ -1,8 +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]; + + for (const element of list) { if (element === target) { return true; } diff --git a/Sprint-1/stretch/aoc-2018-day1/solution.js b/Sprint-1/stretch/aoc-2018-day1/solution.js index e69de29bb..feb9593b5 100644 --- a/Sprint-1/stretch/aoc-2018-day1/solution.js +++ b/Sprint-1/stretch/aoc-2018-day1/solution.js @@ -0,0 +1,3 @@ +function sum(list) { + return list.reduce((a, b) => a + b, 0); +} \ No newline at end of file