From 172242fcce0a69064330b55dd6e7040509cc6128 Mon Sep 17 00:00:00 2001 From: Tyler Date: Wed, 9 Aug 2017 12:24:16 -0400 Subject: [PATCH 01/11] challenge 1 and 2 completed, forgot to submit yesterday --- longestString/longestString.js | 40 ++++++++++++++++++++++++++++++++++ reverseCase/reverseCase.js | 28 +++++++++++++++++++++++- 2 files changed, 67 insertions(+), 1 deletion(-) diff --git a/longestString/longestString.js b/longestString/longestString.js index 35b887c..6671083 100644 --- a/longestString/longestString.js +++ b/longestString/longestString.js @@ -2,3 +2,43 @@ * Write a function that accepts an array of strings. * Return the longest string in the array. */ + +/* function(arrayOfStrings) { + save string length of first index to variable + if variable length is less than length of next index, continue + else, next index length is the new value of the variable + return string whose length is equal to the variable +} + +*/ + +/* + longestStringFunc(['bears', 'beets', 'battlestar'])-> 'battlestar' + longestStringFunc(['one', 'two', 'three', 'four', 'five']); -> 'three' + longestStringFunc(['hey']); -> 'hey' + longestStringFunc([]); -> null +*/ + + +const longestStringFunc = (strings) => { + let longestString = ''; + if (strings.length === 0) { + return null; + } else if (strings.length === 1) { + return strings[0]; + } else { + for (let i = 0; i < strings.length; i++) { + if (strings[i].length > longestString.length) { + longestString = strings[i]; + } + } + } + return longestString +} + + + +console.log(longestStringFunc(['bears', 'beets', 'battlestar'])); +console.log(longestStringFunc(['one', 'two', 'three', 'four', 'five'])); +console.log(longestStringFunc(['hey'])); +console.log(longestStringFunc([])); \ No newline at end of file diff --git a/reverseCase/reverseCase.js b/reverseCase/reverseCase.js index f1051f5..1ff5502 100644 --- a/reverseCase/reverseCase.js +++ b/reverseCase/reverseCase.js @@ -2,4 +2,30 @@ * 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 + */ + + /* +Need to iterate over every character in the string +If it is not a letter of the alphabet (if it is a space), skip it +If it is uppercase, make it lowercase +If it is lowercase, make it uppercase +New empty string -> add iterated, changed letters to empty string + */ + + const reverseCase = (str) => { + let str2 = ''; + for (let i = 0; i < str.length; i++) { + if (str.charAt(i) === '') { + continue; + } else if (str.charAt(i) === str.charAt(i).toUpperCase()) { + str2 += str.charAt(i).toLowerCase(); + } else if (str.charAt(i) === str.charAt(i).toLowerCase()) { + str2 += str.charAt(i).toUpperCase(); + } + } + return str2; + } + +console.log(reverseCase("HeLlO")); +console.log(reverseCase("I am JuST A litTlE SeNTencE.")); +console.log(reverseCase("tHIS iz guD PraKTiS")); From 1fba9fb38f657ee482c640cb5c464597110499c7 Mon Sep 17 00:00:00 2001 From: Tyler Date: Thu, 10 Aug 2017 12:31:26 -0400 Subject: [PATCH 02/11] isUnique finished --- isUnique/isUnique.js | 48 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) diff --git a/isUnique/isUnique.js b/isUnique/isUnique.js index 6c9caf5..32e9c52 100644 --- a/isUnique/isUnique.js +++ b/isUnique/isUnique.js @@ -1,3 +1,49 @@ /* 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 + */ + + /* +Need to iterate through a string to see if each character is unique + -> create new string variable to hold characters as they are separted from the string argument + if so, return true +otherwise, return false + */ + +//If you want to count spaces as characters: + + const isUnique = (str) => { + let newStr = ''; + for (let i = 0; i < str.length; i++) { + if(newStr.includes(str.charAt(i))) { + return false; + } else { + newStr += str.charAt(i); + } + } + return true; + } +console.log(isUnique('hello')); +console.log(isUnique('hey there')); +console.log(isUnique('name')); +console.log(isUnique('orange')); +console.log(isUnique('Hi man bro')); + + +//Otherwise: + +const isUniqueOther = (str2) => { + let newStr2 = ''; + for (let i = 0; i < str2.length; i++) { + if(newStr2.includes(str2.charAt(i))) { + return false; + } else if (str2.charAt(i) !== ' ') { + newStr2 += str2.charAt(i); + } + } + return true; +} +console.log(isUniqueOther('hello')); +console.log(isUniqueOther('hey there')); +console.log(isUniqueOther('name')); +console.log(isUniqueOther('orange')); +console.log(isUniqueOther('Hi man bro')); From 3bec9da49a8687c4d296bfed93915fbaf86c6ec7 Mon Sep 17 00:00:00 2001 From: Tyler Date: Mon, 14 Aug 2017 12:11:47 -0400 Subject: [PATCH 03/11] finished callBackPractice --- callBackPractice/callBackPractice.js | 30 +++++++++++++++++++++++++++- removeDuplicates/removeDuplicates.js | 25 +++++++++++++++++------ 2 files changed, 48 insertions(+), 7 deletions(-) diff --git a/callBackPractice/callBackPractice.js b/callBackPractice/callBackPractice.js index 8f5d9ac..1d4af6f 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 = () => { + firstItem(foods); +} + // 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 = () => { + length(foods); +} + // 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 = () => { + lastItem(foods); +} + // Write a function called sumNums that adds two numbers and passes the result to the callback @@ -43,12 +55,20 @@ sumNums(5, 10, (sum) => { console.log(`The sum is ${sum}.`); }); +const sumNums = () => { + +} + // 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 = () => { + +} + // 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 +76,10 @@ contains(foods, 'ribeye', (result) => { console.log(result ? 'ribeye is in the array' : 'ribeye is not in the array'); }); +const contains = () => { + +} + // 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 +87,13 @@ removeDuplicates(foods, (uniqueFoods) => { console.log(`foods with duplicates removed: ${uniqueFoods}`); }); +const removeDuplicates = () => { + +} + // 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 +}); diff --git a/removeDuplicates/removeDuplicates.js b/removeDuplicates/removeDuplicates.js index 970f719..10cad85 100644 --- a/removeDuplicates/removeDuplicates.js +++ b/removeDuplicates/removeDuplicates.js @@ -1,13 +1,26 @@ /* * 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] - * - * beast mode: try not to use two for loops. - * hint: most array methods native to JS iterate in some way. - * So if you're using 'indexOf' 'sort' 'forEach' etc, + * + * beast mode: try not to use two for loops. + * hint: most array methods native to JS iterate in some way. + * So if you're using 'indexOf' 'sort' 'forEach' etc, * you're more than likely using a for loop under the hood. */ const removeDuplicates = (arr) => { - //code here... -}; \ No newline at end of file + const temp = []; + for (let i = 0;i < arr.length; i++) { + if (!(temp.includes(arr[i]))) { + temp.push(arr[i]); + } + } + arr = temp; + return arr; +}; + + +const a = [1, 1, 1, 2, 2, 3, 4, 5, 5]; +const b = [5,5,5,5,5,5,5,5,5,4,4,4,4,4,4,4,4,4,3,3,3,3,3,2,2,2,2,2,1,1,1,1,1]; +console.log(removeDuplicates(a)); +console.log(removeDuplicates(b)); From 0c5d6aff13aa9cd8359055cf33719063ccb3bf77 Mon Sep 17 00:00:00 2001 From: Tyler Date: Tue, 15 Aug 2017 12:20:29 -0400 Subject: [PATCH 04/11] finished waterJugs --- brainTeasers/waterJugs.md | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/brainTeasers/waterJugs.md b/brainTeasers/waterJugs.md index 5276fc0..3a97597 100644 --- a/brainTeasers/waterJugs.md +++ b/brainTeasers/waterJugs.md @@ -1 +1,12 @@ 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 3 quart jug (3:3, 5:0) +transfer water to 5 quart (3:0, 5:3) +fill 3 quart (3:3, 5:3) +transfer 2 quarts from 3 to 5 (3:1, 5:5) +empty 5 quart (3:1, 5:0) +transfer 1 quart to 5 quart (3:0, 5:1) +fill 3 quart and transfer to 5 quart (3:0, 5:4) +the 5 quart jug now has a total of four quarts of water +*/ \ No newline at end of file From 99f7240e78e767b2675575ee23480b4ef49a4ff4 Mon Sep 17 00:00:00 2001 From: Tyler Date: Wed, 16 Aug 2017 12:31:59 -0400 Subject: [PATCH 05/11] finished forLoopTimeout.js --- forLoopTimeout/forLoopTimeout.js | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/forLoopTimeout/forLoopTimeout.js b/forLoopTimeout/forLoopTimeout.js index 87522c2..5e91b94 100644 --- a/forLoopTimeout/forLoopTimeout.js +++ b/forLoopTimeout/forLoopTimeout.js @@ -4,10 +4,21 @@ // The output should be 1, 2, 3, .... 10. Right now it just prints 11. // I've been asked this three times in separate interviews. +//----------------ES5-------------------- 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); + + console.log(i - 10); + i++; + }, 0); +} + +//-----------ES6-------------- +for (let i = 1; i <= 10; i++) { + setTimeout(function() { + + console.log(i); + + }, 0); } \ No newline at end of file From 3251d64974b2bbb2722c777ac916f94e114b1bc7 Mon Sep 17 00:00:00 2001 From: Tyler Date: Thu, 17 Aug 2017 12:31:54 -0400 Subject: [PATCH 06/11] finished largestPrimePalindrome.js --- .../largestPrimePalindrome.js | 31 ++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/largestPrimePalindrome/largestPrimePalindrome.js b/largestPrimePalindrome/largestPrimePalindrome.js index 4cc99c0..172f837 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 + */ + + function isPalindrome(num) { + if ( num.toString() === num.toString().split( '' ).reverse().join( '' ) ) { + return true; + } + return false; +} + +let primesArray = new Array(); + + function isPrime(num) { + for ( let i = 2; i < num; i++ ) { + if ( num % i === 0 ) { + return false; + } + } + return true; + } + + function largestPrimePalindrome() { + for (let i = 2; i < 1000; i++) { + if( isPalindrome(i) && isPrime(i) ) { + primesArray.push(i); + } + } + return primesArray[primesArray.length - 1]; + } + +console.log(largestPrimePalindrome()); \ No newline at end of file From 79ba5fc991113fb360c1e251cbbd94129ab3c386 Mon Sep 17 00:00:00 2001 From: Tyler Date: Fri, 18 Aug 2017 12:55:53 -0400 Subject: [PATCH 07/11] finished constructors.js --- constructors/constructors.js | 122 ++++++++++++++++++++++++++++++++++- 1 file changed, 121 insertions(+), 1 deletion(-) diff --git a/constructors/constructors.js b/constructors/constructors.js index 54801f6..68dc822 100644 --- a/constructors/constructors.js +++ b/constructors/constructors.js @@ -20,4 +20,124 @@ * * 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 (hp, strength, speed, intelligence, isFriendly, build, attackType, fight) { + this.hp = hp; + this.strength = strength; + this.speed = speed; + this.intelligence = intelligence; + this.isFriendly = isFriendly; + this.build = build; + this.attackType = attackType; + this.fight = fight; + } + attack(enemy) { + enemy.hp -= (Math.floor(Math.random() * 30)); + enemy = enemy.toUpperCase(); + console.log(`${enemy}'s hp is now at ${enemy.hp}!`); + } + } + + class Humanoid extends NPC { + constructor (roboOrBio, speaksEnglish, speaksOther, faveWeapon) { + this.roboOrBio = roboOrBio; + this.speaksEnglish = speaksEnglish; + this.speaksOther = speaksOther; + this.faveWeapon = faveWeapon; + } + } + +class Animal extends NPC { + constructor (hasHair, isOmnivore, numLegs, hasTail) { + this.hasHair = hasHair; + this.isOmnivore = isOmnivore; + this.numLegs = numLegs; + this.hasTail = hasTail; + } +} + +class Plant extends NPC { + constructor(isPoisonous, isPretty, isTasty, color) { + this.isPoisonous = isPoisonous; + this.isPretty = isPretty; + this.isTasty = isTasty; + this.color = color; + } +} + +class Human extends Humanoid { + constructor (isMultilingual, isMarried, birthCity) { + this.isHuman = true; + this.isMultilingual = isMultilingual; + this.isMarried = isMarried; + this.birthCity = birthCity; + } +} + +class Elf extends Humanoid { + constructor (earType, height, houseStyle) { + this.earType = earType; + this.height = height; + this.houseStyle = houseStyle; + } +} + +class Orc extends Humanoid { + constructor (naughtyOrNice, fightingStyle) { + this.naughtyOrNice = naughtyOrNice; + this.fightingStyle = fightingStyle; + } +} + +class Bear extends Animal { + constructor (wildOrTamed, cubOrMama) { + this.wildOrTamed = wildOrTamed; + this.cubOrMama = cubOrMama; + } +} + +class Wolf extends Animal { + constructor (howlsAtTheMoon) { + this.hasFangs = true; + this.howlsAtTheMoon = howlsAtTheMoon; + } +} + +class FleshEatingDaisy extends Plant { + constructor (name) { + this.isPoisonous = true; + this.isHorrible = true; + this.sucks = true; + this.worstFear = 'Roundup'; + this.name = name; + if (this.name !== 'Tulip') { + this.name = 'Tulip'; + } + } +} + +class Soldier extends Human { + constructor (faveGun, camoColor) { + this.isAwesome = true; + this.faveGun = faveGun; + this.camoColor = camoColor; + } +} + +class Peasant extends Soldier { + constructor (isHungry, isLoyal) { + this.isHungry = isHungry; + this.isLoyal = isLoyal; + } +} + +class Bandit extends Human { + constructor () { + this.thief = true; + } + steal() { + console.log('The bandit robbed you and got away!'); + } +} From a1f2faebafd76c1ae14b2944bd139ff298cd1bd2 Mon Sep 17 00:00:00 2001 From: Tyler Date: Mon, 21 Aug 2017 12:11:11 -0400 Subject: [PATCH 08/11] finished commonCharacters --- commonCharacters/commonCharacters.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/commonCharacters/commonCharacters.js b/commonCharacters/commonCharacters.js index ec31d82..1df9d4e 100644 --- a/commonCharacters/commonCharacters.js +++ b/commonCharacters/commonCharacters.js @@ -6,3 +6,16 @@ * Example: commonCharacters('acexivou', 'aegihobu') * * Returns: 'aeiou' */ + +let newArr = []; +function commonCharacters(str1, str2){ + for (var i = 0; i < str1.length; i++) { + for (var j = 0; j < str2.length; j++) { + if (str1[i] === str2[j] && !newArr.includes(str1[i])) { + newArr.push(str1[i]); + } + } + } + return newArr.join(''); +} +console.log(commonCharacters('cat', 'cadillac')); \ No newline at end of file From 2afb08576269829574726ac810ab24746cadd0b0 Mon Sep 17 00:00:00 2001 From: Tyler Date: Mon, 21 Aug 2017 12:15:09 -0400 Subject: [PATCH 09/11] removeDuplicates --- removeDuplicates/removeDuplicates.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/removeDuplicates/removeDuplicates.js b/removeDuplicates/removeDuplicates.js index 10cad85..c3f673e 100644 --- a/removeDuplicates/removeDuplicates.js +++ b/removeDuplicates/removeDuplicates.js @@ -15,8 +15,7 @@ const removeDuplicates = (arr) => { temp.push(arr[i]); } } - arr = temp; - return arr; + return temp; }; From 75c9600bc2df02a0f7fdbd342518200a3598c5b5 Mon Sep 17 00:00:00 2001 From: Tyler Date: Wed, 23 Aug 2017 12:04:09 -0400 Subject: [PATCH 10/11] evenOccurences --- evenOccurences/evenOccurences.js | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/evenOccurences/evenOccurences.js b/evenOccurences/evenOccurences.js index 35da569..813647a 100644 --- a/evenOccurences/evenOccurences.js +++ b/evenOccurences/evenOccurences.js @@ -12,4 +12,21 @@ const evenOccurence = (arr) => { // Your code here. + let holder = {}; + for (var i = 0; i < arr.length; i++) { + if (arr[i] in holder) { + holder.arr[i]++; + } else { + holder.arr[i] = 1; + } + } + for (var j = 0; j < holder.length; j++) { + if (holder.j % 2 === 0) { + return j; + } + } + return null; }; + +const otherArr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 6, 2, 8, 3, 6, 4]; +console.log(evenOccurence(otherArr)); \ No newline at end of file From 46ba47f35c7c97c1a2adb68193a8cf2195974fd0 Mon Sep 17 00:00:00 2001 From: Tyler Date: Thu, 24 Aug 2017 12:06:40 -0400 Subject: [PATCH 11/11] finished vowelCount.js --- vowelCount/vowelCount.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/vowelCount/vowelCount.js b/vowelCount/vowelCount.js index 27fad79..ea4f72b 100644 --- a/vowelCount/vowelCount.js +++ b/vowelCount/vowelCount.js @@ -2,3 +2,17 @@ * Write a function that returns the count of the total number of vowels in a string. * Example: 'Hello World!' -> 3 */ + + + function vowelCount(str) { + const vowels = ['a', 'e', 'i', 'o', 'u','A','E','I','O','U']; + let counter = 0; + for (let i = 0; i < str.length; i++) { + if (vowels.includes(str.charAt(i))) { + counter++; + } + } + return counter; + } + +console.log(vowelCount('Hello World!')); \ No newline at end of file