diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index b22590bc6..3255aadcd 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -6,9 +6,29 @@ // 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]; + if (!Array.isArray(list)) { + return null; + } + + let listCopy = [...list]; + let filteredList = listCopy.filter((x) => typeof x === "number"); + let sortedList = filteredList.sort((a, b) => a - b); + const middleIndex = Math.floor(sortedList.length / 2); + + if (filteredList.length === 0) { + return null; + } + + let median = 0; + if (sortedList.length % 2 === 0) { + median = (sortedList[middleIndex - 1] + sortedList[middleIndex]) / 2; + } else { + median = sortedList[middleIndex]; + } + return median; } + + module.exports = calculateMedian; diff --git a/Sprint-1/implement/dedupe.js b/Sprint-1/implement/dedupe.js index 781e8718a..482aa5b4a 100644 --- a/Sprint-1/implement/dedupe.js +++ b/Sprint-1/implement/dedupe.js @@ -1 +1,18 @@ -function dedupe() {} +function dedupe(arr) { + if (arr.length === 0) { + return []; + } + + for (let element of arr) { + if (arr.indexOf(element) === arr.lastIndexOf(element)) { + return arr; + } else { + arr.splice(arr.lastIndexOf(element), 1); + } + } + return arr; +} + +console.log(dedupe([1, "d", 6, 1, 5, "d", null])); + +module.exports = dedupe; diff --git a/Sprint-1/implement/dedupe.test.js b/Sprint-1/implement/dedupe.test.js index 23e0f8638..5848bf0a8 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,29 @@ 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("given an empty array, it returns an empty array",()=>{ + const array=[] + const currentOutput=dedupe(array) + const targetOutput=[] + expect(currentOutput).toEqual(targetOutput) +}); // 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 a copy of the original array", () => { + const array = [1,"d",6]; + const currentOutput = dedupe(array); + const targetOutput = [1,"d",6]; + expect(currentOutput).toEqual(targetOutput); +}); // 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("given an array with strings or numbers, it removes the duplicate values, preserving the first occurence of each element", () => { + const array = [1, "d", 6,1,5,"d",null]; + const currentOutput = dedupe(array); + const targetOutput = [1, "d", 6,5,null]; + expect(currentOutput).toEqual(targetOutput); +}); \ No newline at end of file diff --git a/Sprint-1/implement/max.js b/Sprint-1/implement/max.js index 6dd76378e..7dd598e64 100644 --- a/Sprint-1/implement/max.js +++ b/Sprint-1/implement/max.js @@ -1,4 +1,11 @@ function findMax(elements) { + const filteredElements = elements.filter( + (x) => typeof x === "number" && !Number.isNaN(x) + ); + + const sortedElements = filteredElements.sort((a, b) => a - b); + + return sortedElements.reduce((a, b) => Math.max(a, b), -Infinity); } module.exports = findMax; diff --git a/Sprint-1/implement/max.test.js b/Sprint-1/implement/max.test.js index 82f18fd88..eb3b3e074 100644 --- a/Sprint-1/implement/max.test.js +++ b/Sprint-1/implement/max.test.js @@ -16,28 +16,69 @@ 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", () => { + const array = []; + const currentOutput = findMax(array); + const targetOutput = -Infinity; + expect(currentOutput).toEqual(targetOutput); +}); // 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", () => { + const array = [3]; + const currentOutput = findMax(array); + const targetOutput = 3; + expect(currentOutput).toEqual(targetOutput); +}); // 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 both positive and negative numbers, returns -the largest number overall ", () => { + const array = [3.5, -5, 7, -9]; + const currentOutput = findMax(array); + const targetOutput = 7; + expect(currentOutput).toEqual(targetOutput); +}); // 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 just negative numbers, return the closest one to 0", () => { + const array = [-5, -1, -7.6, -3]; + const currentOutput = findMax(array); + const targetOutput = -1; + expect(currentOutput).toEqual(targetOutput); +}); // 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, return the largest decimal", () => { + const array = [-5.9, -1.8, -7.6, -3.2]; + const currentOutput = findMax(array); + const targetOutput = -1.8; + expect(currentOutput).toEqual(targetOutput); +}); // 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 an array with non-number values, returns the max number", () => { + const array = [-5, -1, "k", -7.6, null, -3]; + const currentOutput = findMax(array); + const targetOutput = -1; + expect(currentOutput).toEqual(targetOutput); +}); // 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 with only non-numbers, returns -Infinity", () => { + const array = [false, "k", "-7.6", null, {}, NaN]; + const currentOutput = findMax(array); + const targetOutput = -Infinity; + expect(currentOutput).toEqual(targetOutput); +}); diff --git a/Sprint-1/implement/sum.js b/Sprint-1/implement/sum.js index 9062aafe3..738b3991b 100644 --- a/Sprint-1/implement/sum.js +++ b/Sprint-1/implement/sum.js @@ -1,4 +1,13 @@ function sum(elements) { + const filteredElements = elements.filter( + (x) => typeof x === "number" && !Number.isNaN(x) + ); + + if (filteredElements.length === 0) { + return 0; + } + return filteredElements.reduce((a, b) => a + b); } +console.log(sum([0])); module.exports = sum; diff --git a/Sprint-1/implement/sum.test.js b/Sprint-1/implement/sum.test.js index dd0a090ca..3f4584553 100644 --- a/Sprint-1/implement/sum.test.js +++ b/Sprint-1/implement/sum.test.js @@ -3,7 +3,7 @@ In this kata, you will need to implement a function that sums the numerical elements of an array E.g. sum([10, 20, 30]), target output: 60 -E.g. sum(['hey', 10, 'hi', 60, 10]), target output: 80 (ignore any non-numerical elements) +E.g. sum(['hey', 10, 'hi', 60, 10]), target output: 80 (ignore any non-numerical elements) */ const sum = require("./sum.js"); @@ -13,24 +13,59 @@ 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", () => { + const array = []; + const currentOutput = sum(array); + const targetOutput = 0; + expect(currentOutput).toEqual(targetOutput); +}); // 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 the number", () => { + const array = [5]; + const currentOutput = sum(array); + const targetOutput = 5; + expect(currentOutput).toEqual(targetOutput); +}); // 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 containig negative numbers, return the total of the numbers", () => { + const array = [-5, -9, -3]; + const currentOutput = sum(array); + const targetOutput = -17; + expect(currentOutput).toEqual(targetOutput); +}); // 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 containig decimalnumbers, return the total of the numbers", () => { + const array = [-5.5, 9, -3]; + const currentOutput = sum(array); + const targetOutput = 0.5; + expect(currentOutput).toEqual(targetOutput); +}); // 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("given an array containig non-number values,ignore the non-numerical values and return the total of the numbers", () => { + const array = [5, "s", null, NaN, -9, -3]; + const currentOutput = sum(array); + const targetOutput = -7; + expect(currentOutput).toEqual(targetOutput); +}); // 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 containig only non-number values, return 0", () => { + const array = ["s", null, NaN, "-3", true]; + const currentOutput = sum(array); + const targetOutput = 0; + expect(currentOutput).toEqual(targetOutput); +}); diff --git a/Sprint-1/refactor/includes.js b/Sprint-1/refactor/includes.js index 29dad81f0..994131c59 100644 --- a/Sprint-1/refactor/includes.js +++ b/Sprint-1/refactor/includes.js @@ -1,8 +1,7 @@ // 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 (let 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..212c43652 100644 --- a/Sprint-1/stretch/aoc-2018-day1/solution.js +++ b/Sprint-1/stretch/aoc-2018-day1/solution.js @@ -0,0 +1,985 @@ + +const data = ` ++10 +-9 ++12 ++5 ++13 +-6 +-19 +-4 +-17 +-1 ++7 ++18 +-13 +-15 +-6 +-11 +-12 +-9 ++12 ++2 ++14 ++11 ++17 +-10 +-21 ++6 ++18 +-4 +-16 ++15 ++8 ++13 ++12 ++4 +-1 +-19 +-7 ++14 ++8 +-3 ++5 +-9 ++16 +-11 ++21 +-3 ++23 +-8 +-16 ++11 +-3 ++1 ++16 +-3 ++14 +-9 ++17 ++5 +-3 ++17 ++12 ++19 +-12 +-14 ++13 +-3 ++6 ++15 ++18 +-8 ++14 ++14 ++3 ++18 ++4 +-9 ++12 ++6 ++8 ++18 ++12 ++13 +-6 +-13 +-13 +-17 ++14 +-3 ++2 ++12 +-6 ++16 +-9 ++6 +-1 +-4 ++17 ++9 ++6 +-3 ++9 +-15 +-11 +-17 ++7 +-9 +-18 +-21 ++2 +-11 ++12 +-21 +-8 ++14 ++5 ++17 +-4 ++8 ++4 ++9 ++7 +-5 +-7 ++6 ++19 ++20 ++22 ++8 +-11 ++17 +-1 +-1 ++14 ++11 +-9 +-18 ++19 +-10 ++5 +-17 +-19 +-4 ++3 ++7 ++5 ++5 ++21 ++17 ++17 +-12 ++13 ++15 ++18 ++17 +-4 +-7 +-15 ++1 ++5 +-16 +-9 +-11 +-5 ++7 +-10 +-15 ++3 ++11 +-6 +-9 ++10 ++17 ++15 ++7 ++13 ++15 +-6 ++10 ++7 ++12 +-4 +-12 ++3 ++7 +-15 +-13 ++16 ++1 +-9 +-18 +-10 ++6 ++9 +-11 ++7 ++10 ++11 +-10 ++18 ++4 ++5 ++19 +-1 +-16 ++18 +-4 ++5 ++11 ++10 ++17 ++4 +-1 +-13 ++2 +-10 +-17 +-12 ++16 ++16 ++12 ++14 ++12 ++18 ++12 ++2 +-13 ++15 ++16 +-6 ++10 +-13 +-15 +-12 ++6 ++19 +-6 ++9 ++3 ++19 +-3 ++5 ++3 ++6 ++23 ++4 +-18 +-17 +-15 ++1 ++4 ++13 ++11 +-13 ++6 +-18 +-13 +-7 +-13 ++5 ++1 +-19 ++11 +-9 ++5 +-1 +-11 ++8 +-11 +-1 +-14 ++11 +-13 +-15 ++16 +-18 ++7 ++16 ++18 +-13 ++7 +-8 +-12 ++16 +-2 ++9 +-14 +-10 +-17 +-8 +-16 +-19 ++5 +-1 +-2 ++4 ++7 +-19 +-20 ++19 ++17 ++12 ++17 +-14 ++19 +-17 ++7 +-2 +-6 +-19 +-8 +-7 +-2 ++21 ++19 +-12 ++18 +-15 ++33 +-4 +-1 ++2 ++5 ++12 +-1 ++4 +-9 ++23 ++1 ++16 ++1 ++13 +-16 ++9 ++4 ++12 +-1 ++9 ++22 ++1 +-5 ++12 +-14 +-22 ++9 ++28 ++2 ++3 ++10 ++22 +-10 +-2 ++23 ++7 ++14 +-18 ++7 +-9 ++10 ++9 ++11 ++6 ++12 ++11 ++13 ++9 +-4 ++9 ++7 +-19 +-5 +-6 ++5 +-13 +-11 ++13 +-8 +-2 +-9 ++3 +-15 ++20 ++8 ++10 +-14 +-13 ++2 +-11 ++5 ++7 +-13 ++15 +-21 +-16 ++19 +-21 ++10 +-33 ++7 +-28 ++12 +-19 +-25 +-14 ++11 +-32 +-23 +-10 ++17 +-32 +-16 +-18 ++8 ++2 +-74 +-17 ++7 +-8 +-8 +-12 +-12 +-16 +-19 +-2 ++20 +-4 ++19 +-21 ++18 +-10 +-17 ++10 +-20 ++15 +-19 ++13 +-7 +-17 ++4 +-18 ++9 ++17 +-23 ++21 +-27 +-11 +-18 +-23 ++6 +-17 +-9 ++8 +-19 ++10 +-8 +-8 +-7 +-7 ++12 +-18 ++5 +-3 ++12 +-17 +-2 +-13 +-3 ++8 +-2 ++14 +-21 ++16 +-19 +-14 ++1 +-2 ++11 +-3 +-21 ++8 ++2 ++9 ++24 ++18 ++14 +-19 ++12 ++17 +-9 +-6 ++10 ++13 ++13 +-24 +-5 ++27 +-20 +-15 +-6 +-21 ++4 +-19 +-1 ++31 ++17 ++14 ++8 ++11 ++7 +-20 ++7 ++17 ++4 ++7 +-5 ++11 ++26 ++8 ++19 ++19 ++34 ++15 ++8 ++3 ++4 ++10 ++6 +-28 +-13 +-7 ++3 ++19 ++18 +-13 ++9 ++61 ++33 +-39 ++26 +-53 +-5 ++2 +-8 +-30 +-9 +-4 +-16 +-15 ++32 ++15 ++28 ++20 +-88 ++5 +-14 +-16 +-51 ++26 ++5 +-7 +-7 +-22 ++2 +-14 ++8 +-15 +-20 +-40 ++22 +-27 ++2 +-18 +-38 +-23 +-10 +-8 +-3 ++8 +-27 ++17 ++33 ++16 ++77 +-35 ++52 ++339 ++3 +-275 ++81 ++169 +-299 +-668 +-79803 +-11 ++13 +-7 ++14 ++4 +-14 +-12 +-17 +-18 +-4 ++19 ++19 ++2 +-6 +-18 ++9 ++18 +-16 ++17 +-13 +-8 ++3 ++9 ++7 ++15 +-4 ++18 ++10 ++17 +-1 +-17 +-4 ++9 +-2 +-8 +-3 ++16 +-2 +-12 +-15 +-13 +-17 +-15 +-5 +-4 ++2 +-17 +-18 ++4 ++10 +-15 ++17 +-11 +-14 +-17 +-3 +-13 +-9 +-5 +-13 ++6 +-2 +-12 +-16 +-7 ++18 +-16 ++9 ++8 +-11 +-18 ++8 +-2 ++5 +-12 ++2 ++1 +-13 +-13 +-18 +-8 +-6 +-5 +-10 ++17 +-11 +-12 +-10 +-10 +-9 ++4 ++4 ++7 ++19 ++3 +-7 ++13 +-5 +-11 +-13 +-12 +-17 +-6 ++5 +-12 ++14 ++13 +-19 ++15 ++6 +-4 +-19 +-3 +-7 +-4 +-5 ++14 +-7 +-19 ++18 ++2 +-15 ++19 +-18 +-3 +-12 ++5 +-2 +-4 +-14 ++12 +-1 +-14 ++6 ++10 +-12 +-13 +-15 ++10 +-12 +-3 ++4 ++14 ++13 ++18 +-11 ++4 +-3 +-17 ++14 +-20 ++10 ++11 ++4 +-1 ++13 +-5 ++17 ++4 +-3 +-15 ++21 ++19 +-6 +-18 +-13 +-20 +-7 +-15 +-4 ++6 +-7 +-17 ++4 +-18 +-11 +-5 ++19 ++9 ++7 +-15 ++3 ++2 ++8 +-4 ++12 +-9 +-17 +-18 ++10 ++2 +-15 ++4 ++1 +-15 +-16 ++15 ++5 +-18 ++4 +-16 ++3 +-15 +-18 +-17 +-16 +-2 ++11 +-3 ++9 ++7 ++4 +-16 ++4 +-1 +-18 +-21 +-2 +-4 ++1 ++4 +-19 +-12 +-6 ++2 ++1 +-5 +-4 +-7 +-6 +-19 ++6 +-11 ++2 ++4 ++1 ++16 +-4 +-15 ++12 ++10 ++24 ++13 ++14 +-8 +-7 ++21 +-12 ++15 ++17 +-13 ++19 +-5 +-4 ++14 ++10 +-18 ++9 ++22 +-19 ++9 ++8 ++7 ++19 ++13 ++9 +-5 +-6 +-4 +-3 +-1 ++5 ++10 ++6 ++7 ++7 ++8 +-18 ++12 ++4 ++18 ++1 ++9 +-8 ++18 +-14 ++9 +-2 +-2 ++15 ++12 +-11 ++19 ++8 ++3 +-7 ++3 +-13 +-15 +-18 +-16 +-20 +-17 ++14 +-4 ++15 +-14 ++15 ++13 ++10 +-11 +-7 +-27 +-5 +-13 +-10 +-15 +-12 +-18 ++17 ++18 ++5 +-2 ++6 +-13 +-8 ++20 +-17 ++16 +-3 ++2 ++11 +-24 ++9 ++9 ++3 +-1 +-1 ++16 +-12 ++16 ++18 +-9 ++6 ++14 ++18 +-4 +-21 ++16 ++45 ++13 +-17 ++11 ++10 ++7 ++33 ++7 ++13 +-12 +-11 ++18 ++1 ++18 ++18 ++4 +-7 ++15 +-6 ++11 +-18 ++16 +-5 ++12 +-4 +-2 ++20 ++6 +-2 +-15 +-14 ++81142`; +const nums = data.trim().split(/\s+/).map(Number); + +function solution(arr) { + let sum = 0; + for (let elem of arr) { + sum += elem; + } + return sum; +} +console.log(solution(nums)); \ No newline at end of file