diff --git a/brainTeasers/waterJugs.md b/brainTeasers/waterJugs.md index 5276fc0..1483fdd 100644 --- a/brainTeasers/waterJugs.md +++ b/brainTeasers/waterJugs.md @@ -1 +1,2 @@ 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 the three-quart jug, pour it into the five-quart jug. Then fill the three-quart jug again and fill the five-quart jug until it is full (leaving one-quart left in the three-quart jug). Throw away the five-quart jug, then pour your remaining one-quart into the five-quart jug, then fill the three-quart jug and add it to the one-quart in the three-quart jug. \ No newline at end of file diff --git a/callBackPractice/callBackPractice.js b/callBackPractice/callBackPractice.js index 8f5d9ac..a5bb953 100644 --- a/callBackPractice/callBackPractice.js +++ b/callBackPractice/callBackPractice.js @@ -1,6 +1,5 @@ /* For today's coding challenge your job is to write functions * so that each function call works. - * * Example: * * greeting('Hey guys', (message) => { @@ -12,10 +11,8 @@ * const greeting = (str, cb) => { * cb(str); * }; - * */ - // 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']; @@ -24,6 +21,11 @@ firstItem(foods, (firstItem) => { console.log(`The first item is ${firstItem}.`); }); +const first = (foods, callback) => { + ccallback(foods[0]); +}; + console.log() + // Write a function called getLength that passes the length of the array into the callback getLength(foods, (length) => { diff --git a/commonCharacters/commonCharacters.js b/commonCharacters/commonCharacters.js index ec31d82..1074d27 100644 --- a/commonCharacters/commonCharacters.js +++ b/commonCharacters/commonCharacters.js @@ -1,8 +1,14 @@ /* * Common Characters: - * Write a function that accepts two strings as arguments, and returns only the characters that are common to both strings. * + * 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' */ +function commonCharacters (char1, char2) { + +} +return commonCharacters; +console.log(commonCharacters(arre, ace)); \ No newline at end of file diff --git a/constructors/constructors.js b/constructors/constructors.js index 54801f6..be58212 100644 --- a/constructors/constructors.js +++ b/constructors/constructors.js @@ -20,4 +20,41 @@ * * 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 + * this.value = [Humanoid, Animal, Plant]; + this.Humanoid = [Human, Elf, Orc]; + this.Animal =[Bear, Wolf]; + this.Plant = [FleshEatingDaisy]; + */ + + class NPC { + constructor(value) { + this.value = value; + this.children = []; + } + class Humanoid { + constructor(value) { + this.value = value; + this.children = [Human, Elf, Orc]; + } + } + class Animal { + constructor(value) { + this.value = value; + this.children = [Bear, Wolf]; + } + } + class Plant { + constructor(value) { + this.value = value; + this.children = [FleshEatingDaisy]; + } + } + class Human { + constructor(value) { + this.value = value; + this.children = [Soldier, Peasant, Bandit]; + } + } + + + } // end of class NPC \ No newline at end of file diff --git a/evenOccurences/evenOccurences.js b/evenOccurences/evenOccurences.js index 1943998..7626cfd 100644 --- a/evenOccurences/evenOccurences.js +++ b/evenOccurences/evenOccurences.js @@ -9,7 +9,24 @@ * * const onlyEven = evenOccurance([1, 7, 2, 4, 5, 1, 6, 8, 9, 6, 4, 1]); * * console.log(onlyEven); // 4 * */ +const onlyEven = evenOccurence([1, 7, 2, 4, 5, 1, 6, 8, 9, 6, 4, 1]); +const evenOccurence = (array) => { + + // first: let's count occurences of all the elements in the array + let hash = {}; // object to serve as counter for all the items in the array (the items + // will be the keys, the counts will be the values) + array.forEach(function(e) { // for each item e in the array + if(hash[e]) hash[e]++; // if we already encountered this item, then increments the counter + else hash[e] = 1; // otherwise start a new counter (initialized with 1) + }); + }; -const evenOccurence = (arr) => { - // Your code here. -}; + // second: we select only the numbers that occured an odd number of times + let result = []; // the result array + for(var e in hash) { // for each key e in the hash (the key are the items of the array) + if(hash[e] % 2 === 0) // if the count of that item is an even number + result.push(+e); // then push the item into the result array (since they are keys are strings we have to cast them into numbers using unary +) + } + return result; +} +console.log(onlyEven); diff --git a/forLoopTimeout/forLoopTimeout.js b/forLoopTimeout/forLoopTimeout.js index 87522c2..ee74593 100644 --- a/forLoopTimeout/forLoopTimeout.js +++ b/forLoopTimeout/forLoopTimeout.js @@ -3,11 +3,36 @@ // See if you can solve this with just ES5 JS. // 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 (i = 1; i <= 10; i++) { + setPrint(i); +} + +function setPrint(i) { + setTimeout(function() { + console.log(i); + }); +} +// ES6 +for (let h = 1; h <= 10; h++) { + setTimeout(function() { + console.log(h); + }, 0); +} +// + +for (var i = 1; i <= 10; i++) { + (function(i) { + setTimeout(function() { + console.log(i); + }, 0); + })(i); } \ No newline at end of file diff --git a/isUnique/isUnique.js b/isUnique/isUnique.js index 6c9caf5..0a94c76 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 checkStr = (uniqueString) => { + const str = {}; + for (let i = 0; i < uniqueString.length; i++) { + const check = str[i]; + if (uniqueString[check]) + return false; + else uniqueString[check] = true; + } + return true; +} +//test +console.log(checkStr("abddefg")); //true +console.log(checkStr("aa")); //false \ No newline at end of file diff --git a/largestPrimePalindrome/largestPrimePalindrome.js b/largestPrimePalindrome/largestPrimePalindrome.js index 4cc99c0..72a5f9e 100644 --- a/largestPrimePalindrome/largestPrimePalindrome.js +++ b/largestPrimePalindrome/largestPrimePalindrome.js @@ -3,4 +3,60 @@ * 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 largestPrimePalindrome(num) { + + for (i = 0; i <= num; i++) { + // Look for all primes within 1000 + var prime = function () { + var num; + for (num = 0; num < 1001; num++) + if (num % 2 === 0){ + break; + } + else if (num % 3 === 0){ + break; + } + else if (num % 4=== 0){ + break; + } + else if (num % 5 === 0){ + break; + } + else if (num % 6 === 0){ + break; + } + else if (num % 7 === 0){ + break; + } + else if (num % 8 === 0){ + break; + } + else if (num % 9 === 0){ + break; + } + else if (num % 10 === 0){ + break; + } + else if (num % 11 === 0){ + break; + } + else if (num % 12 === 0){ + break; + } + else { + return num; + } + } + }; + //return prime; + console.log(prime(1000)); + + } + // check those primes to see which one is a palindrome + + // } + +//return largestPrimePalindrome(1000); +//console.log(largestPrimePalindrome); diff --git a/longestString/longestString.js b/longestString/longestString.js index 35b887c..a766d13 100644 --- a/longestString/longestString.js +++ b/longestString/longestString.js @@ -2,3 +2,31 @@ * 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 +// ['house', 'boat', 'cat'] -> 'house' +// ['abc', 'def'] -> 'def' +// [] -> null + +/* +function longestString strings + loop over the array of strings + do something in the loop + return longestString + +const longestString = (strings) => { + +}; +*/ + +const longestString ['house', 'boat', 'cat'] => { + const longestString.forEach(function(item, index, array) { + console.log(item, index); +}); + +} + diff --git a/removeDuplicates/removeDuplicates.js b/removeDuplicates/removeDuplicates.js index 970f719..7379e4b 100644 --- a/removeDuplicates/removeDuplicates.js +++ b/removeDuplicates/removeDuplicates.js @@ -7,7 +7,32 @@ * So if you're using 'indexOf' 'sort' 'forEach' etc, * you're more than likely using a for loop under the hood. */ - +const arr = [1, 1, 1, 2, 2, 3, 4, 5, 5]; const removeDuplicates = (arr) => { - //code here... -}; \ No newline at end of file + //array + const nonDuplicates = new Set(); + nonDuplicates.forEach((nonDup, i) => { + if (nonDup === nonDup) { + nonDuplicates[i] = nonDup(); + } else { + nonDuplicates[i] = nonDup(); + } + }); + return nonDuplicates; +}; + +console.log(removeDuplicates(arr)); + +/* +const removeDuplicates = (arr, cb) => { + const set = new Set(); + arr.forEach(item => { + set.add(item); + }); + cb(Array.from(set)); +}; + +removeDuplicates(foods, (uniqueFoods) => { + console.log(`foods with duplicates removed: ${uniqueFoods}`); +}); +*/ diff --git a/reverseCase/reverseCase.js b/reverseCase/reverseCase.js index f1051f5..e545ef1 100644 --- a/reverseCase/reverseCase.js +++ b/reverseCase/reverseCase.js @@ -2,4 +2,18 @@ * 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 swapCase = (word) => { + let swap = " "; + for (let i=0; i < word.length; i++) { + const str = word.charAt(i); + swap += (str == str.toUpperCase() ? str.toLowerCase() : str.toUpperCase()); + // console.log(str); + } + } + return swap; + }; + const swapper = swapCase('Hello'); + console.log(swapper); + */ + diff --git a/stringCompression/stringCompression.js b/stringCompression/stringCompression.js index 48db571..f47deac 100644 --- a/stringCompression/stringCompression.js +++ b/stringCompression/stringCompression.js @@ -4,3 +4,33 @@ // 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 = (str) => { + let arr = []; + // let count =[]; + + str.split('').forEach((letter) => { + if (str.split('').includes(letter) && !arr.includes(letter)) { + arr.push(letter); + } + + // Now, if I have the letters, use RegExp or match to get the count + + + }); + + return arr.join('').replace(str); +} + +console.log(stringCompression('aaabbccc')); + +const stringSearch = (str) => { + for (let count =- 1, index =- 2; index != 1; + count++, index = str.indexOf(stringSearch, index + 1) + ); +} + +return str; +console.log(stringSearch('aaabbb')); + + \ No newline at end of file diff --git a/vowelCount/vowelCount.js b/vowelCount/vowelCount.js index 27fad79..2f6c5ae 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 = str => + Array.from(str) + .filter(vowel => 'aeiou'.includes(vowel)).length; + + +console.log(vowelCount('hello')); // 2