From 2389e178bb74cfd818f59a1539a60c5f5567c3b3 Mon Sep 17 00:00:00 2001 From: sam-crabtree Date: Tue, 8 Aug 2017 11:49:14 -0500 Subject: [PATCH 01/15] longestString challenge complete --- longestString/longestString.js | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/longestString/longestString.js b/longestString/longestString.js index 35b887c..1ef4faa 100644 --- a/longestString/longestString.js +++ b/longestString/longestString.js @@ -1,4 +1,31 @@ -/* +/* * Write a function that accepts an array of strings. * Return the longest string in the array. */ + +// 1. identify test cases +// ['abc', 'a', 'b'] -> 'abc' +// ['abc', 'def'] -> 'def' +// [] -> null + +/* +function longestString strings + loop over the array of strings + do something in the loop + return longestString +*/ + +function findLongestWord(array) { + let longestWord = ''; + + array.forEach(word => { + if(word.length > longestString.length) { + longestWord = word; + } + }); + + return longestWord; +} + +const word = findLongestWord(['Another', 'one', 'bites', 'the', 'dust']); +console.log(word); \ No newline at end of file From 86f0dbfe33efaeee870b4f57c9757087aca4f20f Mon Sep 17 00:00:00 2001 From: sam-crabtree Date: Wed, 9 Aug 2017 11:37:02 -0500 Subject: [PATCH 02/15] reverseCase.js complete --- reverseCase/reverseCase.js | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 reverseCase/reverseCase.js diff --git a/reverseCase/reverseCase.js b/reverseCase/reverseCase.js new file mode 100644 index 0000000..e2abeea --- /dev/null +++ b/reverseCase/reverseCase.js @@ -0,0 +1,21 @@ +/* + * 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. + */ + +const reverseCase = (str) => { + let newStr = ''; + + for (let i = 0; i < str.length; i++) { + let char = str[i]; + if (char === char.toLowerCase()) { + newStr = newStr + char.toUpperCase(); + } else { + newStr = newStr + char.toLowerCase(); + } + } + return newStr; + }; + + console.log(reverseCase('Hello World')); \ No newline at end of file From 52fbbe20b80d7e2e032ba5cd35fc42747c1bd96d Mon Sep 17 00:00:00 2001 From: sam-crabtree Date: Thu, 10 Aug 2017 11:12:29 -0500 Subject: [PATCH 03/15] isUnique complete --- isUnique/isUnique.js | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/isUnique/isUnique.js b/isUnique/isUnique.js index 6c9caf5..5b3b0e5 100644 --- a/isUnique/isUnique.js +++ b/isUnique/isUnique.js @@ -1,3 +1,16 @@ /* 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 + */ + +const findUnique = (str) => { + let unique=''; + + for (let i = 0; i < str.length; i++) { + if(unique.indexOf(str[i]) === -1) { + unique += str[i]; + } + } + return unique; + }; + + console.log(findUnique('This is a string')); \ No newline at end of file From 40a26d7a62ae0d7d61e2d98212b583e38750f437 Mon Sep 17 00:00:00 2001 From: sam-crabtree Date: Fri, 11 Aug 2017 11:30:22 -0500 Subject: [PATCH 04/15] callBackPractice complete --- callBackPractice/callBackPractice.js | 46 +++++++++++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) diff --git a/callBackPractice/callBackPractice.js b/callBackPractice/callBackPractice.js index 8f5d9ac..747667a 100644 --- a/callBackPractice/callBackPractice.js +++ b/callBackPractice/callBackPractice.js @@ -24,18 +24,30 @@ firstItem(foods, (firstItem) => { console.log(`The first item is ${firstItem}.`); }); +const firstItem = (arr, cb) => { + cb(arr[0]); +}; + // Write a function called getLength that passes the length of the array into the callback getLength(foods, (length) => { console.log(`The length of the array is ${length}.`); }); +const getLength = (arr, cb) => { + cb(arr.length); +}; + // Write a function called last which passes the last item of the array into the callback last(foods, (lastItem) => { console.log(`The last item in the array is ${lastItem}.`); }); +const last = (arr, cb) => { + cb(arr[arr.length - 1]); +} + // Write a function called sumNums that adds two numbers and passes the result to the callback @@ -43,12 +55,19 @@ sumNums(5, 10, (sum) => { console.log(`The sum is ${sum}.`); }); +const sumNums = (num1, num2, cb) => { + cb(num1 + num2); +}; + // Write a function called multiplyNums that adds two numbers and passes the result to the callback multiplyNums(5, 10, (product) => { console.log(`The product is ${product}.`); }); +const multiplyNums = (num1, num2, cb) => { + cb(num1 * num2); +}; // 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 @@ -56,6 +75,14 @@ contains(foods, 'ribeye', (result) => { console.log(result ? 'ribeye is in the array' : 'ribeye is not in the array'); }); +const contains = (arr, item, cb) => { + if (arr.includes(item)) { + cb(true); + } else { + cb(false); + } +}; + // 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. @@ -63,9 +90,26 @@ removeDuplicates(foods, (uniqueFoods) => { console.log(`foods with duplicates removed: ${uniqueFoods}`); }); +const removeDuplicates = (arr, cb) => { + const unique = []; + + arr.forEach((value) => { + if (!arr.includes(value)) { + arr.push(value); + } + }); + cb(unique); +}; + // Write a function called forEach that iterates over the provided array and passes the value and index into the callback. forEach(foods, (value, index) => { console.log(`${value} is at index ${index}.`); -}); \ No newline at end of file +}); + +const forEach = (arr, cb) => { + for (let i = 0; i < arr.length; i++) { + cb(arr[i], i); + } +}; \ No newline at end of file From 32d3597098def757c1bc4ba9c09acf8eaa12e394 Mon Sep 17 00:00:00 2001 From: sam-crabtree Date: Fri, 11 Aug 2017 12:27:44 -0500 Subject: [PATCH 05/15] Redid isUnique due to misunderstanding --- isUnique/isUnique.js | 32 ++++++++++++++++++++++---------- 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/isUnique/isUnique.js b/isUnique/isUnique.js index 5b3b0e5..08a47b8 100644 --- a/isUnique/isUnique.js +++ b/isUnique/isUnique.js @@ -2,15 +2,27 @@ * What if you cannot use additional data structures? */ -const findUnique = (str) => { - let unique=''; +// const findUnique = (str) => { +// let unique=''; - for (let i = 0; i < str.length; i++) { - if(unique.indexOf(str[i]) === -1) { - unique += str[i]; - } - } - return unique; - }; +// for (let i = 0; i < str.length; i++) { +// if(unique.indexOf(str[i]) === -1) { +// unique += str[i]; +// } +// } +// return unique; +// }; - console.log(findUnique('This is a string')); \ No newline at end of file +// console.log(findUnique('This is a string')); + +// ^^^ old code because i misunderstood the assignment, lol + + + const isUnique = (str) => { + for (let i = 0; i < str.length; i++) { + for (let j = i +1; j < str.length; j++) { + if (str[i] == str[j]) return false; + } + } + return true; + }; \ No newline at end of file From 8a529d2a19aba5ae65ebc336ca7af304ec657200 Mon Sep 17 00:00:00 2001 From: sam-crabtree Date: Mon, 14 Aug 2017 11:12:02 -0500 Subject: [PATCH 06/15] removeDuplicates complete --- removeDuplicates/removeDuplicates.js | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/removeDuplicates/removeDuplicates.js b/removeDuplicates/removeDuplicates.js index 970f719..da3d9ae 100644 --- a/removeDuplicates/removeDuplicates.js +++ b/removeDuplicates/removeDuplicates.js @@ -9,5 +9,14 @@ */ const removeDuplicates = (arr) => { - //code here... -}; \ No newline at end of file + let decluttered = [arr[0]]; + + for (let i = 1; i < arr.length; i++) { + if (arr[i] != arr[i-1]) { + decluttered.push(arr[i]); + } + } + return decluttered; +}; + +console.log(removeDuplicates([0, 1, 1, 2, 3, 3, 3, 4, 4, 5])); From ad1992ec68816127ac7782e3a634236917fe45a3 Mon Sep 17 00:00:00 2001 From: sam-crabtree Date: Tue, 15 Aug 2017 11:22:28 -0500 Subject: [PATCH 07/15] waterJugs complete --- brainTeasers/waterJugs.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/brainTeasers/waterJugs.md b/brainTeasers/waterJugs.md index 5276fc0..b531409 100644 --- a/brainTeasers/waterJugs.md +++ b/brainTeasers/waterJugs.md @@ -1 +1,10 @@ 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. + +1) Fill up 3 quart jug +2) Use the now full 3 quart jug to fill the 5 quart jug, twice +3) Now you're left with 1 quart in the 3 quart jug +4) Empty the 5 quart jug +5) Transfer that 1 quart of water from the 3 qt jug into the now empty 5 quart jug +6) Fill up the 3 quart jug again +7) Dump it into the 5 quart +8) You now have 4 quarts in the 5 qt jug \ No newline at end of file From 42c874c0cbf723a5ccbff58f2a9ed05f854b5006 Mon Sep 17 00:00:00 2001 From: sam-crabtree Date: Wed, 16 Aug 2017 11:32:18 -0500 Subject: [PATCH 08/15] forLoopTimeout completed --- forLoopTimeout/forLoopTimeout.js | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/forLoopTimeout/forLoopTimeout.js b/forLoopTimeout/forLoopTimeout.js index 87522c2..edd6fee 100644 --- a/forLoopTimeout/forLoopTimeout.js +++ b/forLoopTimeout/forLoopTimeout.js @@ -4,10 +4,25 @@ // 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() { +// // 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); +// } + for (var i = 1; i <= 10; i++) { - setTimeout(function() { - // 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 + setTimeout(function(x) { + return function() { + console.log(x); + }; }(i), 0); +} + +// We're only passing the refernece to the variable i +// and not the the actual value of i when it goes through each loop +// So by the time the setTimeout function actually runs, the for statement +// has already run and produced the number 11 +// Needed to pass the actual value of i at each iteration, so +// another anon function was needed as a wrapper +// Outer function executes first, and takes in a private variable x, which we pass i into From 5708e7f99cfc2610f203cdccc1acc48dbb94ac3c Mon Sep 17 00:00:00 2001 From: sam-crabtree Date: Thu, 17 Aug 2017 11:54:15 -0500 Subject: [PATCH 09/15] largestPrimePalindrome complete --- .../largestPrimePalindrome.js | 31 ++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/largestPrimePalindrome/largestPrimePalindrome.js b/largestPrimePalindrome/largestPrimePalindrome.js index 4cc99c0..957313f 100644 --- a/largestPrimePalindrome/largestPrimePalindrome.js +++ b/largestPrimePalindrome/largestPrimePalindrome.js @@ -3,4 +3,33 @@ * 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 isPalindrome = (num) => { + const num2Str = num.toString(); + + if (num2Str === num2Str.split('').reverse().join('')) return true; + return false; +} + +const isPrime = (num) => { + if (num === 1 || num === 3) return false; + for (let i = 2; i < num; i++) { + if (num % i === 0) return false; + } + return true; +} + +const lPPArray = []; + +const largestPrimePalindrome = (num) => { + for (let i = 2; i < 1000; i++) { + if (isPalindrome(i) && isPrime(i)) { + lPPArray.push(i); + } + } + return lPPArray[lPPArray.length - 1]; +} + +console.log(largestPrimePalindrome()); \ No newline at end of file From 70d0d090c4369439d1e09794fd87905aef6a1969 Mon Sep 17 00:00:00 2001 From: sam-crabtree Date: Fri, 18 Aug 2017 11:30:48 -0500 Subject: [PATCH 10/15] Constructors complete --- constructors/constructors.js | 93 +++++++++++++++++++++++++++++++++++- 1 file changed, 92 insertions(+), 1 deletion(-) diff --git a/constructors/constructors.js b/constructors/constructors.js index 54801f6..aae8248 100644 --- a/constructors/constructors.js +++ b/constructors/constructors.js @@ -20,4 +20,95 @@ * * 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(stats) { + this.hp = stats.hp; + this.strength = stats.strength; + this.speed = stats.speed; + this.luck = stats.luck; + this.dexterity = stats.dexterity; + this.defense = stats.defense; + } +} + +class Humanoid extends NPC { + constructor(stats) { + super(stats); + this.weapon = stats.weapon; + this.armor = stats.armor; + this.inventory = stats.inventory; + } +} + +class Human extends Humanoid { + constructor(stats) { + super(stats); + this.defenseBoost = stats.defense + 25; + } +} + +class Soldier extends Human { + constructor(stats) { + super(stats); + } +} + +class Peasant extends Human { + constructor(stats) { + super(stats); + } +} + +class Bandit extends Human { + constructor(stats) { + super(stats); + } +} + +class Elf extends Humanoid { + constructor(stats) { + super(stats); + this.dexterityBoost = stats.dexterity + 25; + } +} + +class Orc extends Humanoid { + constructor(stats) { + super(stats); + this.StrengthBoost = stats.strength + 25; + } +} + +class Animal extends NPC { + constructor(stats) { + super(stats); + this.hostility = stats.hostility; + } +} + +class Bear extends Animal { + constructor(stats) { + super(stats); + } +} + +class Wolf extends Animal { + constructor(stats) { + super(stats); + } +} + +class Plant extends NPC { + constructor(stats) { + super(stats); + this.magicProperties = stats.magicProperties; + } +} + +class FleshEatingDaisy extends Plant { + constructor(stats) { + super(stats); + } +} \ No newline at end of file From e2f5846f473c3657a1cfe5c992ab870d80d7f7cb Mon Sep 17 00:00:00 2001 From: sam-crabtree Date: Mon, 21 Aug 2017 11:49:52 -0500 Subject: [PATCH 11/15] commonCharacters complete --- commonCharacters/commonCharacters.js | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/commonCharacters/commonCharacters.js b/commonCharacters/commonCharacters.js index ec31d82..7fbebaf 100644 --- a/commonCharacters/commonCharacters.js +++ b/commonCharacters/commonCharacters.js @@ -1,8 +1,22 @@ /* * Common Characters: - * Write a function that accepts two strings as arguments, and returns only the characters that are common to both strings. * - * Your function should return the common characters in the same order that they appear in the first argument. + * Write a function that accepts two strings as arguments, + * and returns only the characters that are common to both strings. * + * Your function should return the common characters in the same order + * that they appear in the first argument. * Do not return duplicate characters and ignore whitespace in your returned string. * * Example: commonCharacters('acexivou', 'aegihobu') * * Returns: 'aeiou' */ + +const commonCharacters = function(string1, string2) { + let sameChars = ''; + for (let i = 0; i < string2.length; i++) { + if (string1.includes(string2[i])) { + sameChars += string2[i]; + } + } + return `Identical characters: ${sameChars}`; +}; + +console.log(commonCharacters('acexivou', 'aegihobu')); \ No newline at end of file From 1d74ed59c5a1e9c76ccd4ebf74129c3ea28a5418 Mon Sep 17 00:00:00 2001 From: sam-crabtree Date: Tue, 22 Aug 2017 11:55:07 -0500 Subject: [PATCH 12/15] evenOccurences complete --- evenOccurences/evenOccurences.js | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/evenOccurences/evenOccurences.js b/evenOccurences/evenOccurences.js index 35da569..2de3cd4 100644 --- a/evenOccurences/evenOccurences.js +++ b/evenOccurences/evenOccurences.js @@ -10,6 +10,22 @@ * * console.log(onlyEven); // 4 * */ + const evenOccurence = (arr) => { - // Your code here. + let storage = {}; + + arr.forEach(function(value, index) { + storage[value] = storage[value] + 1 || 1; + }); + + for (let i = 0; i < arr.length; i++) { + if (storage[arr[i]] % 2 === 0) { + return arr[i]; + } + } + + return null; }; + +const nums = [1, 2, 2, 3, 4, 4, 4, 5]; +console.log(evenOccurence(nums)); \ No newline at end of file From e864a78977d4feaba81ca1ff1ebc7caa75ca0c69 Mon Sep 17 00:00:00 2001 From: sam-crabtree Date: Wed, 23 Aug 2017 11:52:57 -0500 Subject: [PATCH 13/15] stringCompression complete (for now) --- stringCompression/stringCompression.js | 28 ++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/stringCompression/stringCompression.js b/stringCompression/stringCompression.js index 48db571..f8c17cc 100644 --- a/stringCompression/stringCompression.js +++ b/stringCompression/stringCompression.js @@ -4,3 +4,31 @@ // 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 = function(str) { + let count = 1; + let previous = str.charAt(0); + let compressedStr = ''; + + for (let i = 1; i < str.length; i++) { + var current = str[i]; + + if (previous === current) { + count++; + } else { + compressedStr += previous + count; + previous = current; + count = 1; + } + } + + if (count === 1) { + compressedStr += current; + } else { + compressedStr += current + count; + } + + return compressedStr; +} + +console.log(stringCompression('aaaabbbbcddd')); \ No newline at end of file From 534e76d2b2cf3122a2a582633c84ef9691c1ce45 Mon Sep 17 00:00:00 2001 From: sam-crabtree Date: Thu, 24 Aug 2017 11:33:15 -0500 Subject: [PATCH 14/15] vowelCount complete --- vowelCount/vowelCount.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/vowelCount/vowelCount.js b/vowelCount/vowelCount.js index 27fad79..1f298ac 100644 --- a/vowelCount/vowelCount.js +++ b/vowelCount/vowelCount.js @@ -2,3 +2,10 @@ * Write a function that returns the count of the total number of vowels in a string. * Example: 'Hello World!' -> 3 */ + + const vowelCount = function(str) { + let m = str.match(/[aeiou]/gi); + return m === null ? 0 : m.length; + } + + console.log(vowelCount('Hello World!')); \ No newline at end of file From ba5556da710e8df4479fefa05ace798d78e2c835 Mon Sep 17 00:00:00 2001 From: sam-crabtree Date: Fri, 25 Aug 2017 11:55:43 -0500 Subject: [PATCH 15/15] meanMedianMode complete --- meanMedianMode/meanMedianMode.js | 54 ++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/meanMedianMode/meanMedianMode.js b/meanMedianMode/meanMedianMode.js index 821fdc0..a21b0bf 100644 --- a/meanMedianMode/meanMedianMode.js +++ b/meanMedianMode/meanMedianMode.js @@ -2,3 +2,57 @@ * Given an array of numbers calculate the mean, median, and mode. * Return an object with properties for the mean, median, and mode. */ + +const mean = function(arr) { + let total = 0, i; + + for (i = 0; i < arr.length; i++) { + total += arr[i]; + } + return total / arr.length; +} + +const median = function(arr) { + let median = 0; + + if (arr.length % 2 === 0) { + median = (arr[arr.length / 2 - 1] + arr[arr.length / 2] / 2); + } else { + median = numbers[(arr.length - 1) / 2]; + } + return median; +} + +const mode = function(arr) { + let modes = []; + let count = []; + let i = 0; + let number = 0; + let maxIndex = 0; + + for (i = 0; i < arr.length; i++) { + number = arr[i]; + count[number] = (count[number] || 0) + 1; + if (count[number] > maxIndex) { + maxIndex = count[number]; + } + } + + for (i in count) { + if (count.hasOwnProperty(i)) { + if (count[i] === maxIndex) { + modes.push(Number(i)); + } + } + return modes; + } +} + +function meanMedianMode(arr) { + const mMMObj = { + mean: mean(arr), + median: median(arr), + mode: mode(arr) + }; + return mMMObj; +} \ No newline at end of file