diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index b22590bc6..efc3a7c42 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -6,9 +6,24 @@ // 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)|| list.length === 0) { + return null; + } + + let onlyArrOfNumbers = list.filter(el => typeof el === "number"); + + if (onlyArrOfNumbers.length === 0) { + return null; + } + let sortedArr = onlyArrOfNumbers.sort((a,b) => a - b); + + const middleIndex = Math.floor(sortedArr.length / 2); + + if (sortedArr.length % 2 === 1) { + return sortedArr[middleIndex]; + } else { + return (sortedArr[middleIndex - 1] + sortedArr[middleIndex]) / 2; + } +} module.exports = calculateMedian; diff --git a/Sprint-1/implement/dedupe.js b/Sprint-1/implement/dedupe.js index 781e8718a..badd7bda9 100644 --- a/Sprint-1/implement/dedupe.js +++ b/Sprint-1/implement/dedupe.js @@ -1 +1,12 @@ -function dedupe() {} +// start with the function; +function dedupe(arr) { + if (arr.length === 0) return []; + let cleanArr = []; + for (let item of arr) { + if (!cleanArr.includes(item)) { + cleanArr.push(item); + } + } + return cleanArr; +} +module.exports = dedupe; diff --git a/Sprint-1/implement/dedupe.test.js b/Sprint-1/implement/dedupe.test.js index 23e0f8638..513da5167 100644 --- a/Sprint-1/implement/dedupe.test.js +++ b/Sprint-1/implement/dedupe.test.js @@ -3,7 +3,7 @@ const dedupe = require("./dedupe.js"); Dedupe Array 📖 Dedupe means **deduplicate** - + In this kata, you will need to deduplicate the elements of an array E.g. dedupe(['a','a','a','b','b','c']) target output: ['a','b','c'] @@ -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("for empty array it should return an empty array", () => { + const arr = []; + expect(dedupe(arr)).toEqual([]); +}); // Given an array with no duplicates // When passed to the dedupe function // Then it should return a copy of the original array +test("for array with no duplicates should return the original array", () => { + const arr = [1, 2, 3, 4]; + expect(dedupe(arr)).toEqual(arr); +}); // 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("removes duplicates and keeps first occurrence", () => { + const arr = [1, 2, 1, 3, 4]; + expect(dedupe(arr)).toEqual([1, 2, 3, 4]); +}); diff --git a/Sprint-1/implement/max.js b/Sprint-1/implement/max.js index 6dd76378e..764c81f67 100644 --- a/Sprint-1/implement/max.js +++ b/Sprint-1/implement/max.js @@ -1,4 +1,9 @@ +// start your function; function findMax(elements) { + if (elements.length === 0) return -Infinity; + let onlyNumArr = elements.filter((el) => typeof el === "number"); + let maxNum = Math.max(...onlyNumArr); + return maxNum; } module.exports = findMax; diff --git a/Sprint-1/implement/max.test.js b/Sprint-1/implement/max.test.js index 82f18fd88..37f4342df 100644 --- a/Sprint-1/implement/max.test.js +++ b/Sprint-1/implement/max.test.js @@ -16,28 +16,55 @@ 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("if given empty array it should return Infinity", () => { + const elements = []; + expect(findMax(elements)).toEqual(-Infinity); +}); // Given an array with one number // When passed to the max function // Then it should return that number +test("if given 1 number, it should return that number", () => { + const elements = [8]; + expect(findMax(elements)).toEqual(8); +}); // 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 positive and negative value, should return largest number overall", () => { + const elements = [1, 3, 4, -3, -5]; + expect(findMax(elements)).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("if given all negative number it should return closest to Zero", () => { + const elements = [-1, -2, -4, -8]; + expect(findMax(elements)).toEqual(-1); +}); // Given an array with decimal numbers // When passed to the max function // Then it should return the largest decimal number +test("if given decimal number, should return largest decimal number", () => { + const elements = [1.2, 5.5, 9.1]; + expect(findMax(elements)).toEqual(9.1); +}); // 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("if given non-number values it should return only max number", () => { + const elements = [2, 5, 7, "hi", "apple", 9]; + expect(findMax(elements)).toEqual(9); +}); // 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("if given only non-number values it should return -Infinity ", () => { + const elements = ["a", "n", "hi"]; + expect(findMax(elements)).toEqual(-Infinity); +}); diff --git a/Sprint-1/implement/sum.js b/Sprint-1/implement/sum.js index 9062aafe3..1202726b3 100644 --- a/Sprint-1/implement/sum.js +++ b/Sprint-1/implement/sum.js @@ -1,4 +1,12 @@ function sum(elements) { + if (elements.length === 0) return 0; + let numberElement = elements.filter((el) => typeof el === "number"); + if (numberElement.length === 0) return Infinity; + let total = 0; + for (let num of numberElement) { + total += num; + } + return Math.floor(total); } module.exports = sum; diff --git a/Sprint-1/implement/sum.test.js b/Sprint-1/implement/sum.test.js index dd0a090ca..385a2c9da 100644 --- a/Sprint-1/implement/sum.test.js +++ b/Sprint-1/implement/sum.test.js @@ -13,24 +13,47 @@ 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("Empty array should be returned Zero", () => { + let elements = []; + expect(sum(elements)).toEqual(0); +}); // Given an array with just one number // When passed to the sum function // Then it should return that number +test("given one number should be returned same as original", () => { + let elements = [5]; + expect(sum(elements)).toEqual(5); +}); // Given an array containing negative numbers // When passed to the sum function // Then it should still return the correct total sum +test("negative number should return total with no effect", () => { + let elements = [1, 3, -2]; + expect(sum(elements)).toEqual(2); +}); // Given an array with decimal/float numbers // When passed to the sum function // Then it should return the correct total sum +test("decimal number should return the correct total", () => { + let elements = [1, 2.3, 4.5, 6]; + expect(sum(elements)).toEqual(13); +}); // 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("non-number values be ignored, function still should return total", () => { + let elements = [1, 2, 3, "hi", "b"]; + expect(sum(elements)).toEqual(6); +}); // 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("non-number values should return Infinity", () => { + let elements = ["a", "d", "hi"]; + expect(sum(elements)).toEqual(Infinity); +}); diff --git a/Sprint-1/refactor/includes.js b/Sprint-1/refactor/includes.js index 29dad81f0..207f3793b 100644 --- a/Sprint-1/refactor/includes.js +++ b/Sprint-1/refactor/includes.js @@ -1,11 +1,17 @@ // 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 (let index of list) { + if (index === 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..3ce439bae 100644 --- a/Sprint-1/stretch/aoc-2018-day1/solution.js +++ b/Sprint-1/stretch/aoc-2018-day1/solution.js @@ -0,0 +1,968 @@ +function finalFrequency(elements) { + const strList = ` + -10 +-12 ++1 ++14 ++11 +-19 +-4 ++10 ++10 +-12 +-13 +-10 +-8 ++11 ++3 +-10 ++8 ++5 +-14 ++7 ++12 ++12 ++14 +-1 ++17 +-5 ++9 +-15 ++8 +-16 ++9 ++6 ++17 +-11 ++19 ++11 +-9 +-1 +-8 +-16 +-5 ++6 ++2 ++6 ++12 ++15 ++16 +-6 +-8 +-5 ++11 +-9 ++19 ++19 +-5 +-12 +-17 +-20 ++9 +-6 +-2 ++20 ++15 +-8 ++15 ++19 +-5 +-17 +-9 ++1 +-9 +-11 +-19 +-1 +-15 ++18 +-4 ++19 ++3 ++21 ++5 +-1 ++8 ++9 ++9 +-16 ++17 +-15 ++18 ++14 ++8 +-13 +-2 +-15 ++13 ++19 +-13 ++5 ++16 ++16 ++7 ++8 +-19 ++3 +-12 ++18 +-16 +-19 ++8 +-16 ++20 ++15 +-5 +-17 ++15 ++14 +-2 ++18 +-6 ++5 ++16 ++13 +-8 +-9 +-1 ++13 ++18 +-2 ++16 +-4 ++19 ++6 ++14 +-6 +-16 +-10 +-5 +-15 ++11 ++10 ++18 ++8 +-2 ++15 +-9 ++7 ++10 ++9 +-6 ++1 +-17 +-12 ++19 ++14 +-9 +-18 ++20 ++12 ++10 ++14 ++6 ++9 +-18 ++4 ++15 ++15 ++3 ++9 +-4 +-19 ++8 ++16 ++19 +-12 +-1 ++16 ++11 ++13 +-8 ++4 ++18 ++10 +-11 ++18 +-8 ++19 +-4 +-5 +-12 +-3 +-10 +-11 +-18 +-7 ++3 ++14 ++18 ++2 +-7 +-12 ++14 ++12 ++13 +-1 ++13 +-1 ++15 ++13 +-1 +-17 +-4 ++17 +-3 ++13 ++5 +-19 ++17 ++10 ++6 +-15 ++11 ++16 ++11 ++20 ++19 ++5 +-11 +-2 +-12 +-6 +-11 +-14 ++5 ++10 +-23 ++14 +-11 ++22 +-1 +-14 +-35 +-23 ++11 ++4 ++10 +-6 ++19 +-5 ++4 +-13 +-7 ++11 +-24 +-17 ++9 +-17 ++11 ++2 +-4 +-10 +-24 ++14 ++15 ++19 ++7 +-2 ++11 +-13 +-8 +-8 +-14 +-17 ++6 ++8 +-16 +-18 ++1 +-21 +-14 ++8 +-2 ++20 +-19 ++9 +-15 +-6 +-7 ++4 +-3 +-8 ++16 ++15 +-26 ++18 +-22 +-16 +-3 +-17 +-18 ++4 ++5 ++3 +-1 ++3 ++2 ++3 ++7 +-5 ++20 +-11 ++15 +-6 +-19 +-24 +-6 +-20 ++13 +-4 +-18 ++15 ++9 ++16 ++12 +-5 ++16 ++9 +-4 +-14 +-9 +-19 +-5 +-16 +-16 ++5 +-4 ++12 +-15 +-15 +-14 ++4 +-18 +-12 +-4 ++6 ++9 +-12 ++14 ++9 +-6 ++3 ++8 ++4 ++7 +-14 +-15 +-18 ++12 +-19 +-14 +-15 +-16 ++15 +-8 ++14 +-4 +-21 ++7 +-12 +-2 +-10 +-6 ++14 ++14 +-9 +-14 +-16 ++2 ++7 ++5 +-15 +-15 ++19 +-16 +-5 +-16 +-19 ++9 +-27 +-8 ++6 ++1 +-14 +-9 +-50 +-9 +-21 ++5 +-20 +-19 ++6 +-11 ++6 ++1 +-17 +-14 +-2 ++8 +-4 ++16 ++1 +-3 +-9 ++20 ++15 +-22 +-3 +-13 +-12 +-25 ++5 +-13 +-2 +-8 +-11 ++17 ++3 +-7 ++13 ++1 ++18 +-13 ++1 ++7 +-15 +-43 +-6 +-10 +-9 +-20 +-7 +-8 +-15 +-9 ++13 +-9 ++24 ++13 ++5 +-8 +-15 +-4 +-4 +-4 +-14 +-21 ++16 ++15 +-14 +-20 +-20 +-15 +-19 +-41 ++16 ++3 ++8 +-22 +-30 ++8 ++40 ++15 ++24 +-7 ++22 ++10 ++15 ++9 ++28 ++14 ++2 ++14 ++6 ++17 ++1 ++30 ++5 +-2 ++46 ++24 ++46 +-3 +-7 ++35 ++16 ++13 +-21 ++28 ++13 +-25 ++180 ++25 ++5 +-9 ++23 +-5 +-2 ++13 +-12 ++2 +-25 +-5 ++18 +-24 ++45 ++17 +-7 ++22 +-152 +-11 ++4 ++50 ++129 +-7 ++22 ++109 +-56 ++80 ++29 +-75060 +-2 +-14 ++7 +-17 +-5 ++6 +-3 ++18 ++19 +-14 +-12 +-6 ++9 +-5 +-13 +-6 +-3 +-15 ++13 ++3 +-18 +-11 ++16 +-2 ++10 +-14 +-7 ++19 +-20 ++15 ++10 ++12 ++10 ++16 +-11 ++14 +-5 ++14 ++11 +-5 ++1 +-13 ++1 ++18 +-10 ++25 ++13 +-17 ++15 +-18 ++1 +-6 +-12 ++5 +-7 ++18 +-6 +-18 +-17 +-5 +-15 +-1 +-15 ++1 +-12 ++9 ++16 ++8 ++5 ++1 ++9 ++18 +-13 +-30 +-27 +-19 ++2 +-16 +-19 +-10 +-18 ++21 ++18 +-5 ++14 ++9 +-6 ++2 +-16 +-11 ++1 ++18 +-4 ++19 ++7 ++6 +-8 ++25 +-50 ++6 +-21 ++6 +-2 ++14 +-11 ++10 +-9 ++16 ++25 +-61 +-17 +-14 ++13 ++3 +-14 +-4 +-1 ++9 +-1 +-4 +-5 ++7 +-15 ++7 ++16 ++6 +-2 +-11 +-11 +-13 ++11 +-12 +-13 +-8 ++3 +-13 ++16 +-1 +-14 ++19 ++4 ++10 ++12 ++26 ++6 ++4 ++24 +-31 ++9 +-10 +-23 +-13 ++3 +-18 +-9 +-6 ++1 +-2 ++15 +-10 +-17 +-17 ++15 ++9 ++4 ++19 +-4 ++12 ++11 +-25 ++16 ++4 +-2 ++39 ++49 ++9 ++96 ++4 ++19 ++3 ++11 ++25 ++14 ++11 +-5 +-26 +-8 +-16 ++11 ++2 ++2 +-1 +-10 +-45 +-160 +-31 +-3 +-43 +-18 ++19 ++3 ++4 ++11 +-16 +-19 +-22 ++6 ++2 ++10 +-11 +-17 ++7 ++1 ++15 ++17 +-19 ++6 +-26 +-7 +-9 ++19 ++2 ++6 +-22 ++2 +-8 ++5 ++7 ++9 ++8 +-16 ++2 +-19 +-19 +-3 ++16 ++5 ++4 +-18 +-11 +-19 ++16 +-15 ++7 +-1 +-9 +-19 ++11 +-17 +-5 ++7 +-11 ++16 +-19 ++5 ++21 +-2 ++12 ++14 +-7 ++18 +-8 ++10 +-11 +-17 ++3 +-13 ++19 +-3 ++10 ++17 ++17 +-18 ++7 +-2 +-2 ++12 +-6 ++19 +-20 +-15 +-11 +-27 ++8 ++18 ++9 +-1 +-2 ++19 ++7 ++12 ++32 ++14 ++13 +-9 +-14 ++20 ++18 +-4 +-49 +-6 +-10 +-7 +-4 +-9 +-6 +-37 +-21 +-2 ++5 ++12 ++4 +-33 +-4 +-13 +-4 ++13 +-19 +-7 +-16 +-1 +-1 ++8 +-14 +-15 ++11 +-10 +-14 +-22 +-11 ++10 +-3 ++7 ++9 +-15 ++16 +-7 +-1 ++2 ++20 ++11 +-1 ++6 +-2 ++3 ++3 ++2 +-20 +-10 ++11 +-10 ++13 +-1 ++20 ++7 ++7 ++5 ++1 ++4 +-13 ++15 +-3 ++2 +-4 +-3 +-1 ++13 +-15 +-14 +-2 +-6 ++3 ++13 +-19 +-11 ++8 +-21 +-2 +-23 +-18 ++20 ++7 ++6 +-28 ++7 +-33 ++13 +-16 +-18 +-10 +-18 ++13 ++8 +-14 +-13 +-2 ++4 +-14 +-7 +-18 +-20 +-4 +-5 ++8 +-4 ++16 ++21 ++20 +-8 +-14 +-2 +-14 +-22 ++14 ++36 ++32 +-28 ++1 +-21 ++8 ++14 ++75784`; + + let newArr = strList.split("\n"); + let finalFreq = 0; + for (let n of newArr) { + finalFreq += Number(n); + } + /* + let totalFreq = newArr.map(Number); + let finalFreq = totalFreq.reduce((total, n) => total + n, 0); + return finalFreq; + */ + + return finalFreq; +} +console.log(finalFrequency()); // ==> 529