diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index b22590bc6..71aa3115e 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -6,9 +6,23 @@ // 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; + // return null if it is not an array + if (!Array.isArray(list)) return null; + + // filter only numbers from array and sort the filtered array. If no numbers return null + const numbers = list + .filter((item) => typeof item === "number") + .sort((a, b) => a - b); + if (numbers.length === 0) return null; + + // get numbers array mid point + const middleIndex = Math.floor(numbers.length / 2); + // check if numbers array length is even + const isEven = numbers.length % 2 === 0; + // calculate median based on whether the numbers array length is even or odd + return isEven + ? (median = (numbers[middleIndex - 1] + numbers[middleIndex]) / 2) + : numbers[middleIndex]; } module.exports = calculateMedian; diff --git a/Sprint-1/implement/dedupe.js b/Sprint-1/implement/dedupe.js index 781e8718a..921c7e6e4 100644 --- a/Sprint-1/implement/dedupe.js +++ b/Sprint-1/implement/dedupe.js @@ -1 +1,7 @@ -function dedupe() {} +function dedupe(array) { + // return empty array if empty input + if (array.length === 0) return []; + return Array.from(new Set(array)); +} + +module.exports = dedupe; diff --git a/Sprint-1/implement/dedupe.test.js b/Sprint-1/implement/dedupe.test.js index d7c8e3d8e..03d21bfe1 100644 --- a/Sprint-1/implement/dedupe.test.js +++ b/Sprint-1/implement/dedupe.test.js @@ -16,13 +16,29 @@ 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("given an empty array, it returns 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("given an array with no duplicates, it returns the original array", () => { + expect(dedupe([4, 5, 6])).toEqual([4, 5, 6]); +}); // 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("given a number array with duplicates, it returns a new array without duplicates", () => { + expect(dedupe([1, 2, 3, 2, 3])).toEqual([1, 2, 3]); +}); + +test("given a string array with duplicates, it returns a new array without duplicates", () => { + expect(dedupe(["a", "a", "b", "c"])).toEqual(["a", "b", "c"]); +}); + +test("given a mixed array with duplicates, it returns a new array without duplicates", () => { + expect(dedupe([1, "a", "a", "b", "c"])).toEqual([1, "a", "b", "c"]); +}); diff --git a/Sprint-1/implement/max.js b/Sprint-1/implement/max.js index 6dd76378e..5d81cb063 100644 --- a/Sprint-1/implement/max.js +++ b/Sprint-1/implement/max.js @@ -1,4 +1,8 @@ function findMax(elements) { + // filter to return only numbers or an empty array + const numbers = elements.filter((item) => Number.isFinite(item)); + // if numbers present in array return largest number or return -Infinity for empty array + return numbers.length ? Math.max(...numbers) : -Infinity; } module.exports = findMax; diff --git a/Sprint-1/implement/max.test.js b/Sprint-1/implement/max.test.js index 82f18fd88..c424243f9 100644 --- a/Sprint-1/implement/max.test.js +++ b/Sprint-1/implement/max.test.js @@ -16,28 +16,45 @@ 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("given an empty array, returns -Infinity", () => { + expect(findMax([])).toBe(-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, returns that number", () => { + expect(findMax([2])).toBe(2); +}); // Given an array with both positive and negative numbers // When passed to the max function // Then it should return the largest number overall - +test("given an array with positive and negative numbers, returns largest overall number", () => { + expect(findMax([5, -3, 15])).toBe(15); +}); // 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 negative numbers, returns number closest to zero", () => { + expect(findMax([-5, -3, -15])).toBe(-3); +}); // Given an array with decimal numbers // When passed to the max function // Then it should return the largest decimal number +test("given an array with decimal numbers, returns largest decimal number", () => { + expect(findMax([10.5, 2.6, 8.1])).toBe(10.5); +}); // 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("given a mixed array with non-number values, returns largest number and ignore non-numbers", () => { + expect(findMax([7, 3, "hello", "#"])).toBe(7); +}); // 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("given an array of non-number values, returns -Infinity", () => { + expect(findMax(["#", "hello", "?"])).toBe(-Infinity); +}); diff --git a/Sprint-1/implement/sum.js b/Sprint-1/implement/sum.js index 9062aafe3..11ff75dcb 100644 --- a/Sprint-1/implement/sum.js +++ b/Sprint-1/implement/sum.js @@ -1,4 +1,8 @@ function sum(elements) { + // filter out the numbers and sum them up + return elements + .filter((item) => Number.isFinite(item)) + .reduce((acc, num) => acc + num, 0); } module.exports = sum; diff --git a/Sprint-1/implement/sum.test.js b/Sprint-1/implement/sum.test.js index dd0a090ca..2cc159e93 100644 --- a/Sprint-1/implement/sum.test.js +++ b/Sprint-1/implement/sum.test.js @@ -13,24 +13,39 @@ 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("given an empty array, returns 0", () => { + expect(sum([])).toBe(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 one number, returns the number", () => { + expect(sum([1])).toBe(1); +}); // Given an array containing negative numbers // When passed to the sum function // Then it should still return the correct total sum - +test("given an array of only negative numbers, returns the sum", () => { + expect(sum([-1, -2])).toBe(-3); +}); // Given an array with decimal/float numbers // When passed to the sum function // Then it should return the correct total sum - +test("given an array with decimal/float numbers, returns the sum", () => { + expect(sum([2, 3.5, 4])).toBe(9.5); +}); // 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 +// Then it should ignore the non-numerical values and return the sum of the numerical +test("given an array containing non-number values, returns sum of number values", () => { + expect(sum([5, "#", "hello", 2])).toBe(7); +}); // 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("given an array of non-number values, returns 0", () => { + expect(sum(["#", "hello"])).toBe(0); +}); diff --git a/Sprint-1/refactor/includes.js b/Sprint-1/refactor/includes.js index 29dad81f0..9373ad463 100644 --- a/Sprint-1/refactor/includes.js +++ b/Sprint-1/refactor/includes.js @@ -1,11 +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) { - return true; - } + for (const number of list) { + if (number === target) return true; } return false; } diff --git a/Sprint-1/stretch/aoc-2018-day1/solution.js b/Sprint-1/stretch/aoc-2018-day1/solution.js index e69de29bb..5e76650dd 100644 --- a/Sprint-1/stretch/aoc-2018-day1/solution.js +++ b/Sprint-1/stretch/aoc-2018-day1/solution.js @@ -0,0 +1,14 @@ +const fs = require("fs"); +const path = require("path"); +const data = fs.readFileSync(path.join(__dirname, "input.txt"), "utf-8"); +const total = data + .split("\n") + .map((line) => line.trim()) + .filter((line) => line !== "") + .map(Number); + +const mapFrequency = (changes) => changes.reduce((acc, num) => acc + num, 0); +console.log(mapFrequency([+1, +1, +1])); // 3 +console.log(mapFrequency([+1, +1, -2])); // 0 +console.log(mapFrequency([-1, -2, -3])); // -6 +console.log(mapFrequency(total)); // 529;