From 87000a347463f9ac91f39c065604505550243a1c Mon Sep 17 00:00:00 2001 From: Frank Faustino Date: Tue, 8 Aug 2017 09:51:38 -0700 Subject: [PATCH 01/35] Completed --- longestString/longestString.js | 35 +++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/longestString/longestString.js b/longestString/longestString.js index 35b887c..b33ed32 100644 --- a/longestString/longestString.js +++ b/longestString/longestString.js @@ -1,4 +1,37 @@ -/* +/* 0. Instructions * Write a function that accepts an array of strings. * Return the longest string in the array. */ + +/* 1. Identify Test Cases + const a = ['abc', 'a', 'b'] // -> 'abc' + const b = ['abc', 'def'] // -> 'def' + const c = [] // -> null + const d = ['asdfasfas', 'sdfadfsa', 'asfasfasdfasfasfd', 's'] // -> 'asfasfasdfasfasfd' +*/ + +/* 2. Pseudo-Code +function longestString strings + loop over the array of strings + do something in the loop + return longestString +*/ + +/* 3a. Solution #1 +const longestStr = (arr) => { + let x = ''; + for (let i = 0; i < arr.length; i++) { + if (arr[i].length > x.length) x = arr[i]; + } + return x; +}; +*/ + +// 3b. Solution #4 +const longestStr = (arr) => { + let x = ''; + arr.forEach(i => { + if (i.length > x.length) x = i; + }); + return x; +} \ No newline at end of file From c9322bb27b1640a954d644f61af446486d1c887d Mon Sep 17 00:00:00 2001 From: Frank Faustino Date: Tue, 8 Aug 2017 10:02:30 -0700 Subject: [PATCH 02/35] Completed --- longestString/longestString.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/longestString/longestString.js b/longestString/longestString.js index b33ed32..21245db 100644 --- a/longestString/longestString.js +++ b/longestString/longestString.js @@ -27,7 +27,7 @@ const longestStr = (arr) => { }; */ -// 3b. Solution #4 +// 3b. Solution #2 const longestStr = (arr) => { let x = ''; arr.forEach(i => { From ff979018716acfd7ae96aebba561e0e7d765086f Mon Sep 17 00:00:00 2001 From: Frank Faustino Date: Tue, 8 Aug 2017 10:27:56 -0700 Subject: [PATCH 03/35] Completed --- longestString/longestString.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/longestString/longestString.js b/longestString/longestString.js index 21245db..2bedaf4 100644 --- a/longestString/longestString.js +++ b/longestString/longestString.js @@ -29,7 +29,7 @@ const longestStr = (arr) => { // 3b. Solution #2 const longestStr = (arr) => { - let x = ''; + let x = arr[0]; arr.forEach(i => { if (i.length > x.length) x = i; }); From c80df557ed59c8ee6c647937240fcb6729eb6b82 Mon Sep 17 00:00:00 2001 From: Frank Faustino Date: Wed, 9 Aug 2017 09:15:37 -0700 Subject: [PATCH 04/35] Completed reverseCase.js --- reverseCase/reverseCase.js | 38 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 36 insertions(+), 2 deletions(-) diff --git a/reverseCase/reverseCase.js b/reverseCase/reverseCase.js index f1051f5..261b801 100644 --- a/reverseCase/reverseCase.js +++ b/reverseCase/reverseCase.js @@ -1,5 +1,39 @@ -/* +/* 0. Instructions * 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 + */ + + /* Identify Test Cases + * const a = 'Bacon ipsum dolor amet spare ribs drumstick strip steak.' + * const b = 'HeLLo WoRLd!' + * const c = 'bvAlhatQBq' + */ + +/* 2. Pseudo-Code + * function reverseCase str + * initialize empty String variable + * loop over str + * store each element of str in variable n + * if element n is uppercase, convert to lowercase, otherwise convert to uppercase + * store that element into s + * return s + */ + +// 3a. Solution Using .split(), .map(), .join() +const reverseCase = function (str) { + return str.split('').map(function(x){ + return x === x.toUpperCase() ? x.toLowerCase() : x.toUpperCase() + }).join(''); +} + +// 3b. Solution Using for loop +const reverseCase = function (str) { + let s = ''; + for (let i=0; i Date: Wed, 9 Aug 2017 09:22:21 -0700 Subject: [PATCH 05/35] Completed reverseCase.js --- reverseCase/reverseCase.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/reverseCase/reverseCase.js b/reverseCase/reverseCase.js index 261b801..527c5a1 100644 --- a/reverseCase/reverseCase.js +++ b/reverseCase/reverseCase.js @@ -4,7 +4,7 @@ * Assume that each string will contain only spaces and letters. */ - /* Identify Test Cases + /* 1. Identify Test Cases * const a = 'Bacon ipsum dolor amet spare ribs drumstick strip steak.' * const b = 'HeLLo WoRLd!' * const c = 'bvAlhatQBq' From a27f2f9e2aa0c689709fd18fd058abfc4b842aef Mon Sep 17 00:00:00 2001 From: Frank Faustino Date: Wed, 9 Aug 2017 09:27:53 -0700 Subject: [PATCH 06/35] Completed reverseCase.js --- reverseCase/reverseCase.js | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/reverseCase/reverseCase.js b/reverseCase/reverseCase.js index 527c5a1..fb4be27 100644 --- a/reverseCase/reverseCase.js +++ b/reverseCase/reverseCase.js @@ -21,14 +21,10 @@ */ // 3a. Solution Using .split(), .map(), .join() -const reverseCase = function (str) { - return str.split('').map(function(x){ - return x === x.toUpperCase() ? x.toLowerCase() : x.toUpperCase() - }).join(''); -} +const reverseCase = str => str.split('').map((x) => x === x.toUpperCase() ? x.toLowerCase() : x.toUpperCase()).join(''); // 3b. Solution Using for loop -const reverseCase = function (str) { +const reverseCase = str => { let s = ''; for (let i=0; i Date: Wed, 9 Aug 2017 09:41:50 -0700 Subject: [PATCH 07/35] Completed reverseCase.js --- reverseCase/reverseCase.js | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/reverseCase/reverseCase.js b/reverseCase/reverseCase.js index fb4be27..9890ba3 100644 --- a/reverseCase/reverseCase.js +++ b/reverseCase/reverseCase.js @@ -4,7 +4,7 @@ * Assume that each string will contain only spaces and letters. */ - /* 1. Identify Test Cases +/* 1. Identify Test Cases * const a = 'Bacon ipsum dolor amet spare ribs drumstick strip steak.' * const b = 'HeLLo WoRLd!' * const c = 'bvAlhatQBq' @@ -21,15 +21,18 @@ */ // 3a. Solution Using .split(), .map(), .join() -const reverseCase = str => str.split('').map((x) => x === x.toUpperCase() ? x.toLowerCase() : x.toUpperCase()).join(''); +const reverseCase = str => + str + .split('') + .map(x => (x === x.toUpperCase() ? x.toLowerCase() : x.toUpperCase())) + .join(''); // 3b. Solution Using for loop const reverseCase = str => { - let s = ''; - for (let i=0; i Date: Thu, 10 Aug 2017 10:02:35 -0700 Subject: [PATCH 08/35] Completed isUnique.js --- isUnique/isUnique.js | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/isUnique/isUnique.js b/isUnique/isUnique.js index 6c9caf5..aab0d29 100644 --- a/isUnique/isUnique.js +++ b/isUnique/isUnique.js @@ -1,3 +1,24 @@ -/* Implement an algorithm to determine if a string has all unique characters. +/* 0. Instructions + * 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 + */ + +/* 1. Identify Test Cases + * const a = 'Bacon ipsum dolor amet spare ribs drumstick strip steak.' + * const b = 'HeLLo WORLd!' + * const c = 'bvAlhatQBq' + */ + +/* 2. Pseudo-Code + * function with string param + * initialize empty string + * store unique characters in empty string + * compare new string with original string + * return true if has unique characters, otherwise return false + */ + +// 3. Solution using an Array +const uniqueChars = str => { + let x = Array.from(new Set(str)); + console.log(x !== str); +}; From eb80a8f93a54caade321ae45f0ce4ea1acfe1d2c Mon Sep 17 00:00:00 2001 From: Frank Faustino Date: Thu, 10 Aug 2017 11:13:29 -0700 Subject: [PATCH 09/35] Completed isUnique.js --- isUnique/isUnique.js | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/isUnique/isUnique.js b/isUnique/isUnique.js index aab0d29..6ee67f6 100644 --- a/isUnique/isUnique.js +++ b/isUnique/isUnique.js @@ -4,9 +4,9 @@ */ /* 1. Identify Test Cases - * const a = 'Bacon ipsum dolor amet spare ribs drumstick strip steak.' - * const b = 'HeLLo WORLd!' - * const c = 'bvAlhatQBq' + * const a = 'Bacon ipsum dolor amet spare ribs drumstick strip steak.' // false + * const b = 'HeLLo WORLd!' // false + * const c = 'bvAlh!(@^$%#)atQBq' // true */ /* 2. Pseudo-Code @@ -17,8 +17,20 @@ * return true if has unique characters, otherwise return false */ -// 3. Solution using an Array -const uniqueChars = str => { - let x = Array.from(new Set(str)); - console.log(x !== str); +// 3a. Solution using for loop / indexOf() +const isUnique = str => { + let x = ''; + + for (let i = 0; i < str.length; i++) { + if (x.indexOf(str[i]) === -1) { + x += str[i]; + } + } + console.log(x === str); // return true if string contains unique characters +}; + +// 3b. Solution using an Array.from() / new Set() / .join() +const isUnique = str => { + let x = Array.from(new Set(str)).join(''); + console.log(x === str); // return true if string contains unique characters }; From 8c2c4d07ac2bd58e49c65eb75e3a177966a5633f Mon Sep 17 00:00:00 2001 From: Frank Faustino Date: Thu, 10 Aug 2017 11:23:57 -0700 Subject: [PATCH 10/35] Completed isUnique.js --- isUnique/isUnique.js | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/isUnique/isUnique.js b/isUnique/isUnique.js index 6ee67f6..abf0883 100644 --- a/isUnique/isUnique.js +++ b/isUnique/isUnique.js @@ -3,11 +3,10 @@ * What if you cannot use additional data structures? */ -/* 1. Identify Test Cases - * const a = 'Bacon ipsum dolor amet spare ribs drumstick strip steak.' // false - * const b = 'HeLLo WORLd!' // false - * const c = 'bvAlh!(@^$%#)atQBq' // true - */ +// 1. Identify Test Cases +// const a = 'Bacon ipsum dolor amet spare ribs drumstick strip steak.' // false +// const b = 'HeLLo WORLd!' // false +// const c = 'bvAlh!(@^$%#)atQBq' // true /* 2. Pseudo-Code * function with string param @@ -31,6 +30,12 @@ const isUnique = str => { // 3b. Solution using an Array.from() / new Set() / .join() const isUnique = str => { - let x = Array.from(new Set(str)).join(''); + const x = Array.from(new Set(str)).join(''); console.log(x === str); // return true if string contains unique characters }; + +// 3c. Solution using spread operator / new Set() / .join() +const isUnique = str => { + const u = x => [...new Set(x)].join(''); + console.log(u(str) === str); // return true if string contains unique characters +}; From ac6497fa3b216facb7bcaf9f094b737f993e666d Mon Sep 17 00:00:00 2001 From: Frank Faustino Date: Thu, 10 Aug 2017 11:27:01 -0700 Subject: [PATCH 11/35] Completed isUnique.js --- isUnique/isUnique.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/isUnique/isUnique.js b/isUnique/isUnique.js index abf0883..c551b83 100644 --- a/isUnique/isUnique.js +++ b/isUnique/isUnique.js @@ -36,6 +36,6 @@ const isUnique = str => { // 3c. Solution using spread operator / new Set() / .join() const isUnique = str => { - const u = x => [...new Set(x)].join(''); - console.log(u(str) === str); // return true if string contains unique characters + const x = [...new Set(str)].join(''); + console.log(x === str); // return true if string contains unique characters }; From bd705c34373c26b219eed4b88b6ca8fbccfc6af2 Mon Sep 17 00:00:00 2001 From: Frank Faustino Date: Fri, 11 Aug 2017 09:25:06 -0700 Subject: [PATCH 12/35] Completed callBackPractice --- callBackPractice/callBackPractice.js | 45 +++++++++++++++++++--------- 1 file changed, 31 insertions(+), 14 deletions(-) diff --git a/callBackPractice/callBackPractice.js b/callBackPractice/callBackPractice.js index 8f5d9ac..4165ec9 100644 --- a/callBackPractice/callBackPractice.js +++ b/callBackPractice/callBackPractice.js @@ -15,57 +15,74 @@ * */ - // Write a function called firstItem that passes the first item of the given array to the callback function - -const foods = ['pineapple', 'mango', 'ribeye', 'curry', 'tacos', 'ribeye', 'mango']; - -firstItem(foods, (firstItem) => { +const firstItem = (foods, cb) => cb(foods[0]); + +const foods = [ + 'pineapple', + 'mango', + 'ribeye', + 'curry', + 'tacos', + 'ribeye', + 'mango' +]; + +firstItem(foods, firstItem => { console.log(`The first item is ${firstItem}.`); }); // Write a function called getLength that passes the length of the array into the callback +const getLength = (length, cb) => cb(foods.length); -getLength(foods, (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 = (foods, cb) => cb(foods[foods.length - 1]); -last(foods, (lastItem) => { +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 = (a, b, cb) => cb(a + b); - -sumNums(5, 10, (sum) => { +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) => { +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 = (foods, i, cb) => cb(foods.indexOf(i) > -1); -contains(foods, 'ribeye', (result) => { +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 = (foods, cb) => cb(Array.from(new Set(foods))); -removeDuplicates(foods, (uniqueFoods) => { +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 = (foods, cb) => { + for (let i = 0; i < foods.length; i++) { + cb(foods[i], i); + } +}; forEach(foods, (value, index) => { console.log(`${value} is at index ${index}.`); -}); \ No newline at end of file +}); From a47cf9a36df8e57c35db508a059142994603419d Mon Sep 17 00:00:00 2001 From: Frank Faustino Date: Mon, 14 Aug 2017 09:54:48 -0700 Subject: [PATCH 13/35] Completed removeDuplicates.js --- removeDuplicates/removeDuplicates.js | 45 ++++++++++++++++++++++++++-- 1 file changed, 42 insertions(+), 3 deletions(-) diff --git a/removeDuplicates/removeDuplicates.js b/removeDuplicates/removeDuplicates.js index 970f719..932aa7d 100644 --- a/removeDuplicates/removeDuplicates.js +++ b/removeDuplicates/removeDuplicates.js @@ -1,4 +1,4 @@ -/* +/* 0. Instructions * Write a function that takes an array and removes all duplicate items. * [1, 1, 1, 2, 2, 3, 4, 5, 5] -> [1, 2, 3, 4, 5] * @@ -8,6 +8,45 @@ * you're more than likely using a for loop under the hood. */ +// 1. Identify Test Cases +const a = [1, 1, 1, 2, 2, 3, 4, 5, 5] // -> [1, 2, 3, 4, 5] +const b = ["spray", "spray", "limit", "elite", "elite", "exuberant", "destruction", "present"] + +/* 2. Pseudo-Code + initialize array with first element + loop thrugh arr + compare second element with first element + */ + +// 3a. Solution #1 using for loop const removeDuplicates = (arr) => { - //code here... -}; \ No newline at end of file + let seen = {}; + let result = []; + let j = 0; + for (let i = 0; i < arr.length; i++) { + if (seen[arr[i]] !== 1) { + seen[arr[i]] = 1; + result[j++] = arr[i]; + } + } + return result; +} + +console.log(removeDuplicates(a)) +console.log(removeDuplicates(b)) + +// 3b. Solution #1 +// const removeDuplicates = (arr) => { +// return arr.filter((element, index, array) => { // filter creates new array from elements that return true +// return !index || element != array[index - 1]; +// }) +// }; + +// console.log(removeDuplicates(a)) +// console.log(removeDuplicates(b)) + +// 3c. Solution #3 using new Set(); +// const removeDuplicates = (arr) => Array.from(new Set(arr)); + +// console.log(removeDuplicates(a)) +// console.log(removeDuplicates(b)) \ No newline at end of file From 1aacd4c2b32fc15f68aa9614da1c3311ae9744b8 Mon Sep 17 00:00:00 2001 From: Frank Faustino Date: Mon, 14 Aug 2017 09:56:38 -0700 Subject: [PATCH 14/35] Completed removeDuplicates.js --- removeDuplicates/removeDuplicates.js | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/removeDuplicates/removeDuplicates.js b/removeDuplicates/removeDuplicates.js index 932aa7d..bf1f734 100644 --- a/removeDuplicates/removeDuplicates.js +++ b/removeDuplicates/removeDuplicates.js @@ -12,13 +12,7 @@ const a = [1, 1, 1, 2, 2, 3, 4, 5, 5] // -> [1, 2, 3, 4, 5] const b = ["spray", "spray", "limit", "elite", "elite", "exuberant", "destruction", "present"] -/* 2. Pseudo-Code - initialize array with first element - loop thrugh arr - compare second element with first element - */ - -// 3a. Solution #1 using for loop +// 2a. Solution #1 using for loop const removeDuplicates = (arr) => { let seen = {}; let result = []; @@ -35,7 +29,7 @@ const removeDuplicates = (arr) => { console.log(removeDuplicates(a)) console.log(removeDuplicates(b)) -// 3b. Solution #1 +// 2b. Solution #1 // const removeDuplicates = (arr) => { // return arr.filter((element, index, array) => { // filter creates new array from elements that return true // return !index || element != array[index - 1]; @@ -45,7 +39,7 @@ console.log(removeDuplicates(b)) // console.log(removeDuplicates(a)) // console.log(removeDuplicates(b)) -// 3c. Solution #3 using new Set(); +// 2c. Solution #3 using new Set(); // const removeDuplicates = (arr) => Array.from(new Set(arr)); // console.log(removeDuplicates(a)) From f0c6e025109c894a685bc49f654bdf864649bf99 Mon Sep 17 00:00:00 2001 From: Frank Faustino Date: Tue, 15 Aug 2017 09:16:30 -0700 Subject: [PATCH 15/35] Completed waterJugs.md --- brainTeasers/waterJugs.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/brainTeasers/waterJugs.md b/brainTeasers/waterJugs.md index 5276fc0..2639733 100644 --- a/brainTeasers/waterJugs.md +++ b/brainTeasers/waterJugs.md @@ -1 +1,13 @@ 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. + + + 5 quart jug | 3 quart jug + -------------------------------- + 0 | 3 fill up 3 quart jug + 3 | 0 transfer 3 quarts to the 5 quart jug + 3 | 3 fill up 3 quart jug again + 5 | 1 fill up the 5 quart jug from the 3 quart jug with 1 quart remaining in the 3 quart jug + 0 | 1 empty the 5 quart jug + 1 | 0 transfer 1 quart from the 3 quart jug to the 5 quart jug + 1 | 3 fill up the 3 quart jug + 4 | 0 transfer the 3 quart jug to the 5 quart jug to get the desired 4 quarts of water \ No newline at end of file From 08c93f66a7a4c5c51ee1b73bf2b5780b8e593b9c Mon Sep 17 00:00:00 2001 From: Frank Faustino Date: Tue, 15 Aug 2017 09:16:57 -0700 Subject: [PATCH 16/35] Completed waterJugs.md --- brainTeasers/waterJugs.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/brainTeasers/waterJugs.md b/brainTeasers/waterJugs.md index 2639733..6be9f5e 100644 --- a/brainTeasers/waterJugs.md +++ b/brainTeasers/waterJugs.md @@ -1,7 +1,7 @@ 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. - 5 quart jug | 3 quart jug + 5 quart jug | 3 quart jug -------------------------------- 0 | 3 fill up 3 quart jug 3 | 0 transfer 3 quarts to the 5 quart jug From 77a3bfdf4a86c1be5edb838b5983dcd6fd142318 Mon Sep 17 00:00:00 2001 From: Frank Faustino Date: Tue, 15 Aug 2017 09:21:39 -0700 Subject: [PATCH 17/35] Completed waterJugs.md --- brainTeasers/waterJugs.md | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/brainTeasers/waterJugs.md b/brainTeasers/waterJugs.md index 6be9f5e..de27589 100644 --- a/brainTeasers/waterJugs.md +++ b/brainTeasers/waterJugs.md @@ -1,13 +1,13 @@ 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. - 5 quart jug | 3 quart jug - -------------------------------- - 0 | 3 fill up 3 quart jug - 3 | 0 transfer 3 quarts to the 5 quart jug - 3 | 3 fill up 3 quart jug again - 5 | 1 fill up the 5 quart jug from the 3 quart jug with 1 quart remaining in the 3 quart jug - 0 | 1 empty the 5 quart jug - 1 | 0 transfer 1 quart from the 3 quart jug to the 5 quart jug - 1 | 3 fill up the 3 quart jug - 4 | 0 transfer the 3 quart jug to the 5 quart jug to get the desired 4 quarts of water \ No newline at end of file + 5 qt jug │ 3 qt jug + ─────────────┼──────────── + 0 │ 3 fill up 3 quart jug + 3 │ 0 transfer 3 quarts to the 5 quart jug + 3 │ 3 fill up 3 quart jug again + 5 │ 1 fill up the 5 quart jug from the 3 quart jug with 1 quart remaining in the 3 quart jug + 0 │ 1 empty the 5 quart jug + 1 │ 0 transfer 1 quart from the 3 quart jug to the 5 quart jug + 1 │ 3 fill up the 3 quart jug + 4 │ 0 transfer the 3 quart jug to the 5 quart jug to get the desired 4 quarts of water \ No newline at end of file From 550f803677567c734de876187298b5ad4d9d3513 Mon Sep 17 00:00:00 2001 From: Frank Faustino Date: Wed, 16 Aug 2017 09:49:32 -0700 Subject: [PATCH 18/35] Completed forLoopTimeout.js --- forLoopTimeout/forLoopTimeout.js | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/forLoopTimeout/forLoopTimeout.js b/forLoopTimeout/forLoopTimeout.js index 87522c2..a43fd4a 100644 --- a/forLoopTimeout/forLoopTimeout.js +++ b/forLoopTimeout/forLoopTimeout.js @@ -1,13 +1,23 @@ +// 1. Instructions // 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. // 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. -for (var i = 1; i <= 10; i++) { - setTimeout(function() { +// 2a. let instead of var (ES6 fix) +// for (let i = 1; i <= 10; i++) { +// setTimeout(() => console.log(i), 0); // 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); +// } + +// 2b. setting setTimeout scope +function x(i) { + setTimeout(() => console.log(i), 0) +} + // 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. +for (var i = 1; i <= 10; i++) { + x(i); } \ No newline at end of file From dfdff897e10b5a343cbd9a6e63c60bc4d87a3a13 Mon Sep 17 00:00:00 2001 From: Frank Faustino Date: Wed, 16 Aug 2017 09:54:45 -0700 Subject: [PATCH 19/35] Completed forLoopTimeout.js --- forLoopTimeout/forLoopTimeout.js | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/forLoopTimeout/forLoopTimeout.js b/forLoopTimeout/forLoopTimeout.js index a43fd4a..437e9af 100644 --- a/forLoopTimeout/forLoopTimeout.js +++ b/forLoopTimeout/forLoopTimeout.js @@ -5,19 +5,15 @@ // The output should be 1, 2, 3, .... 10. Right now it just prints 11. // I've been asked this three times in separate interviews. -// 2a. let instead of var (ES6 fix) +// 2a. Solution: let instead of var (ES6 fix) // for (let i = 1; i <= 10; i++) { // setTimeout(() => console.log(i), 0); - // 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. // } -// 2b. setting setTimeout scope +// 2b. Solution: setting setTimeout scope function x(i) { setTimeout(() => console.log(i), 0) } - // 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. for (var i = 1; i <= 10; i++) { x(i); } \ No newline at end of file From 874b5eef4129b038e6b9c2a5b712ee81f1dc24fd Mon Sep 17 00:00:00 2001 From: Frank Faustino Date: Wed, 16 Aug 2017 10:32:11 -0700 Subject: [PATCH 20/35] Completed forLoopTimeout.js --- forLoopTimeout/forLoopTimeout.js | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/forLoopTimeout/forLoopTimeout.js b/forLoopTimeout/forLoopTimeout.js index 437e9af..0f975cc 100644 --- a/forLoopTimeout/forLoopTimeout.js +++ b/forLoopTimeout/forLoopTimeout.js @@ -5,15 +5,29 @@ // The output should be 1, 2, 3, .... 10. Right now it just prints 11. // I've been asked this three times in separate interviews. -// 2a. Solution: let instead of var (ES6 fix) +// 2a. Solution: ES6 fix. let instead of var encloses the scope of i to the for loop // for (let i = 1; i <= 10; i++) { // setTimeout(() => console.log(i), 0); // } -// 2b. Solution: setting setTimeout scope -function x(i) { - setTimeout(() => console.log(i), 0) -} +// 2b. Solution: Passing a new copy of i with each iteration to setTimeout function callback +// function x(i) { +// setTimeout(() => console.log(i), 0) +// } +// for (var i = 1; i <= 10; i++) { +// x(i); +// } + +// 2c. Solution: New scope for each iteration using IIFE, setTimeout function callback +// provided a variable j that evaluates to i within the scope of for loop +// for (var i = 1; i <= 10; i++) { +// (() => { +// var j = i; +// setTimeout(() => console.log(j), 0); +// })(); +// } + +// 2d. Solution: New scope for each iteration using IIFE (passing i as an argument) for (var i = 1; i <= 10; i++) { - x(i); + ((i) => setTimeout(() => console.log(i), 0))(i); } \ No newline at end of file From e0fb5ad2f9fc0defc493ec0e27c042d268ec2d05 Mon Sep 17 00:00:00 2001 From: Frank Faustino Date: Wed, 16 Aug 2017 10:37:02 -0700 Subject: [PATCH 21/35] Completed forLoopTimeout.js --- forLoopTimeout/forLoopTimeout.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/forLoopTimeout/forLoopTimeout.js b/forLoopTimeout/forLoopTimeout.js index 0f975cc..bb752b1 100644 --- a/forLoopTimeout/forLoopTimeout.js +++ b/forLoopTimeout/forLoopTimeout.js @@ -19,15 +19,15 @@ // } // 2c. Solution: New scope for each iteration using IIFE, setTimeout function callback -// provided a variable j that evaluates to i within the scope of for loop +// provided a variable j that evaluates to i within the scope of for loop // for (var i = 1; i <= 10; i++) { // (() => { -// var j = i; -// setTimeout(() => console.log(j), 0); +// var j = i; +// setTimeout(() => console.log(j), 0); // })(); // } // 2d. Solution: New scope for each iteration using IIFE (passing i as an argument) for (var i = 1; i <= 10; i++) { - ((i) => setTimeout(() => console.log(i), 0))(i); + ((i) => setTimeout(() => console.log(i), 0))(i); } \ No newline at end of file From e0b2ac172139f82d8a85d1f846615ea6ec5e6483 Mon Sep 17 00:00:00 2001 From: Frank Faustino Date: Fri, 18 Aug 2017 01:07:01 -0700 Subject: [PATCH 22/35] Completed largestPrimePalindrome.js --- .../largestPrimePalindrome.js | 39 ++++++++++++++++--- 1 file changed, 33 insertions(+), 6 deletions(-) diff --git a/largestPrimePalindrome/largestPrimePalindrome.js b/largestPrimePalindrome/largestPrimePalindrome.js index 4cc99c0..a03a587 100644 --- a/largestPrimePalindrome/largestPrimePalindrome.js +++ b/largestPrimePalindrome/largestPrimePalindrome.js @@ -1,6 +1,33 @@ -/* - * Create a function that returns the largest prime palindrome less than 1000. - * 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 = n => { + let divisor = 3, + limit = Math.sqrt(n); + + if (n === 2 || n === 3) return true; + if (n % 2 === 0) return false; + + while (divisor <= limit) { + if (n % divisor === 0) return false; + else divisor += 2; + } + return true; +} +const isPalindrome = n => { + let str = n.toString(), + len = str.length; + + for (let i = 0; i < len / 2; i++) { + if (str[i] !== str[len -1 -i]) + return false; + } + return true; +} +const largestPrimePalindrome = n => { + for (let i = n; i > 0; i--) { + if (isPrime(i) && isPalindrome(i)) return i; + } +} + +console.log(largestPrimePalindrome(1000)) + + From ac9f2c8d9b8f4c10f202aa348b9a9cc65101b3a7 Mon Sep 17 00:00:00 2001 From: Frank Faustino Date: Fri, 18 Aug 2017 01:07:54 -0700 Subject: [PATCH 23/35] Completed largestPrimePalindrome.js --- largestPrimePalindrome/largestPrimePalindrome.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/largestPrimePalindrome/largestPrimePalindrome.js b/largestPrimePalindrome/largestPrimePalindrome.js index a03a587..3fbbb1c 100644 --- a/largestPrimePalindrome/largestPrimePalindrome.js +++ b/largestPrimePalindrome/largestPrimePalindrome.js @@ -1,4 +1,3 @@ - const isPrime = n => { let divisor = 3, limit = Math.sqrt(n); @@ -12,6 +11,7 @@ const isPrime = n => { } return true; } + const isPalindrome = n => { let str = n.toString(), len = str.length; @@ -22,6 +22,7 @@ const isPalindrome = n => { } return true; } + const largestPrimePalindrome = n => { for (let i = n; i > 0; i--) { if (isPrime(i) && isPalindrome(i)) return i; From 07346e0e9a8bafc524c4f2339a3bbb355a53acab Mon Sep 17 00:00:00 2001 From: Frank Faustino Date: Mon, 21 Aug 2017 01:18:33 -0700 Subject: [PATCH 24/35] Completed constructors.js --- constructors/constructors.js | 129 ++++++++++++++++++++++++++++++++++- 1 file changed, 127 insertions(+), 2 deletions(-) diff --git a/constructors/constructors.js b/constructors/constructors.js index 54801f6..26e0888 100644 --- a/constructors/constructors.js +++ b/constructors/constructors.js @@ -8,7 +8,7 @@ * Human -> Soldier, Peasant, Bandit * * NPC should be a general class for a non-player character in the game. - * This class will probably include general attributes like hp, strength, speed, etc. + * This class will probably include general attributes like hp, str, speed, etc. * * Humanoid, Animal, and Plant should all inherit from NPC. The classes * shown to the right of the arrow are classes that will inherit @@ -20,4 +20,129 @@ * * This is how you would structure the game objects in an actual game * application in Unity or another similar framework. - */ \ No newline at end of file + */ + +class NPC { + constructor(options) { + this.con = options.con; + this.str = options.str; + this.dex = options.dex; + this.int = options.int; + this.wis = options.wis; + this.cha = options.cha; + } +} + +// Humanoid +// ↳ Human +// ↳ Fighter → Thief → Wizard +// ↳ Elf +// ↳ Ranger → Rogue → Druid +// ↳ Orc +// ↳ Barbarian → Warlock → Shaman +class Humanoid extends NPC { + constructor(options) { + super(options); + } +} + +class Human extends Humanoid { + constructor(options) { + super(options); + this.name = options.name; + } +} + +class Fighter extends Human { + constructor(options) { + super(options); + } +} + +class Thief extends Human { + constructor(options) { + super(options); + } +} + +class Wizard extends Human { + constructor(options) { + super(options); + this.abilities = ['Magic Missile', 'Shock Pulse', 'Spectral Blade', 'Electrocute']; + } +} +class Elf extends NPC { + constructor(options) { + super(options); + } +} + +class Ranger extends Elf { + constructor(options) { + super(options); + } +} + +class Rogue extends Elf { + constructor(options) { + super(options); + } +} + +class Druid extends Elf { + constructor(options) { + super(options); + } +} + +class Orc extends NPC { + constructor(options) { + super(options); + } +} + +class Barbarian extends Orc { + constructor(options) { + super(options); + } +} + +class Warlock extends Orc { + constructor(options) { + super(options); + } +} + +class Shaman extends Orc { + constructor(options) { + super(options); + } +} +// Animal +// ↳ Tiny +// ↳ Rat → Lizard → Hawk +// ↳ Large +// ↳ Brown Bear → Dire Wolf → Horse +// ↳ Huge +// ↳ Giant Octopus → Nemean Lion → Roc +class Animal extends NPC { + constructor(options) { + super(options); + } + } + +// Monster +// ↳ Dragon +// ↳ Adult Dragon → Wyrm → Faerie Dragon +// ↳ Giant +// ↳ Ogre → Ettin → Cyclops +// ↳ Undead +// ↳ Lich → Zombie → Ghoul +class Monster extends NPC { + constructor(options) { + super(options); + } + } + +var frank = new Wizard({ name: 'Frank', con: 4, str: 3, dex: 3, int: 10, wis: 10, cha: 8 }); +console.log(frank); \ No newline at end of file From 7f3fb3b6accc0bb2a2fe47fdcecd217fd9563a3a Mon Sep 17 00:00:00 2001 From: Frank Faustino Date: Wed, 23 Aug 2017 08:54:23 -0700 Subject: [PATCH 25/35] Completed evenOccurences.js --- evenOccurences/evenOccurences.js | 49 +++++++++++++++++++++++--------- 1 file changed, 36 insertions(+), 13 deletions(-) diff --git a/evenOccurences/evenOccurences.js b/evenOccurences/evenOccurences.js index 35da569..06d7a03 100644 --- a/evenOccurences/evenOccurences.js +++ b/evenOccurences/evenOccurences.js @@ -1,15 +1,38 @@ -/* - * * 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: - * * const onlyEven = evenOccurence([1, 7, 2, 4, 5, 1, 6, 8, 9, 6, 4, 1]); - * * console.log(onlyEven); // 4 - * */ +/* Instructions + * 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. + */ + +// Test Cases +const onlyEven = [1, 7, 2, 4, 5, 1, 6, 8, 9, 6, 4, 1]; // 4 const evenOccurence = (arr) => { - // Your code here. -}; + let a = [...new Set(arr)], + b = 0; + for (i in a) { + b = 0; + for (j in arr) { + if (a[i] === arr[j]) { + b++; + } + } + if (b % 2 === 0) return a[i]; + } + return null; +} + +// const evenOccurence = arr => { +// let a = {}; + +// arr.forEach((i) => { +// if (a[i]) a[i] += 1; +// else a[i] = 1; +// }) +// for (let k in a) { +// if (a[k] % 2 === 0) return Number(k); +// } +// return null; +// }; + +console.log(evenOccurence(onlyEven)); From cbd8df23db345ea2148caf7e5a6fe41c75289cae Mon Sep 17 00:00:00 2001 From: Frank Faustino Date: Wed, 23 Aug 2017 08:56:14 -0700 Subject: [PATCH 26/35] Completed evenOccurences.js --- evenOccurences/evenOccurences.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/evenOccurences/evenOccurences.js b/evenOccurences/evenOccurences.js index 06d7a03..90327c5 100644 --- a/evenOccurences/evenOccurences.js +++ b/evenOccurences/evenOccurences.js @@ -8,8 +8,8 @@ const onlyEven = [1, 7, 2, 4, 5, 1, 6, 8, 9, 6, 4, 1]; // 4 const evenOccurence = (arr) => { - let a = [...new Set(arr)], - b = 0; + let a = [...new Set(arr)], + b = 0; for (i in a) { b = 0; for (j in arr) { From 8ddaf177f0482ecf853df597cc3842ce41be191c Mon Sep 17 00:00:00 2001 From: Frank Faustino Date: Wed, 23 Aug 2017 08:56:43 -0700 Subject: [PATCH 27/35] Completed evenOccurences.js --- evenOccurences/evenOccurences.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/evenOccurences/evenOccurences.js b/evenOccurences/evenOccurences.js index 90327c5..107e5ad 100644 --- a/evenOccurences/evenOccurences.js +++ b/evenOccurences/evenOccurences.js @@ -8,7 +8,7 @@ const onlyEven = [1, 7, 2, 4, 5, 1, 6, 8, 9, 6, 4, 1]; // 4 const evenOccurence = (arr) => { - let a = [...new Set(arr)], + let a = [...new Set(arr)], b = 0; for (i in a) { b = 0; From 2cb8696622fb09593c2335b8e3c0950f1c3584a7 Mon Sep 17 00:00:00 2001 From: Frank Faustino Date: Wed, 23 Aug 2017 09:20:43 -0700 Subject: [PATCH 28/35] Completed stringCompression.js --- stringCompression/stringCompression.js | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/stringCompression/stringCompression.js b/stringCompression/stringCompression.js index 48db571..259f608 100644 --- a/stringCompression/stringCompression.js +++ b/stringCompression/stringCompression.js @@ -4,3 +4,24 @@ // 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). + +// Test Case +const a = "aabcccccaaa"; // 'a2b1c5a3' + +// Solution +const stringCompression = str => { + let a = [], + count = 1, + i; + for (i = 0; i < str.length; i++) { + if (str[i] === str[i + 1]) { + count++; + } else if (str[i] != str[i + 1]) { + a.push(`${str[i]}${count}`); + count = 1; + } + } + return a.join(''); +}; + +console.log(stringCompression(a)); From aa8af7ec7eb05e8dc901aafcb1596b9e49525fd6 Mon Sep 17 00:00:00 2001 From: Frank Faustino Date: Thu, 24 Aug 2017 12:03:32 -0700 Subject: [PATCH 29/35] Completed stringCompression.js / Added check for 'compressed' string.length --- stringCompression/stringCompression.js | 39 ++++++++++++++++++++++++-- 1 file changed, 37 insertions(+), 2 deletions(-) diff --git a/stringCompression/stringCompression.js b/stringCompression/stringCompression.js index 259f608..a9a3f02 100644 --- a/stringCompression/stringCompression.js +++ b/stringCompression/stringCompression.js @@ -7,6 +7,7 @@ // Test Case const a = "aabcccccaaa"; // 'a2b1c5a3' +const b = "qwertyuiop"; // 'qwertyuiop' // Solution const stringCompression = str => { @@ -16,12 +17,46 @@ const stringCompression = str => { for (i = 0; i < str.length; i++) { if (str[i] === str[i + 1]) { count++; - } else if (str[i] != str[i + 1]) { + } else if (str[i] !== str[i + 1]) { a.push(`${str[i]}${count}`); count = 1; } } - return a.join(''); + // added conditional check to see if "compressed" string is longer than original string + const result = a.join(''); + return result.length >= str.length ? str : result; }; console.log(stringCompression(a)); + +// Tai's Solution + // currentChar = null + // compressedString = '' + // charCount = 1 + // use a for loop, i = 0, until string.length, i++ + // if string.charAt(i) is equal to currentChar, charCount++ + // if (currentChar === null, currentChar = string.charAt(i) + // if (string.charAt(i) !== currentChar || string.charAt(i) === undefined + // compressedString += currentChar; + // compressedString += charCount; + // charCount = 1; + // currentChar = string.charAt(i); + // return using ternary statement (compressedString.length > string.length ? string + // : compressedString; +// const compressString = (string) => { +// let currentChar = string.charAt(0); +// let compressedString = ''; +// let charCount = 0; +// for (let i = 0; i <= string.length; i++) { +// if (string.charAt(i) === currentChar) charCount++; +// if (string.charAt(i) !== currentChar || string.charAt(i) === undefined) { +// compressedString += currentChar; +// compressedString += charCount; +// charCount = 1; +// currentChar = string.charAt(i); +// } +// } +// return compressedString.length > string.length ? string : compressedString; +// }; + +// console.log(compressString('qwertyuiop')); \ No newline at end of file From 832a0c9832f3eacd721ce0ac4ababf8c15576e11 Mon Sep 17 00:00:00 2001 From: Frank Faustino Date: Fri, 25 Aug 2017 00:02:34 -0700 Subject: [PATCH 30/35] Completed vowelCount.js --- vowelCount/vowelCount.js | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/vowelCount/vowelCount.js b/vowelCount/vowelCount.js index 27fad79..4434f3f 100644 --- a/vowelCount/vowelCount.js +++ b/vowelCount/vowelCount.js @@ -1,4 +1,24 @@ -/* +/* Instructions * Write a function that returns the count of the total number of vowels in a string. * Example: 'Hello World!' -> 3 */ + +// Test Case +const a = 'Hello World!'; // 3 +const b = ''; // 0 +const c = 'Disrupt jean shorts viral hella meh, plaid cupidatat magna art party.' // 20 + +/* Pseudo-Code + * init vowels variable + * + */ + + // Solution using match() and RegEx +// const vowelCount = str => (str.match(/[aeiou]/gi) || []).length; + +// Solution using Array.from(), filter() and includes() +const vowelCount = str => Array.from(str).filter(i => 'aeiou'.includes(i)).length; + +console.log(vowelCount(a)); +console.log(vowelCount(b)); +console.log(vowelCount(c)); \ No newline at end of file From 9609043b7a746b403d73e7e2a1d0b30f8fbbf9bd Mon Sep 17 00:00:00 2001 From: Frank Faustino Date: Fri, 25 Aug 2017 00:03:03 -0700 Subject: [PATCH 31/35] Completed vowelCount.js --- vowelCount/vowelCount.js | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/vowelCount/vowelCount.js b/vowelCount/vowelCount.js index 4434f3f..8e91589 100644 --- a/vowelCount/vowelCount.js +++ b/vowelCount/vowelCount.js @@ -8,12 +8,7 @@ const a = 'Hello World!'; // 3 const b = ''; // 0 const c = 'Disrupt jean shorts viral hella meh, plaid cupidatat magna art party.' // 20 -/* Pseudo-Code - * init vowels variable - * - */ - - // Solution using match() and RegEx +// Solution using match() and RegEx // const vowelCount = str => (str.match(/[aeiou]/gi) || []).length; // Solution using Array.from(), filter() and includes() From 3af821d853f63a38ffe8ddc19f8fd4d4bcc9ed2e Mon Sep 17 00:00:00 2001 From: Frank Faustino Date: Fri, 25 Aug 2017 10:49:30 -0700 Subject: [PATCH 32/35] Completed vowelCount.js --- vowelCount/vowelCount.js | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/vowelCount/vowelCount.js b/vowelCount/vowelCount.js index dec42a0..a88322f 100644 --- a/vowelCount/vowelCount.js +++ b/vowelCount/vowelCount.js @@ -6,23 +6,23 @@ // Test Case const a = 'Hello World!'; // 3 const b = ''; // 0 -const c = 'Disrupt jean shorts viral hella meh, plaid cupidatat magna art party.' // 20 +const c = 'Disrupt jean shorts viral hella meh, plaid cupidatat magna art pArty.' // 20 // Solution using match() and RegEx const vowelCount = str => (str.match(/[aeiou]/gi) || []).length; // Solution using Array.from(), filter() and includes() -const vowelCount = str => Array.from(str).filter(i => 'aeiou'.includes(i)).length; +// const vowelCount = str => Array.from(str).filter(i => 'aeiouAEIOU'.includes(i)).length; // Sean's Solution using Set -const vowelCount = str => { - const vowels = new Set(['a','e','i','o','u','A','E','I','O','U']); - let count = 0; - for (let i = 0; i < str.length; i++) { - if (vowels.has(str[i])) count++; - } - return count; -} +// const vowelCount = str => { +// const vowels = new Set(['a','e','i','o','u','A','E','I','O','U']); +// let count = 0; +// for (let i = 0; i < str.length; i++) { +// if (vowels.has(str[i])) count++; +// } +// return count; +// } console.log(vowelCount(a)); console.log(vowelCount(b)); From 79a26d6855a9b5b01f113400d36ca916397b6707 Mon Sep 17 00:00:00 2001 From: Frank Faustino Date: Fri, 25 Aug 2017 11:24:25 -0700 Subject: [PATCH 33/35] Completed meanMedianMode.js --- meanMedianMode/meanMedianMode.js | 55 +++++++++++++++++++++++++++++++- 1 file changed, 54 insertions(+), 1 deletion(-) diff --git a/meanMedianMode/meanMedianMode.js b/meanMedianMode/meanMedianMode.js index 821fdc0..fb881a8 100644 --- a/meanMedianMode/meanMedianMode.js +++ b/meanMedianMode/meanMedianMode.js @@ -1,4 +1,57 @@ -/* +/* Instructions * Given an array of numbers calculate the mean, median, and mode. * Return an object with properties for the mean, median, and mode. */ + +/* Mean: Sum of values of a data set divided by number of values. + * Median: Middle value separating the greater and lesser halves of a data set + * Mode: Most frequent value in a data set + */ + +// Test Case +const a = [13, 18, 13, 14, 13, 16, 14, 21, 13]; +// const a = [13, 18, 13, 14, 13, 16, 14, 21,]; + +// Solution +const mean = arr => { + let sum = 0, + i; + for (i = 0; i < arr.length; i++) { + sum += arr[i]; + } + return sum / arr.length; +}; + +const median = arr => { + let median = 0, + len = arr.length; + arr.sort(); + if (len % 2 === 0) median = (arr[len / 2 - 1] + arr[len / 2]) / 2; + else median = arr[(len - 1) / 2]; + return median; +}; + +const mode = arr => { + const seen = {}; + arr.forEach(i => { + if (seen[i] === undefined) return (seen[i] = 1); + if (seen[i] !== undefined) return (seen[i] += 1); + + console.log(seen[i]); + }); + const tmp = Object.values(seen), + mode = tmp.reduce((a, b) => Math.max(a, b)); + return mode; + // return Math.max.apply(null, Object.values(seen)); +}; + +class MeanMedianMode { + constructor(options) { + this.mean = options.mean; + this.media = options.median; + this.mode = options.mode; + } +} + +const obj = new MeanMedianMode({ mean: mean(a), median: median(a), mode: mode(a) }); +console.log(obj); \ No newline at end of file From 3706c3f8add9a8ba54d5bbad5d76c4719c7fa402 Mon Sep 17 00:00:00 2001 From: Frank Faustino Date: Fri, 25 Aug 2017 11:27:28 -0700 Subject: [PATCH 34/35] Completed meanMedianMode.js --- meanMedianMode/meanMedianMode.js | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/meanMedianMode/meanMedianMode.js b/meanMedianMode/meanMedianMode.js index fb881a8..34646ce 100644 --- a/meanMedianMode/meanMedianMode.js +++ b/meanMedianMode/meanMedianMode.js @@ -9,8 +9,8 @@ */ // Test Case -const a = [13, 18, 13, 14, 13, 16, 14, 21, 13]; -// const a = [13, 18, 13, 14, 13, 16, 14, 21,]; +const a = [13, 18, 13, 14, 13, 16, 14, 21, 13]; // odd number of elements +// const a = [13, 18, 13, 14, 13, 16, 14, 21]; // even number of elements // Solution const mean = arr => { @@ -36,8 +36,6 @@ const mode = arr => { arr.forEach(i => { if (seen[i] === undefined) return (seen[i] = 1); if (seen[i] !== undefined) return (seen[i] += 1); - - console.log(seen[i]); }); const tmp = Object.values(seen), mode = tmp.reduce((a, b) => Math.max(a, b)); From 465aaf3c516d53620c11183980d6606151fb222b Mon Sep 17 00:00:00 2001 From: Frank Faustino Date: Fri, 25 Aug 2017 11:29:15 -0700 Subject: [PATCH 35/35] Completed meanMedianMode.js --- meanMedianMode/meanMedianMode.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/meanMedianMode/meanMedianMode.js b/meanMedianMode/meanMedianMode.js index 34646ce..85b24ea 100644 --- a/meanMedianMode/meanMedianMode.js +++ b/meanMedianMode/meanMedianMode.js @@ -46,7 +46,7 @@ const mode = arr => { class MeanMedianMode { constructor(options) { this.mean = options.mean; - this.media = options.median; + this.median = options.median; this.mode = options.mode; } }