diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index b22590bc6..3e775bfe6 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; + if (!Array.isArray(list)) { + return null; + } + const sortedList = list + .filter((element) => typeof element === "number") + .sort((a, b) => a - b); + + if (sortedList.length === 0) { + return null; + } + const middleIndex = Math.floor(sortedList.length / 2); + + if (sortedList.length % 2 === 0) { + return (sortedList[middleIndex - 1] + sortedList[middleIndex]) / 2; + } else { + return sortedList[middleIndex]; + } } module.exports = calculateMedian; diff --git a/Sprint-1/implement/dedupe.js b/Sprint-1/implement/dedupe.js index 781e8718a..5fe58a4b2 100644 --- a/Sprint-1/implement/dedupe.js +++ b/Sprint-1/implement/dedupe.js @@ -1 +1,7 @@ -function dedupe() {} +function dedupe(array) { + if (!Array.isArray(array)) return []; + + return [...new Set(array)]; +} + +module.exports = dedupe; diff --git a/Sprint-1/implement/dedupe.test.js b/Sprint-1/implement/dedupe.test.js index d7c8e3d8e..d9c3467b7 100644 --- a/Sprint-1/implement/dedupe.test.js +++ b/Sprint-1/implement/dedupe.test.js @@ -1,4 +1,83 @@ const dedupe = require("./dedupe.js"); + +describe("dedupe", () => { + test("Return empty array for given empty array", () => { + expect(dedupe([])).toEqual([]); + }); + + test("Return copy for no duplicates input", () => { + [ + [2, 4, 1, 6.9, 0], + ["a", "d", "e", "b", "n", "c"], + ["2", "4", "1", "6", "9", "0"], + ["cat", "dog", "cow"], + ].forEach((input) => { + expect(dedupe(input)).toEqual(input); + }); + }); + + test("removes duplicate strings", () => { + [ + { input: ["a", "a", "a", "b", "b", "c"], expected: ["a", "b", "c"] }, + { + input: ["2", "4", "1", "6", "2", "4"], + expected: ["2", "4", "1", "6"], + }, + { input: ["cat", "dog", "cat"], expected: ["cat", "dog"] }, + ].forEach((obj) => expect(dedupe(obj.input)).toEqual(obj.expected)); + }); + + test("removes duplicate numbers", () => { + [ + { input: [1, 2, 1], expected: [1, 2] }, + { + input: [2, 4, 1, 6.9, 2, 0], + expected: [2, 4, 1, 6.9, 0], + }, + ].forEach((obj) => expect(dedupe(obj.input)).toEqual(obj.expected)); + }); + + test("removes duplicate numbers and strings", () => { + expect(dedupe([1, "a", 2, "b", 1, "a", "2"])).toEqual([ + 1, + "a", + 2, + "b", + "2", + ]); + }); + + test("handles special characters", () => { + expect(dedupe(["@", "@", "#", "#", "!"])).toEqual(["@", "#", "!"]); + }); + + test("handles strings with spaces", () => { + expect(dedupe(["a a", "a a", "b b"])).toEqual(["a a", "b b"]); + }); + + test("handles null and undefined", () => { + expect(dedupe([null, null, undefined])).toEqual([null, undefined]); + }); + + test("handles boolean values", () => { + expect(dedupe([true, false, true])).toEqual([true, false]); + }); + + [null, undefined, 123, "abc", {}].forEach((input) => { + expect(dedupe(input)).toEqual([]); + }); + + test("does not mutate original array", () => { + const input = [1, 2, 3]; + const original = [...input]; + + const result = dedupe(input); + + expect(result).not.toBe(input); + expect(input).toEqual(original); + }); +}); + /* Dedupe Array @@ -24,5 +103,5 @@ test.todo("given an empty array, it returns an empty array"); // 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. diff --git a/Sprint-1/implement/max.js b/Sprint-1/implement/max.js index 6dd76378e..b6d79cced 100644 --- a/Sprint-1/implement/max.js +++ b/Sprint-1/implement/max.js @@ -1,4 +1,7 @@ function findMax(elements) { + const numberArray = elements.filter((el) => typeof el === "number"); + if (numberArray.length === 0) return -Infinity; + return Math.max(...numberArray); } module.exports = findMax; diff --git a/Sprint-1/implement/max.test.js b/Sprint-1/implement/max.test.js index 82f18fd88..166393196 100644 --- a/Sprint-1/implement/max.test.js +++ b/Sprint-1/implement/max.test.js @@ -12,11 +12,40 @@ We have set things up already so that this file can see your function from the o const findMax = require("./max.js"); +describe("findMax", () => { + test("return -Infinity for empty array", () => { + expect(findMax([])).toEqual(-Infinity); + }); + + test("return same number with one value array", () => { + expect(findMax([3])).toEqual(3); + }); + + test("return largest number with both positive and negative ", () => { + expect(findMax([3, 6, -2, 0, -5, 2])).toEqual(6); + }); + + test("return largest number with just negative numbers ", () => { + expect(findMax([-3, -6, -2, -1, -5, -2])).toEqual(-1); + }); + + test("return largest number with just decimal numbers ", () => { + expect(findMax([-3, 6.75, 2, 1, 6.25, 2])).toEqual(6.75); + }); + + test("ignore non-numeric values with non-number values array", () => { + expect(findMax([-3, 6, "a", 1, "abc", 2])).toEqual(6); + }); + + test("return -Infinity with all non-number values", () => { + expect(findMax(["abc", "h", "a", "r", "b"])).toEqual(-Infinity); + }); +}); + // Given an empty array // 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"); // Given an array with one number // When passed to the max function diff --git a/Sprint-1/implement/sum.js b/Sprint-1/implement/sum.js index 9062aafe3..2d9e5101c 100644 --- a/Sprint-1/implement/sum.js +++ b/Sprint-1/implement/sum.js @@ -1,4 +1,6 @@ function sum(elements) { + return elements + .filter((el) => typeof el === "number") + .reduce((sum, num) => sum + num, 0); } - module.exports = sum; diff --git a/Sprint-1/implement/sum.test.js b/Sprint-1/implement/sum.test.js index dd0a090ca..27387b856 100644 --- a/Sprint-1/implement/sum.test.js +++ b/Sprint-1/implement/sum.test.js @@ -8,12 +8,38 @@ E.g. sum(['hey', 10, 'hi', 60, 10]), target output: 80 (ignore any non-numerical const sum = require("./sum.js"); +describe("sum", () => { + test("Return 0 for empty array", () => { + expect(sum([])).toEqual(0); + }); + + test("Return same number with one value array", () => { + expect(sum([5])).toEqual(5); + }); + + test("Return sum for array containing negative numbers", () => { + expect(sum([2, -5, 0, 9, -10, 15])).toEqual(11); + }); + + test("Return sum for decimal/float numbers array", () => { + expect(sum([2, 5.5, 0, 9, 10.75])).toEqual(27.25); + }); + + test("Ignore non-numeric values with non-number values array", () => { + expect(sum([-3, 6, "a", 1, "abc", 2])).toEqual(6); + }); + + test("return 0 with all non-number values", () => { + expect(sum(["abc", "h", "a", "r", "b"])).toEqual(0); + }); +}); + // 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.todo("given an empty array, returns 0"); // Given an array with just one number // When passed to the sum function diff --git a/Sprint-1/refactor/includes.js b/Sprint-1/refactor/includes.js index 29dad81f0..177564d3d 100644 --- a/Sprint-1/refactor/includes.js +++ b/Sprint-1/refactor/includes.js @@ -1,6 +1,6 @@ // Refactor the implementation of includes to use a for...of loop -function includes(list, target) { +/*function includes(list, target) { for (let index = 0; index < list.length; index++) { const element = list[index]; if (element === target) { @@ -9,5 +9,14 @@ function includes(list, target) { } return false; } +*/ +function includes(list, target) { + for (const element of list) { + if (element === target) { + return true; + } + } + return false; +} module.exports = includes; diff --git a/Sprint-1/stretch/aoc-2018-day1/solution.js b/Sprint-1/stretch/aoc-2018-day1/solution.js index e69de29bb..681b4952b 100644 --- a/Sprint-1/stretch/aoc-2018-day1/solution.js +++ b/Sprint-1/stretch/aoc-2018-day1/solution.js @@ -0,0 +1,9 @@ +const fs = require("fs"); +const data = fs.readFileSync("input.txt", "utf-8"); + +function findFinalFrequency(inputString) { + const numberArray = inputString.split("\n").map(Number); + return numberArray.reduce((sum, num) => sum + num, 0); +} + +console.log(findFinalFrequency(data));