From 1676b00df2233d3861cd2ab388e054e0785c7df8 Mon Sep 17 00:00:00 2001 From: Mjax Date: Tue, 8 Aug 2017 09:35:32 -0700 Subject: [PATCH 01/18] Completed longestString challenge --- longestString/longestString.js | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/longestString/longestString.js b/longestString/longestString.js index 35b887c..ed4a5a0 100644 --- a/longestString/longestString.js +++ b/longestString/longestString.js @@ -2,3 +2,21 @@ * Write a function that accepts an array of strings. * Return the longest string in the array. */ + +/* Test Cases +['a', 'bc' , 'acv'] -> 'acv' +strings of the same length with retrn the first longest element +*/ + +// loop through the strings array and search for the longest + +const returnLongestString = (arr) => { + let longestString = ''; + for (let i = 0; i < arr.length; i++) { + if (arr[i].length > longestString.length) { + longestString = arr[i]; + } + } + return longestString; +} + From 79267cd0ca4fb7e70fabef190edebd75c08add43 Mon Sep 17 00:00:00 2001 From: Mjax Date: Wed, 9 Aug 2017 09:17:00 -0700 Subject: [PATCH 02/18] complete reverseCase.js --- reverseCase/reverseCase.js | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/reverseCase/reverseCase.js b/reverseCase/reverseCase.js index f1051f5..0587b88 100644 --- a/reverseCase/reverseCase.js +++ b/reverseCase/reverseCase.js @@ -2,4 +2,20 @@ * Write a function that reverses the case of each letter in the strings that it receives. * Example: 'Hello World' -> 'hELLO wORLD' * Assume that each string will contain only spaces and letters. - */ \ No newline at end of file + */ + + + + const reverseCase = (str) => { + let newStr = '' + for (let i = 0; i < str.length; i++) { + if (str[i] === str[i].toUpperCase()) { + newStr += str[i].toLowerCase(); + } else { + newStr += str[i].toUpperCase(); + } + } + return newStr; + }; + + console.log(reverseCase('Hello There DUde wHatS upP')) \ No newline at end of file From 4ebb667b7a26c7826cc589c7ef95b03341a3e256 Mon Sep 17 00:00:00 2001 From: Mjax Date: Thu, 10 Aug 2017 09:28:00 -0700 Subject: [PATCH 03/18] complete isUnique challenge --- isUnique/isUnique.js | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/isUnique/isUnique.js b/isUnique/isUnique.js index 6c9caf5..a557ca8 100644 --- a/isUnique/isUnique.js +++ b/isUnique/isUnique.js @@ -1,3 +1,20 @@ /* Implement an algorithm to determine if a string has all unique characters. * What if you cannot use additional data structures? - */ \ No newline at end of file + */ + + + //This function uniqueCh will accept a string and return true or false depending on if it has repeating Characters + //This assumes spaces and other non-letters as characters + //Case sensitive +const uniqueCh = (str) => { + for (let i = 0; i < str.length; i++) { + if (str.slice(i+1, str.length).toUpperCase().includes(str[i].toUpperCase())) { + return false; + } + } + return true +}; + +console.log(uniqueCh("abcdefghii")) +console.log(uniqueCh("abcdefghij")) +console.log(uniqueCh("Halp meh")) \ No newline at end of file From 6fb695cc7001caa23db290d28955db6ee6edb2cb Mon Sep 17 00:00:00 2001 From: Mjax Date: Fri, 11 Aug 2017 09:35:51 -0700 Subject: [PATCH 04/18] complete callback practice --- callBackPractice/callBackPractice.js | 40 ++++++++++++++++++++++------ 1 file changed, 32 insertions(+), 8 deletions(-) diff --git a/callBackPractice/callBackPractice.js b/callBackPractice/callBackPractice.js index 8f5d9ac..0c35b25 100644 --- a/callBackPractice/callBackPractice.js +++ b/callBackPractice/callBackPractice.js @@ -17,7 +17,9 @@ // Write a function called firstItem that passes the first item of the given array to the callback function - +const firstItem = (arr, cb) => { + cb(arr[0]); +}; const foods = ['pineapple', 'mango', 'ribeye', 'curry', 'tacos', 'ribeye', 'mango']; firstItem(foods, (firstItem) => { @@ -25,46 +27,68 @@ firstItem(foods, (firstItem) => { }); // Write a function called getLength that passes the length of the array into the callback - +const getLength = (arr, cb) => { + return cb(arr.length); +}; getLength(foods, (length) => { console.log(`The length of the array is ${length}.`); }); // Write a function called last which passes the last item of the array into the callback - +const last = (arr, cb) => { + return cb(arr[arr.length - 1]); +}; last(foods, (lastItem) => { console.log(`The last item in the array is ${lastItem}.`); }); // Write a function called sumNums that adds two numbers and passes the result to the callback - +const sumNums = (num1, num2, cb) => { + cb(num1 + num2); +}; sumNums(5, 10, (sum) => { console.log(`The sum is ${sum}.`); }); // Write a function called multiplyNums that adds two numbers and passes the result to the callback - +const multiplyNums = (a, b, cb) => { + cb(a * b); +}; multiplyNums(5, 10, (product) => { console.log(`The product is ${product}.`); }); // Write a function called contains that checks if an item is present inside of the given array. // Pass true to the callback if it is, otherwise pass false - +const contains = (arr, check, cb) => { + cb(arr.includes(check)); +} contains(foods, 'ribeye', (result) => { console.log(result ? 'ribeye is in the array' : 'ribeye is not in the array'); }); // Write a function called removeDuplicates that removes all duplicate values from the given array. // Pass the array to the callback function. Do not mutate the original array. - +const removeDuplicates = (arr, cb) => { + const uniqArr = []; + for (let i = 0; i < arr.length; i++) { + if (!(arr.slice(i+1, arr[arr.length]).includes(arr[i]))) { + uniqArr.push(arr[i]); + } + } + cb(uniqArr); +} removeDuplicates(foods, (uniqueFoods) => { console.log(`foods with duplicates removed: ${uniqueFoods}`); }); // Write a function called forEach that iterates over the provided array and passes the value and index into the callback. - +const forEach = (arr, cb) => { + for (let i = 0; i < arr.length; i++) { + cb(arr[i], i); + } +} forEach(foods, (value, index) => { console.log(`${value} is at index ${index}.`); From 994c93366f2824fd2877375fbc5e1e6f42fd1735 Mon Sep 17 00:00:00 2001 From: Mjax Date: Mon, 14 Aug 2017 09:12:11 -0700 Subject: [PATCH 05/18] complete removeDuplicates --- removeDuplicates/removeDuplicates.js | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/removeDuplicates/removeDuplicates.js b/removeDuplicates/removeDuplicates.js index 970f719..b4b7901 100644 --- a/removeDuplicates/removeDuplicates.js +++ b/removeDuplicates/removeDuplicates.js @@ -9,5 +9,13 @@ */ const removeDuplicates = (arr) => { - //code here... -}; \ No newline at end of file + const newArr = []; + for (let i = 0; i < arr.length; i++) { + if (!newArr.includes(arr[i])) { + newArr.push(arr[i]); + } + } + return newArr; +}; + +console.log(removeDuplicates([1,2,3,5,4,6,6,7,9,8])) \ No newline at end of file From 801cc0c3e8ba7ffef051c7b514430e29bfd0aa55 Mon Sep 17 00:00:00 2001 From: Mjax Date: Mon, 14 Aug 2017 09:38:29 -0700 Subject: [PATCH 06/18] complete beast mode removeDuplicates --- removeDuplicates/removeDuplicates.js | 29 ++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/removeDuplicates/removeDuplicates.js b/removeDuplicates/removeDuplicates.js index b4b7901..a093a79 100644 --- a/removeDuplicates/removeDuplicates.js +++ b/removeDuplicates/removeDuplicates.js @@ -9,13 +9,38 @@ */ const removeDuplicates = (arr) => { + // 1 + // const newArr = []; + // for (let i = 0; i < arr.length; i++) { + // if (!newArr.includes(arr[i])) { + // newArr.push(arr[i]); + // } + // } + // return newArr; + + // 2 + //assuming the arr is sorted + // let tempNum; + // const newArr = []; + // for (let i = 0; i < arr.length; i++) { + // if (arr[i] !== tempNum) { + // newArr.push(arr[i]); + // } + // tempNum = arr[i]; + // } + // return newArr; + + // 3 + const elSearch = {}; const newArr = []; for (let i = 0; i < arr.length; i++) { - if (!newArr.includes(arr[i])) { + if(elSearch[arr[i]] === undefined) { newArr.push(arr[i]); + elSearch[arr[i]] = true; } } return newArr; }; -console.log(removeDuplicates([1,2,3,5,4,6,6,7,9,8])) \ No newline at end of file +console.log(removeDuplicates([1,2,3,5,4,6,6,7,9,8])) +console.log(removeDuplicates([4,5,4,2,2,4,5,9,8,7,9,-1,3,2,-1,-2])) \ No newline at end of file From 29af1344b1370407a64ac3e7ad9c177fcf486667 Mon Sep 17 00:00:00 2001 From: Mjax Date: Tue, 15 Aug 2017 09:12:25 -0700 Subject: [PATCH 07/18] water jugs solution --- brainTeasers/waterJugs.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/brainTeasers/waterJugs.md b/brainTeasers/waterJugs.md index 5276fc0..7c908eb 100644 --- a/brainTeasers/waterJugs.md +++ b/brainTeasers/waterJugs.md @@ -1 +1,4 @@ You have a five-quart jug, a three-quart jug, and an unlimited supply of water (but no measuring cups). How would you come up with exactly four quarts of water? Note that the jugs are oddly shaped, such that filling up exactly "half" of the jug would be impossible. + + +fill up the 5 quart jug with water and then fill the 3 quart jug just to the top. The 5 quart jug will be left with 2 quarts. Put the 2 quarts aside and repeat again. Add 2 and 2 together to get 4 quarts. \ No newline at end of file From f43d751a00c6e2162bef25145b0da020781cb396 Mon Sep 17 00:00:00 2001 From: Mjax Date: Tue, 15 Aug 2017 09:22:58 -0700 Subject: [PATCH 08/18] updated water jugs --- brainTeasers/waterJugs.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/brainTeasers/waterJugs.md b/brainTeasers/waterJugs.md index 7c908eb..9d02afc 100644 --- a/brainTeasers/waterJugs.md +++ b/brainTeasers/waterJugs.md @@ -1,4 +1,8 @@ You have a five-quart jug, a three-quart jug, and an unlimited supply of water (but no measuring cups). How would you come up with exactly four quarts of water? Note that the jugs are oddly shaped, such that filling up exactly "half" of the jug would be impossible. -fill up the 5 quart jug with water and then fill the 3 quart jug just to the top. The 5 quart jug will be left with 2 quarts. Put the 2 quarts aside and repeat again. Add 2 and 2 together to get 4 quarts. \ No newline at end of file +fill up the 5 quart jug with water and then fill the 3 quart jug just to the top. The 5 quart jug will be left with 2 quarts. Put the 2 quarts aside and repeat again. Add 2 and 2 together to get 4 quarts. + +or + +fill the 5 quarts with 3 quarts. repeat but leave 1 quart in the 3 quart jug. empty the 5 quart jug and fill it with the 1 quart of water. fill up the 3 quart jug and add it to the 1 quart to get 4 quarts. \ No newline at end of file From 03b53ab419132eed9339defafe5ba8b3f70ec248 Mon Sep 17 00:00:00 2001 From: Mjax Date: Wed, 16 Aug 2017 09:52:55 -0700 Subject: [PATCH 09/18] create closure for forLoopTimeout --- forLoopTimeout/forLoopTimeout.js | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/forLoopTimeout/forLoopTimeout.js b/forLoopTimeout/forLoopTimeout.js index 87522c2..f2c88e8 100644 --- a/forLoopTimeout/forLoopTimeout.js +++ b/forLoopTimeout/forLoopTimeout.js @@ -1,13 +1,20 @@ // Explain what is wrong with this code and how you would fix this. -// With ES6 there is a very, very simple way to solve this. +// With ES6 there is a very, very simple way to solve this. (change var to let) // See if you can solve this with just ES5 JS. // The output should be 1, 2, 3, .... 10. Right now it just prints 11. // I've been asked this three times in separate interviews. + +// in this expample, there is closue being created becaues i is call in a higher scope. (i=11 when +//the for loop ends) for (var i = 1; i <= 10; i++) { - setTimeout(function() { + var sT = (function(i) { // From looking at the code you would assume it would print 1 - 10 // It doesn't. Why? How can you make it print 1 - 10. - console.log(i); - }, 0); -} \ No newline at end of file + return function() { + console.log(i); + } + })(i); + sT(setTimeout,0) +} +//i = 20 \ No newline at end of file From f7242cbc3f6b3271b92cb2c2c01014705c550077 Mon Sep 17 00:00:00 2001 From: Mjax Date: Thu, 17 Aug 2017 09:20:11 -0700 Subject: [PATCH 10/18] longest palindrome complete --- .../largestPrimePalindrome.js | 34 ++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/largestPrimePalindrome/largestPrimePalindrome.js b/largestPrimePalindrome/largestPrimePalindrome.js index 4cc99c0..859d8e2 100644 --- a/largestPrimePalindrome/largestPrimePalindrome.js +++ b/largestPrimePalindrome/largestPrimePalindrome.js @@ -3,4 +3,36 @@ * Hint: it's 929 * You will first want to determine if the number is a palindrome and then determine if it is prime. * A palindrome is a number that is the same forwards and backwards: 121, 323, 123454321, etc. - */ \ No newline at end of file + */ + +const isPrime = (num) => { + if (num == 0 || num == 1 || num % 2 == 0) return false; + else if (num == 2) return true; + else { + for (let i = 3; i <= Math.sqrt(num); i += 2) { + if (num%i == 0) return false; + } + return true; + } +} + +const isPalindrome = (num) => { + strNum = num.toString() + for (let i = 0; i < strNum.length / 2; i++) { + if (strNum[i] != strNum[strNum.length - (1+i)]) return false + } + return true +} + +const longestPalindrome = (range) => { + for (let i = range; i > 0; i--) { + if (isPrime(i) && isPalindrome(i)) return i; + } + return undefined; +} +console.log(isPrime(68)) +console.log(isPrime(19)) +console.log(isPalindrome(12345)) +console.log(isPalindrome(123454321)) + +console.log(longestPalindrome(1000)) \ No newline at end of file From 017d0419b1ab7b2e36590706e2ca9235faa935d8 Mon Sep 17 00:00:00 2001 From: Mjax Date: Fri, 18 Aug 2017 10:01:37 -0700 Subject: [PATCH 11/18] unfinished constructors --- constructors/constructors.js | 40 ++++++++++++++++++++++++++++++++++-- 1 file changed, 38 insertions(+), 2 deletions(-) diff --git a/constructors/constructors.js b/constructors/constructors.js index 54801f6..f44dbef 100644 --- a/constructors/constructors.js +++ b/constructors/constructors.js @@ -18,6 +18,42 @@ * * Create properties for these different classes that fit with the character. * - * This is how you would structure the game objects in an actual game + * This is how you would structure the game objectsme in an actual game * application in Unity or another similar framework. - */ \ No newline at end of file + */ + +class NPC { + constructor(name) { + this.alive = true; + this.name = name; + } + attack(target) { + console.log(`${this.name} attacks ${target}`); + } + describe() { + if (this.alive) console.log(`${this.name} is alive`); + else console.log(`${this.name} is dead....`); + } + /*get name() { + return this.name; + }*/ +} + +class Humanoid extends NPC { + constructor(name) { + super(); + } +} + +class Elf extends Humanoid { +} + +const me = new Humanoid('Matt'); +const me2 = new Elf('Lego'); +const me3 = new NPC('test'); +me3.describe(); +me.attack('nic'); +me.describe(); +me.name; + +//console.log(me2.name;) \ No newline at end of file From f49ce4f81bac1afa9d85ce2d186a224f4fae023f Mon Sep 17 00:00:00 2001 From: Mjax Date: Fri, 18 Aug 2017 10:02:20 -0700 Subject: [PATCH 12/18] saved file --- constructors/constructors.js | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/constructors/constructors.js b/constructors/constructors.js index f44dbef..7ac6c57 100644 --- a/constructors/constructors.js +++ b/constructors/constructors.js @@ -44,10 +44,40 @@ class Humanoid extends NPC { super(); } } +class Animal extends NPC { +} +class Plant extends NPC { + +} class Elf extends Humanoid { } +class Human extends Humanoid { + +} +class Orc extends Humanoid { + +} +class Peasant extends Humanoid { + +} +class Bandit extends Humanoid { + +} +class Soldier extends Humanoid { + +} +class FleshEatingDaisy extends Plant { + +} +class Wolf extends Animal { + +} +class Bear extends Animal { + +} + const me = new Humanoid('Matt'); const me2 = new Elf('Lego'); const me3 = new NPC('test'); From 1a248a9f0b13025bd1b4411fb4526f5143cf58df Mon Sep 17 00:00:00 2001 From: Mjax Date: Mon, 21 Aug 2017 09:29:08 -0700 Subject: [PATCH 13/18] complete common characters --- commonCharacters/commonCharacters.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/commonCharacters/commonCharacters.js b/commonCharacters/commonCharacters.js index ec31d82..21ad053 100644 --- a/commonCharacters/commonCharacters.js +++ b/commonCharacters/commonCharacters.js @@ -6,3 +6,15 @@ * Example: commonCharacters('acexivou', 'aegihobu') * * Returns: 'aeiou' */ + +const commonCharacters = (str1, str2) => { + const commonChArr = []; + for (let i = 0; i < str1.length; i++){ + if (!commonChArr.includes(str1[i])){ + if (str2.includes(str1[i])) commonChArr.push(str1[i]); + } + } + return commonChArr; +} + +console.log(commonCharacters('apple','bananas')); From 643918e7b82d8150c2dc0b444ada436d362a3d8a Mon Sep 17 00:00:00 2001 From: Mjax Date: Tue, 22 Aug 2017 09:38:55 -0700 Subject: [PATCH 14/18] complete evenOccurene --- evenOccurences/evenOccurences.js | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/evenOccurences/evenOccurences.js b/evenOccurences/evenOccurences.js index 35da569..44be270 100644 --- a/evenOccurences/evenOccurences.js +++ b/evenOccurences/evenOccurences.js @@ -6,10 +6,26 @@ /* * * example usage: - * * const onlyEven = evenOccurence([1, 7, 2, 4, 5, 1, 6, 8, 9, 6, 4, 1]); * * console.log(onlyEven); // 4 * */ const evenOccurence = (arr) => { - // Your code here. + const occNums = {}; + arr.forEach((x) => { + if (occNums[x]) occNums[x] += 1; + else occNums[x] = 1; + }); + const occNumKeys = Object.keys(occNums); + let firstEvenOcc; + occNumKeys.forEach((x) => { + if (occNums[x] % 2 === 0) { + if (!firstEvenOcc) firstEvenOcc = x; + return; + } + }); + if (firstEvenOcc) return firstEvenOcc; + return null; }; +const onlyEven = evenOccurence([1, 7, 2, 4, 5, 1, 6, 8, 9, 6, 4, 1]); +console.log(onlyEven); + From 25e4896e212d9381f803c13e9293a19295d507f1 Mon Sep 17 00:00:00 2001 From: Mjax Date: Wed, 23 Aug 2017 09:03:32 -0700 Subject: [PATCH 15/18] ? --- evenOccurences/evenOccurences.js~ | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 evenOccurences/evenOccurences.js~ diff --git a/evenOccurences/evenOccurences.js~ b/evenOccurences/evenOccurences.js~ new file mode 100644 index 0000000..4608cb0 --- /dev/null +++ b/evenOccurences/evenOccurences.js~ @@ -0,0 +1,31 @@ +/* + * * Find the first item that occurs an even number of times in an array. + * * Remember to handle multiple even-occurance items and return the first one. + * * Return null if there are no even-occurance items. + * */ + +/* + * * example usage: + * * console.log(onlyEven); // 4 + * */ + +const evenOccurence = (arr) => { + const occNums = {}; + arr.forEach((x) => { + if (occNums[x]) occNums[x] += 1; + else occNums[x] = 1; + }); + const occNumKeys = Object.keys(occNums); + let firstEvenOcc; + occNumKeys.forEach((x) => { + if (occNums[x] % 2 === 0) { + firstEvenOcc = x; + return; + } + }); + if (firstEvenOcc) return firstEvenOcc; + return null; +}; +const onlyEven = evenOccurence([1, 7, 2, 4, 5, 1, 6, 8, 9, 6, 4, 1]); +console.log(onlyEven); + From f6147a1cbb6fddfb07394b8fdc10dfffbe62811c Mon Sep 17 00:00:00 2001 From: Mjax Date: Wed, 23 Aug 2017 09:27:31 -0700 Subject: [PATCH 16/18] complete stringCompression --- stringCompression/stringCompression.js | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/stringCompression/stringCompression.js b/stringCompression/stringCompression.js index 48db571..f9ee5ae 100644 --- a/stringCompression/stringCompression.js +++ b/stringCompression/stringCompression.js @@ -4,3 +4,19 @@ // If the "compressed" string would not become smaller than the original string, // your method should return the original string. // You can assume the string has only uppercase and lowercase letters (a - z). + +const stringCompression = (str) => { + let count = 1; + let newStr = ""; + for (let i = 0; i < str.length; i++) { + if (str[i] === str[i+1]) count += 1; + else { + newStr += `${str[i]}${count}`; + count = 1; + } + } + if (newStr.length < str.length) return newStr; + return str; +} + +console.log(stringCompression('aabcccccaaa')); From c2105a0ac6e9c20b6b2aa26bc2f126f5e0ce26df Mon Sep 17 00:00:00 2001 From: Mjax Date: Thu, 24 Aug 2017 09:17:23 -0700 Subject: [PATCH 17/18] complete vowel count --- vowelCount/vowelCount.js | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/vowelCount/vowelCount.js b/vowelCount/vowelCount.js index 27fad79..5267243 100644 --- a/vowelCount/vowelCount.js +++ b/vowelCount/vowelCount.js @@ -2,3 +2,19 @@ * Write a function that returns the count of the total number of vowels in a string. * Example: 'Hello World!' -> 3 */ +const vowelCount = (str) => { + const vowels = { + a: true, + e: true, + i: true, + o: true, + u: true + }; + let count = 0; + for (let i = 0; i < str.length; i++){ + if (vowels[str[i].toLowerCase()]) count += 1; + } + return count; +}; + +console.log(vowelCount('Hello WOrld!')); From 4ed51d319b115da1c3527c616823ec5dc38878c0 Mon Sep 17 00:00:00 2001 From: Mjax Date: Fri, 25 Aug 2017 10:13:55 -0700 Subject: [PATCH 18/18] complet mean median mode --- meanMedianMode/meanMedianMode.js | 34 ++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/meanMedianMode/meanMedianMode.js b/meanMedianMode/meanMedianMode.js index 821fdc0..5b1c637 100644 --- a/meanMedianMode/meanMedianMode.js +++ b/meanMedianMode/meanMedianMode.js @@ -2,3 +2,37 @@ * Given an array of numbers calculate the mean, median, and mode. * Return an object with properties for the mean, median, and mode. */ + +const mmmArrCalc = arr => { + arr = arr.sort((a,b)=>{return a-b}); + const countEach = {} + let sum = 0; + let med = 0; + if (arr.length %2 === 0) { + med = (arr[arr.length/2] + (arr[(arr.length/2)-1]))/2; + } else med = arr[Math.floor(arr.length/2)]; + for (let i = 0; i < arr.length; i++) { + sum += arr[i]; + if (countEach[arr[i]]) { + countEach[arr[i]] += 1; + } else { + countEach[arr[i]] = 1; + } + } + const countK = Object.keys(countEach); + let most = 0; + let mostValue = 0; + for (let i = 0; i < countK.length; i++) { + if (countEach[countK[i]] > most) { + most = countEach[countK[i]]; + mostValue = countK[i]; + } + } + const mmm = {}; + mmm.median = med; + mmm.mean = sum/arr.length; + mmm.mode = mostValue; + return mmm; +} + +console.log(mmmArrCalc([20,10,30,20]))