diff --git a/brainTeasers/waterJugs.md b/brainTeasers/waterJugs.md index 5276fc0..85f01d8 100644 --- a/brainTeasers/waterJugs.md +++ b/brainTeasers/waterJugs.md @@ -1 +1,27 @@ 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 //3 Quart + +need: 4 quarts. + +**///////////////\\\\\\\\\\\\\\** + +I would fill up the 5 quart jug fully. Pour our 3 quarts into the 3 quart jug- take the remaining 2 quarts and put into the 4 quart jug. + +I would then rinse and repeat. I would fully empty the 3 quart jug. + +Fill the 5 quart jug again- pour 3 quarts into 3qt jug and fill the rest of the four quart jug with the remaining two quarts. + +***/////////////////\\\\\\\\\\\\\\\\\\\*** +Step 1: +Fill 5 to top, + +Step 2: +Empty 5quart to 3quart, +there will be 2quart remaining, + +Step 3: +take remaining 2quart to fill 4quart halfway. + +Step 4: +Repeat steps 1 - 3 to completion of task. \ No newline at end of file diff --git a/callBackPractice/callBackPractice.js b/callBackPractice/callBackPractice.js index 8f5d9ac..50b567e 100644 --- a/callBackPractice/callBackPractice.js +++ b/callBackPractice/callBackPractice.js @@ -20,31 +20,50 @@ const foods = ['pineapple', 'mango', 'ribeye', 'curry', 'tacos', 'ribeye', 'mango']; +const firstItem = (foods, cb) => { + cb(foods[0]); +}; + 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 = (foods, cb) => { + cb(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) => { console.log(`The last item in the array is ${lastItem}.`); }); // Write a function called sumNums that adds two numbers and passes the result to the callback - +const sumNums = (num1, num2, cb) => { + cb(num1 + num2); +}; sumNums(5, 10, (sum) => { console.log(`The sum is ${sum}.`); }); // Write a function called multiplyNums that adds two numbers and passes the result to the callback +const multiplyNums =(num1, num2, cb) => { + cb(num1 * num2); +}; + + multiplyNums(5, 10, (product) => { console.log(`The product is ${product}.`); }); @@ -52,6 +71,15 @@ multiplyNums(5, 10, (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, item, cb) => { + for(let i = 0; i < foods.length; i++){ + if(foods[i] === item){ + cb(true); + } + } +return false; +} + contains(foods, 'ribeye', (result) => { console.log(result ? 'ribeye is in the array' : 'ribeye is not in the array'); }); @@ -59,11 +87,26 @@ contains(foods, 'ribeye', (result) => { // 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) => { + for (let i = 0; i < foods.length; i++){ + if(foods[i]===foods[i]) + + } +} + + 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) => { diff --git a/commonCharacters/commonCharacters.js b/commonCharacters/commonCharacters.js index ec31d82..acc530f 100644 --- a/commonCharacters/commonCharacters.js +++ b/commonCharacters/commonCharacters.js @@ -6,3 +6,19 @@ * Example: commonCharacters('acexivou', 'aegihobu') * * Returns: 'aeiou' */ +<<<<<<< HEAD +const commonChars = (string1, string2) => { + let combinedStr = ''; + for(let i = 0; i < string1.length; i++){ + for(let j = 0; j < string2.length; j++){ + if(string1[i] === string2[j]){ + combinedStr += string1[i]; + } + } + } + return combinedStr; + } + + console.log(commonChars('acexivou', 'aegihobu'));`` +======= +>>>>>>> e4337df47805c308e007181cf6cdc974564b766c diff --git a/constructors/constructors.js b/constructors/constructors.js index 54801f6..90f44a1 100644 --- a/constructors/constructors.js +++ b/constructors/constructors.js @@ -20,4 +20,114 @@ * * 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.hp = options.hp; + this.strength = options.strength; + this.agility = options.agility; + this.intellect = options.intellect; + } +} +class Humanoid extends NPC { + constructor(options) { + super(options); + + } +} +class Animal extends NPC { + constructor(options) { + super(options); + } +} +class Plant extends NPC { + constructor(options) { + super(options); + } +} +class Human extends Humanoid { + constructor(options) { + super(options); + this.name = options.name; + this.hairColor = options.hairColor; + this.skinColor = options.skinColor; + this.facialHair = options.facialHair; + this.height = options.height; + } +} +class Elf extends Humanoid { + constructor(options) { + super(options); + this.name = options.name; + this.hairColor = options.hairColor; + this.skinColor = options.skinColor; + this.height = options.height; + } +} +class Orc extends Humanoid { + constructor(options) { + super(options); + this.name = options.name; + this.hairColor = options.hairColor; + this.skinColor = options.skinColor; + this.scars = options.scars; + this.facialHair = options.facialHair; + this.height = options.height; + } +} +class Bear extends Animal { + constructor(options) { + super(options); + this.typeOfBear = options.typeOfBear; + this.size = options.size; + this.ferocity = options.ferocity; + } + +} +class Wolf extends Animal { + constructor(options) { + super(options); + this.typeOfWolf = options.typeOfWolf; + this.size = options.size; + this.ferocity = options.ferocity; + + } +} +class FleshEatingdaisy extends Plant { + constructor(options) { + super(options); + this.size = options.size; + this.color = options.color; + } +} +class Solider extends Human { + constructor(options) { + super(options); + this.tacticalMind = options.tacticalMind; + this.weaponMastery = options.weaponMastery; + this.willToLive = options.willToLive; + } +} +class Peasant extends Human { + constructor(options) { + super(options); + this.workEthic = options.workEthic; + this.barteringSkill = options.barteringSkill; + this.woodWorking = options.woodWorking; + this.fishing = options.fishing; + this.hunting = options.hunting; + this.blacksmithing = options.blacksmithing; + } +} +class Bandit extends Human { + constructor(options) { + super(options); + this.guile = options.guile; + this.archery = options.archery; + this.speed = options.speed; + this.pickPocket = options.pickPocket; + this.traps = options.traps; + this.morality = options.morality; + } +} \ No newline at end of file diff --git a/evenOccurences/evenOccurences.js b/evenOccurences/evenOccurences.js index 1943998..bde325d 100644 --- a/evenOccurences/evenOccurences.js +++ b/evenOccurences/evenOccurences.js @@ -11,5 +11,20 @@ * */ const evenOccurence = (arr) => { - // Your code here. + let count = 0; + let element = 0; + for(let i = 0; i < arr.length; i++){ + let tempInt = arr[i]; + let tempCount = 0; + for(let j = 0; j < arr.length; j++){ + if(arr[j] === tempInt){ + tempCount++; + } + } + if(tempCount % 2 === 0 && tempCount > count){ + count = tempCount; + element = tempInt; + } + } + return element; }; diff --git a/forLoopTimeout/forLoopTimeout.js b/forLoopTimeout/forLoopTimeout.js index 87522c2..2ed67d3 100644 --- a/forLoopTimeout/forLoopTimeout.js +++ b/forLoopTimeout/forLoopTimeout.js @@ -10,4 +10,33 @@ for (var i = 1; i <= 10; i++) { // It doesn't. Why? How can you make it print 1 - 10. console.log(i); }, 0); -} \ No newline at end of file +} + +////////////////////\\\\\\\\\\\\\\\\\\ +for (var i = 1; i <= 10; i++) { + setDelay(i); +} + +function setDelay(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. // **Written answer below*** + console.log(i); + },0); +} + +//The code fix was the result of some googling. +// Looking this over again and again didn't make much sense as to why this was not working as intended +//After reading the explanation- it all became apparent. +//The explanation is as follows: +/* We have to arrange a distinct copy of 'i' to be present for each of the timeout functions. +If we don't do something like this, then each of the timer handler functions will share the same variable i. +*/ + +/* My take away from the above is this: We need to make a call back function in order to separate the 'i' values +so the 'i' values don't overwrite each other and make a grand sum to print 10 times. When making a call back function, +we allow the 'i' value to be separated by the loop which in turn allows the loop to properly iterate through the numbers. */ + +//To be completely honest alot of this stuff is really foggy at the moment and I have a fingertip grasp on much of these concepts +//so my explanation can be completely off- trying my hardest, feel like a dumby, but pushing through in the hopes that this all makes +//sense soon. \ No newline at end of file diff --git a/isUnique/isUnique.js b/isUnique/isUnique.js index 6c9caf5..eb228f7 100644 --- a/isUnique/isUnique.js +++ b/isUnique/isUnique.js @@ -1,3 +1,25 @@ /* 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 isUnique = (string) => { + let uniqueStr = ' '; + let nonUnique = ' '; + for (let i = 0; i < string.length; i++){ + if(uniqueStr[i] !== string[i]){ + uniqueStr += string[i]; + } else if (string[i]== string[i]){ + nonUnique += string[i]; + } + } + console.log(uniqueStr); +} + +isUnique('Goodbyee') + +// Issue is that it catches a nonUniqe and stores it only once +// Ex: Goodbyee turns into Godbyee +//Try to figure out why that is + +//Incrementing (++) won't work. Re-ordering of test statements doesn't work. \ No newline at end of file diff --git a/largestPrimePalindrome/largestPrimePalindrome.js b/largestPrimePalindrome/largestPrimePalindrome.js index 4cc99c0..1787b15 100644 --- a/largestPrimePalindrome/largestPrimePalindrome.js +++ b/largestPrimePalindrome/largestPrimePalindrome.js @@ -3,4 +3,21 @@ * 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 checkForPrimePalindrome = (num) => { + num = num + ''; + let reverseNum = num.split("").reverse().join(""); + if(reverseNum === num){ + if(reverseNum % num === 0){ + return true; + } else if (reverseNum === 0 || reverseNum === 1 ) { + return false; + } + } + return false; +} + + +console.log(checkForPrimePalindrome(929)); \ No newline at end of file diff --git a/longestString/longestString.js b/longestString/longestString.js index 35b887c..bc0dc21 100644 --- a/longestString/longestString.js +++ b/longestString/longestString.js @@ -2,3 +2,63 @@ * Write a function that accepts an array of strings. * Return the longest string in the array. */ +/* + * 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 +*/ +// let longest = 0; +// let strings = ['Hello there', 'go HOME NOW PLEASE', 'NOPE']; + +// const longestString = (strings) => { +// for(let i = 0; i < strings.length; i++){ +// if(strings[i].length > longest){ +// longest = strings[i]; +// } +// } +// return longest; +// }; + +// console.log(longestString(strings)); + +// let longest = 0; +// let strings = ['Hello there please go to the basement', 'go HOME NOW PLEASE', 'NOPE']; + +// const longestString = (strings) => { +// for(let i = 0; i < strings.length; i++){ +// if(strings[i].length > longest){ +// if(strings[i] > strings){ +// longest = strings[i]; +// } +// } +// } +// return longest; +// }; + +// console.log(longestString(strings)); + + +let strings = ['This is a story all about how ', 'my life got twist turned upside down', 'NOPE']; + +const longestString = (strings) => { + let longest = strings[0]; + for(let i = 0; i < strings.length; i++){ + if(strings[i].length > longest.length){ + longest = strings[i]; + } + } + return longest; +}; + +console.log(longestString(strings)); \ No newline at end of file diff --git a/removeDuplicates/removeDuplicates.js b/removeDuplicates/removeDuplicates.js index 970f719..b9a7b52 100644 --- a/removeDuplicates/removeDuplicates.js +++ b/removeDuplicates/removeDuplicates.js @@ -8,6 +8,12 @@ * you're more than likely using a for loop under the hood. */ +<<<<<<< HEAD +const removeDuplicates = arr.filter( (el, i, arr) => array.indexOf(el) === i); + +console.log(removeDuplicates); +======= const removeDuplicates = (arr) => { //code here... -}; \ No newline at end of file +}; +>>>>>>> f75af20f74da22c8512a4b36b7b3655b91930623 diff --git a/reverseCase/reverseCase.js b/reverseCase/reverseCase.js index f1051f5..14d89cf 100644 --- a/reverseCase/reverseCase.js +++ b/reverseCase/reverseCase.js @@ -2,4 +2,19 @@ * 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 stringChange = (string)=>{ + let newString =''; + for (let i = 0; i < string.length; i++){ + if(string[i] === string[i].toUpperCase()){ + newString += string[i].toLowerCase(); + } else { + newString += string[i].toUpperCase(); + } + + } + console.log(newString); +}; + +(stringChange('rAYMOND Rosario')); \ No newline at end of file diff --git a/vowelCount/vowelCount.js b/vowelCount/vowelCount.js index 27fad79..c12135f 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 */ +const vowelCount = (string) => { + let vowels = ['a', 'e', 'i', 'o', 'u']; + let count = 0; + for (let i = 0; i <= string.length; i++){ + for(let j = 0; j <= vowels.length; j++){ + if(string[i] === vowels[j]){ + count++; + } + } + } + return count; +} + +console.log(vowelCount('Hello World!')); \ No newline at end of file