diff --git a/brainTeasers/waterJugs.md b/brainTeasers/waterJugs.md index 5276fc0..33d1e29 100644 --- a/brainTeasers/waterJugs.md +++ b/brainTeasers/waterJugs.md @@ -1 +1,20 @@ 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 qt +3 qt + +=> 4 qt + +find out where 3 qt mark is on 5 qt jug, which will +let us know where 2 qts is + +fill 3 qt jug +xfer 3 qt water to 5 qt jug +mark 3 qt point on 5 qt jug +fill 5 qt jug +xfer water up to 3 qt mark to 3qt jug (2 qt) +mark 2 qt point on 3 qt jug +empty 5 qt jug +xfer 2 qt of water from 3 qt jug to 5 qt jug +pour 2 qt of water to 3 qt jug +xfer 2 qt of water from 3 qt jug to 5 qt jug \ No newline at end of file diff --git a/callBackPractice/callBackPractice.js b/callBackPractice/callBackPractice.js index 8f5d9ac..47d9711 100644 --- a/callBackPractice/callBackPractice.js +++ b/callBackPractice/callBackPractice.js @@ -18,54 +18,100 @@ // Write a function called firstItem that passes the first item of the given array to the callback function +const firstItem = (arr, cb) => { + cb(arr.shift()); +}; + + const foods = ['pineapple', 'mango', 'ribeye', 'curry', 'tacos', 'ribeye', 'mango']; firstItem(foods, (firstItem) => { console.log(`The first item is ${firstItem}.`); + console.log(firstItem === 'pineapple'); }); // Write a function called getLength that passes the length of the array into the callback +const getLength = (arr, cb) => { + cb(arr.length); +}; + getLength(foods, (length) => { console.log(`The length of the array is ${length}.`); + console.log(length === 6); }); // Write a function called last which passes the last item of the array into the callback +const last = (arr, cb) => { + cb(arr.pop()); +} + last(foods, (lastItem) => { console.log(`The last item in the array is ${lastItem}.`); + console.log(lastItem === 'mango'); }); // 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}.`); + console.log(15 === 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}.`); + console.log(product === 50); }); // 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 = (arr, item, cb) => { + cb(arr.indexOf(item) !== -1); +}; + contains(foods, 'ribeye', (result) => { console.log(result ? 'ribeye is in the array' : 'ribeye is not in the array'); + console.log(result === true); }); // 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. +var removeDuplicates = (arr, cb) => { + const out = []; + arr.forEach((el) => { + if(!out.includes(el)){ + out.push(el); + } + }); + + cb(out); +} + removeDuplicates(foods, (uniqueFoods) => { console.log(`foods with duplicates removed: ${uniqueFoods}`); + console.log(uniqueFoods.toString() === "mango,ribeye,curry,tacos"); }); // Write a function called forEach that iterates over the provided array and passes the value and index into the callback. +const forEach = (arr, cb) => { + arr.forEach(cb); +}; + forEach(foods, (value, index) => { console.log(`${value} is at index ${index}.`); -}); \ No newline at end of file +}); diff --git a/constructors/constructors.js b/constructors/constructors.js index 54801f6..7f0b416 100644 --- a/constructors/constructors.js +++ b/constructors/constructors.js @@ -20,4 +20,66 @@ * * 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){ + this.hp = hp; + this.strength = strength; + this.speed = speed; + } +} + +class Humanoid extends NPC { + constructor(hp, strength, speed){ + super(hp, strength, speed); + } +} + +class Animal extends NPC { + constructor(hp, strength, speed){ + super(hp, strength, speed); + } +} + +class Plant extends NPC { + constructor(hp, strength, speed) { + super(hp, strength, speed); + } +} + +class Humanoid extends Human { + constructor(hp, strength, speed) { + super(hp, strength, speed); + } +} + +class Elf extends Humanoid { + constructor(hp, strength, speed) { + super(hp, strength, speed); + } +} + +class Orc extends Humanoid { + constructor(hp, strength, speed) { + super(hp, strength, speed); + } +} + +class Bear extends Animal { + constructor(hp, strength, speed) { + super(hp, strength, speed); + } +} + +class Wolf extends Animal { + constructor(hp, strength, speed) { + super(hp, strength, speed); + } +} + +class FleshEatingDaisy extends Plant { + constructor(hp, strength, speed) { + super(hp, strength, speed); + } +} diff --git a/forLoopTimeout/forLoopTimeout.js b/forLoopTimeout/forLoopTimeout.js index 87522c2..8b112b3 100644 --- a/forLoopTimeout/forLoopTimeout.js +++ b/forLoopTimeout/forLoopTimeout.js @@ -10,4 +10,12 @@ 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 +<<<<<<< HEAD +} + +/* put the for loop in the callback function and wrapping console.log(i) in it. It doesn't work +in its current state as expected because nothing is blocking the incrementing of i from happening and by the time console.log(i) executes, we have already reached the end of the loop +*/ +======= +} +>>>>>>> d1daab308de663dad2c7129e60c72bbb70519b4c diff --git a/isUnique/isUnique.js b/isUnique/isUnique.js index 6c9caf5..9dd2208 100644 --- a/isUnique/isUnique.js +++ b/isUnique/isUnique.js @@ -1,3 +1,27 @@ /* 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 + */ +/* + * i: string + * o: boolean + * + * make new str that contains set of chars + * + * for each char in the str + * if curr str is found in new str + * return false + * add curr char to new str * + * return true + */ + +const isUnique = (str) => { + let included = ''; + for (let i = 0; i < str.length; i++) { + if (included.indexOf(str[i]) !== -1) return false; + included += str[i]; + } + return true; +}; + +console.log(isUnique('aa')); +console.log(isUnique('ab')); diff --git a/largestPrimePalindrome/largestPrimePalindrome.js b/largestPrimePalindrome/largestPrimePalindrome.js index 4cc99c0..09a251b 100644 --- a/largestPrimePalindrome/largestPrimePalindrome.js +++ b/largestPrimePalindrome/largestPrimePalindrome.js @@ -3,4 +3,39 @@ * 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 + */ + +/* + determine if number is prime + determine whether or not it's a palindrome + find largest number of all numbers less than 1000 -> so we start counting down from 999 + + it's probably more efficient the other way around (palindrome, then prime) +*/ + +const findLargestPrimePalindromeLessThan1000 = () => { + let i = 999; + while (i > 0) { + if (String(i) === String(i).split('').reverse().join('')) { + if (isPrime(i)) { + return i; + } + } + i--; + } + return 'no prime numbers that are palindromes'; +}; + +const isPrime = (n) => { + if (n < 2) return false; + if (n === 2) return true; + for (let i = Math.floor(n/2); i > 2; i--) { + if (n % i === 0) return false; + } + return true; +}; + +kisPrime(2); +isPrime(11); +isPrime(22); +isPrime(1); diff --git a/longestString/longestString.js b/longestString/longestString.js index 35b887c..2290622 100644 --- a/longestString/longestString.js +++ b/longestString/longestString.js @@ -1,4 +1,42 @@ /* * Write a function that accepts an array of strings. * Return the longest string in the array. + * + * must track both the longest length as well as the longest string + * will mapping work? + * + * in: arr of strings out: longest string + * to find the longest string in a list of strings + * take a list of strings, choose first string as being longest(assuming + * that a valid list of strings has been inputted) and then continue to loop through list until + * exhausted + *return longest string + * + * + */ + + + + +const longest = (arr) => { + if(arr.length === 0) return null; + let longestLength = arr[0].length; + let longestString = arr[0]; + arr.forEach((str) => { // whatever...will just use forEach instead of starting at i = 1 using for loop + if(str.length > longestLength){ + longestLength = str.length; + longestString = str; + } + }); + return longestString; +}; + +/* expect longest(['a','aaaaa','aa']) to return 'aaaaa' + * expect longest(['aaaaa','a','aa']) to return 'aaaaa' + * expect longest(['a','aa','aaaaa']) to return 'aaaaa' + * expect longest(['aaaaa','a','aaaaa']) to return 'aaaaa' + * + * */ + +longest(['a','aaaaa','aa']) === 'aaaaa'; diff --git a/removeDuplicates/removeDuplicates.js b/removeDuplicates/removeDuplicates.js index 970f719..ce4adc2 100644 --- a/removeDuplicates/removeDuplicates.js +++ b/removeDuplicates/removeDuplicates.js @@ -9,5 +9,7 @@ */ const removeDuplicates = (arr) => { + return Array.from(new Set(arr)); //code here... -}; \ No newline at end of file +}; + diff --git a/reverseCase/reverseCase.js b/reverseCase/reverseCase.js index f1051f5..7f69fcb 100644 --- a/reverseCase/reverseCase.js +++ b/reverseCase/reverseCase.js @@ -2,4 +2,23 @@ * 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 + */ + +/* + * in: string + * out: string with reversed case of input string + * + * create empty string + * for each char + * if current char has lower case + * then change to upper + * else change to lower + * + */ + +const reverseCase = (str) => { + return str.split('').map((ch) => { + if (ch.toUpperCase() === ch) return ch.toLowerCase(); + return ch.toUpperCase(); + }).join(''); +};