diff --git a/brainTeasers/waterJugs.md b/brainTeasers/waterJugs.md index 5276fc0..0a62ae4 100644 --- a/brainTeasers/waterJugs.md +++ b/brainTeasers/waterJugs.md @@ -1 +1,16 @@ 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 five quart jug +1 three quart jug + +needs 4 quarts of water + +can't fill the water up exactly half + +Fill up the five-quart jug with 3 quarts of water and fill the three-quart jug with 1 quarts of water. +You will get exactly 4 quarts of water. + +Step 1: Fill the 5 quarts pour all of the water inside the 3 quarts jug +Step 2: + +does not say we can't go over half of the jug. Just says having exactly half is impossible. diff --git a/callBackPractice/callBackPractice.js b/callBackPractice/callBackPractice.js index 8f5d9ac..e11601c 100644 --- a/callBackPractice/callBackPractice.js +++ b/callBackPractice/callBackPractice.js @@ -17,55 +17,73 @@ // 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, (firstItem) => { + firstItem = foods[0]; console.log(`The first item is ${firstItem}.`); }); +firstItem(); // Write a function called getLength that passes the length of the array into the callback - -getLength(foods, (length) => { +const getLength = (foods, (length) => { + length = foods.length; console.log(`The length of the array is ${length}.`); }); +getLength(); // Write a function called last which passes the last item of the array into the callback - -last(foods, (lastItem) => { +const last = (foods, (lastItem) => { + lastItem = foods[6]; console.log(`The last item in the array is ${lastItem}.`); }); +last(); // Write a function called sumNums that adds two numbers and passes the result to the callback - - -sumNums(5, 10, (sum) => { +const sumNums = (5, 10, (sum) => { + sum = 5 + 10; console.log(`The sum is ${sum}.`); }); +sumNums(); // Write a function called multiplyNums that adds two numbers and passes the result to the callback - -multiplyNums(5, 10, (product) => { +const multiplyNums = (5, 10, (product) => { + product = 5 * 10; console.log(`The product is ${product}.`); }); +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 - -contains(foods, 'ribeye', (result) => { +const contains = (foods, 'ribeye', (result) => { + for (let i = 0; i < foods.length; i++) { + if (foods[i] === 'ribeye'){ + result = foods[i]; + }//if + }//for console.log(result ? 'ribeye is in the array' : 'ribeye is not in the array'); }); +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. +const removeDuplicates = (foods, (uniqueFoods) => { + let newFoods = []; + for (let i = 0; i < foods.length; i++) { + newFoods[i] = foods[i]; + } + uniqueFoods = newFoods.slice(1,2); + console.log(uniqueFoods) -removeDuplicates(foods, (uniqueFoods) => { console.log(`foods with duplicates removed: ${uniqueFoods}`); }); +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) => { +const forEach = (foods, (value, index) => { + for (let i = 0; i < foods.length; i++) { + value(foods[i], index); + } console.log(`${value} is at index ${index}.`); -}); \ No newline at end of file +}); +forEach(); diff --git a/commonCharacters/commonCharacters.js b/commonCharacters/commonCharacters.js index ec31d82..0f726b2 100644 --- a/commonCharacters/commonCharacters.js +++ b/commonCharacters/commonCharacters.js @@ -6,3 +6,43 @@ * Example: commonCharacters('acexivou', 'aegihobu') * * Returns: 'aeiou' */ +/* +const commonChar = (str1, str2) => { + let string = []; + if (str1.indexOf('a') > -1 && str2.indexOf('a') > -1) { + string.push('a'); + } + if (str1.indexOf('e') > -1 && str2.indexOf('e') > -1) { + string.push('e'); + } + if (str1.indexOf('i') > -1 && str2.indexOf('i') > -1) { + string.push('i'); + } + if (str1.indexOf('o') > -1 && str2.indexOf('o') > -1) { + string.push('o'); + } + if (str1.indexOf('u') > -1 && str2.indexOf('u') > -1) { + string.push('u'); + } + return string.join(''); +} + +const str1 = 'areyiwoqu'; +const str2 = 'asedifogu'; + +console.log(commonChar(str1, str2)); +*/ + + +const commonChars = (str1, str2) => { + let common = []; + + + str1.split('').forEach((letter) => { + if (str2.split('').includes(letter) && !common.includes(letter)) { + common.push(letter); + } + }); + return common.join('').replace(' ', ''); +} +console.log(commonChars('axei ou', 'aeidou')); \ No newline at end of file diff --git a/constructors/constructors.js b/constructors/constructors.js index 54801f6..80149b0 100644 --- a/constructors/constructors.js +++ b/constructors/constructors.js @@ -20,4 +20,76 @@ * * 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() { + this.humanoid = []; + this.animal = []; + this.plant = []; + } + humanoidClass(selectClass) { + const humanoidClass = [`human`, 'Elf', 'Orc']; + let selectedHumanClass = humanoidClass[selectClass]; + if (selectedHumanClass === `human`) { + class Stats { + constructor() { + this.hp = 125; + this.stregnth = 2; + this.agility = 3; + this.mana = 5; + } + } + const stats = new Stats(); + this.humanoid.push(selectedHumanClass, new Stats); + } + if (selectedHumanClass === 'Elf') { + class Stats { + constructor() { + this.hp = 125; + this.stregnth = 2; + this.agility = 3; + this.mana = 5; + } + } + this.humanoid.push(selectedHumanClass, new Stats); + } + if (selectedHumanClass === 'Orc') { + class Stats { + constructor() { + this.hp = 200; + this.strength = 5; + this.agility = 4; + this.mana = 0; + } + } + this.humanoid.push(selectedHumanClass, new Stats); + } + } + animalClass(selectClass) { + const animalClass = ['Bear', 'Wolf']; + let selectedAnimalClass = animalCLass[selectClass]; + if (selectedAnimalClass === 'Bear') { + class Stats { + constructor() { + this.hp = 1000; + this.strength = 6; + this.agility = 4; + this.mana = 0; + } + } + this.animal.push(selectedAnimalClass, new Stats); + } + } + plantClass(selectClass) { + const plantClass = ['Flesh Eating Daisy']; + this.animal.push(plantClass[selectClass]); + } +} + +const npc = new NPC(); +npc.humanoidClass(0); +//npc.humanoidClass(1); +//npc.humanoidClass(2); +console.log(npc.humanoid); + diff --git a/evenOccurences/evenOccurences.js b/evenOccurences/evenOccurences.js index 35da569..048bca1 100644 --- a/evenOccurences/evenOccurences.js +++ b/evenOccurences/evenOccurences.js @@ -10,6 +10,22 @@ * * console.log(onlyEven); // 4 * */ + const evenOccurence = (arr) => { - // Your code here. + let arrStorage = {}; + + arr.forEach = (value) => { + arrStorage[value] = storage[value] + 1 || 1; + }; + + for (let i = 0; i < arr.length; i++) { + let even = arr[i]; + + if (arrStorage[even] % 2 === 0) { + return even; + } + } + return null; }; + +const randomArray = evenOccurence([1, 7, 2, 4, 5, 1, 6, 8, 9, 6, 4, 1]); diff --git a/forLoopTimeout/forLoopTimeout.js b/forLoopTimeout/forLoopTimeout.js index 87522c2..195e79d 100644 --- a/forLoopTimeout/forLoopTimeout.js +++ b/forLoopTimeout/forLoopTimeout.js @@ -6,8 +6,6 @@ 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++); }, 0); } \ No newline at end of file diff --git a/isUnique/isUnique.js b/isUnique/isUnique.js index 6c9caf5..854b68c 100644 --- a/isUnique/isUnique.js +++ b/isUnique/isUnique.js @@ -1,3 +1,75 @@ /* 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 + */ + +/* + let isUnique = string => { + unique: 'Hello' + unique1: 'Hi' + unique2: 'This' + }; + + if (isUnique < 100) { + console.log('this does not have unique char'); + } else + console.log('This has unique char'); + console.log(isUnique); + +switch (isUnique()) { + case (isUnique.unique): { + console.log('char hello'); + } + case(isUnique.unique1): { + console.log('char hi'); + } + case(isUnique.unique2): { + console.log('char this'); + break; + } + console.log('No other char'); +} +console.log('Program ending'); + +*/ + +/* +function isUnique structures +iterate over str +iterate over str starting at i + 1 +if we find a match return false +return true; +*/ + +/* +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; +}; +const str1 = 'abcdefg'; +const str2 = 'abcdefgg'; +const result = isUnique(str1); +console.log(result); +*/ + + + +const isUnique = (str) => { + const set = new Set(); + for (let i = 0; i < str.length; i++) { + const character = str[i]; + const isInSet = set.has(character); + if(isInSet) return false; + if (!isInSet) set.add(character); + } + return true; +}; +const str1 = '1234567890qwertyuiopasdfghjklzxcvbnm'; +const str2 = 'abcdefgg'; + +const result = isUnique(str1); + +console.log(result); diff --git a/largestPrimePalindrome/largestPrimePalindrome.js b/largestPrimePalindrome/largestPrimePalindrome.js index 4cc99c0..ebfed60 100644 --- a/largestPrimePalindrome/largestPrimePalindrome.js +++ b/largestPrimePalindrome/largestPrimePalindrome.js @@ -3,4 +3,24 @@ * 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 largestPrimePalindrome = () => { + + const isPalindrome = (n) => { + if (n === n.toString().split(''.reverse().join(''))) return true; + } + const isPrime = (n) => { + if (n % 2 === 0) { + for (let j = 3; j <= Math.sqrt(n); j++) { + if (n % k === 0) return false; + } + return true; + } + } + + for (let i = 1000; i > 0; i--) { + if (isPalindrome(i) && isPrime(i)) return i; + } + } diff --git a/longestString/longestString.js b/longestString/longestString.js index 35b887c..b91478d 100644 --- a/longestString/longestString.js +++ b/longestString/longestString.js @@ -2,3 +2,34 @@ * Write a function that accepts an array of strings. * Return the longest string in the array. */ +/* +console.log('Picking an OS with the highest number of characters'); +const stringLong = (strings) => { + let os = ['MacOSX', 'Linux', 'Windows10']; + console.log(os); + for(let i = 0; i < os.length; i++){ + let number = os[i]; + console.log(number.length); + if(number.length > 8){ + + console.log('Windows 10 is best OS'); + return number; + break; + }//if + }//for +}; + + +stringLong(); +*/ + + + +const reverseCase = (string) => +string + .split('') + .map(c => c.toUpperCase() === c ? c.toLowerCase() : c.toUpperCase()) + .join('') + + +console.log(reverseCase('AHSDHWADHShsdahdhwhadhwahdHHDAWHDASHDA')); diff --git a/removeDuplicates/removeDuplicates.js b/removeDuplicates/removeDuplicates.js index 970f719..08dc697 100644 --- a/removeDuplicates/removeDuplicates.js +++ b/removeDuplicates/removeDuplicates.js @@ -1,13 +1,23 @@ /* * 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 duptArray = [1, 1, 1, 2, 2, 3, 4, 5, 5]; + +const removeDuplicates = (arr, callback) => { + const set = new Set(); + arr.forEach(item => { + set.add(item); + }); + callback(Array.from(set)); +}; + +removeDuplicates(duptArray, (result) => { + console.log(result); +}); diff --git a/reverseCase/reverseCase.js b/reverseCase/reverseCase.js index f1051f5..a804b3e 100644 --- a/reverseCase/reverseCase.js +++ b/reverseCase/reverseCase.js @@ -2,4 +2,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. - */ \ No newline at end of file + */ + +const caseChange = (str) =>{ + let newCase = ''; + for (let i = 0; i < str.length; i++){ + if(str[i] === str[i].toLowerCase()){ + newCase += str[i].toUpperCase(); + }else { + newCase += str[i].toLowerCase(); + } + } + console.log(newCase); + return newCase; +} + +let string = 'Hello World'; + +let reversedString = caseChange(string);