diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index b22590bc6..0bd092a95 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -6,9 +6,27 @@ // 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' isn't an array + if (!Array.isArray(list)) return null; + + const numbers = list.filter(Number.isFinite); + + if (numbers.length === 0) return null; + if (numbers.length === 1) return numbers[0]; + + + // using 'spread operator' copies the array without mutating it + numbers.sort((a, b) => a - b); + + const middleIndex = Math.floor(numbers.length / 2); + + if (numbers.length % 2 === 0) { + return (numbers[middleIndex - 1] + numbers[middleIndex]) / 2; + } else { + return numbers[middleIndex] + } + } module.exports = calculateMedian; diff --git a/Sprint-1/implement/dedupe.js b/Sprint-1/implement/dedupe.js index 781e8718a..b6db3188a 100644 --- a/Sprint-1/implement/dedupe.js +++ b/Sprint-1/implement/dedupe.js @@ -1 +1,15 @@ -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; + + return [...new Set(array)]; +} + +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..e014510d9 100644 --- a/Sprint-1/implement/dedupe.test.js +++ b/Sprint-1/implement/dedupe.test.js @@ -16,12 +16,27 @@ 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", () => { + const input = [1, 3, 5, 7]; + const result = dedupe(input); + + expect(result).toEqual([1, 3, 5, 7]); + expect(result).not.toBe([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..adb83b4db 100644 --- a/Sprint-1/implement/max.js +++ b/Sprint-1/implement/max.js @@ -1,4 +1,11 @@ function findMax(elements) { + const numbers = elements.filter(Number.isFinite); + + if (numbers.length == 0) { + return -Infinity; + } + + return Math.max(...numbers); } module.exports = findMax; diff --git a/Sprint-1/implement/max.test.js b/Sprint-1/implement/max.test.js index 82f18fd88..097d79edf 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, "400", "2", "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..fafb010c1 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(Number.isFinite).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..141cce0d6 100644 --- a/Sprint-1/implement/sum.test.js +++ b/Sprint-1/implement/sum.test.js @@ -13,24 +13,50 @@ 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])).toBeCloseTo(0.8); + expect(sum([1.2, 0.6, 0.005])).toBeCloseTo(1.805); +}) // 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..01a625aea 100644 --- a/Sprint-1/stretch/aoc-2018-day1/solution.js +++ b/Sprint-1/stretch/aoc-2018-day1/solution.js @@ -0,0 +1,12 @@ +const fs = require('fs'); + +const data = fs.readFileSync('input.txt', 'utf-8'); + +// split by spaces or new lines +// filter out empty values +// convert strings into numbers +const numbers = data.split(/\s+/).filter(Boolean).map(Number); + +const sum = numbers.reduce((a, b) => a + b, 0); + +console.log(sum); // 529 \ No newline at end of file