diff --git a/brainTeasers/waterJugs.md b/brainTeasers/waterJugs.md index 5276fc0..9d02afc 100644 --- a/brainTeasers/waterJugs.md +++ b/brainTeasers/waterJugs.md @@ -1 +1,8 @@ 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 up the 5 quart jug with water and then fill the 3 quart jug just to the top. The 5 quart jug will be left with 2 quarts. Put the 2 quarts aside and repeat again. Add 2 and 2 together to get 4 quarts. + +or + +fill the 5 quarts with 3 quarts. repeat but leave 1 quart in the 3 quart jug. empty the 5 quart jug and fill it with the 1 quart of water. fill up the 3 quart jug and add it to the 1 quart to get 4 quarts. \ No newline at end of file diff --git a/callBackPractice/callBackPractice.js b/callBackPractice/callBackPractice.js index 8f5d9ac..0c35b25 100644 --- a/callBackPractice/callBackPractice.js +++ b/callBackPractice/callBackPractice.js @@ -17,7 +17,9 @@ // Write a function called firstItem that passes the first item of the given array to the callback function - +const firstItem = (arr, cb) => { + cb(arr[0]); +}; const foods = ['pineapple', 'mango', 'ribeye', 'curry', 'tacos', 'ribeye', 'mango']; firstItem(foods, (firstItem) => { @@ -25,46 +27,68 @@ firstItem(foods, (firstItem) => { }); // Write a function called getLength that passes the length of the array into the callback - +const getLength = (arr, cb) => { + return cb(arr.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 = (arr, cb) => { + return cb(arr[arr.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 = (a, b, cb) => { + cb(a * b); +}; multiplyNums(5, 10, (product) => { console.log(`The product is ${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 = (arr, check, cb) => { + cb(arr.includes(check)); +} contains(foods, 'ribeye', (result) => { console.log(result ? 'ribeye is in the array' : 'ribeye is not in the array'); }); // 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 = (arr, cb) => { + const uniqArr = []; + for (let i = 0; i < arr.length; i++) { + if (!(arr.slice(i+1, arr[arr.length]).includes(arr[i]))) { + uniqArr.push(arr[i]); + } + } + cb(uniqArr); +} 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 = (arr, cb) => { + for (let i = 0; i < arr.length; i++) { + cb(arr[i], i); + } +} forEach(foods, (value, index) => { console.log(`${value} is at index ${index}.`); diff --git a/commonCharacters/commonCharacters.js b/commonCharacters/commonCharacters.js index ec31d82..21ad053 100644 --- a/commonCharacters/commonCharacters.js +++ b/commonCharacters/commonCharacters.js @@ -6,3 +6,15 @@ * Example: commonCharacters('acexivou', 'aegihobu') * * Returns: 'aeiou' */ + +const commonCharacters = (str1, str2) => { + const commonChArr = []; + for (let i = 0; i < str1.length; i++){ + if (!commonChArr.includes(str1[i])){ + if (str2.includes(str1[i])) commonChArr.push(str1[i]); + } + } + return commonChArr; +} + +console.log(commonCharacters('apple','bananas')); diff --git a/constructors/constructors.js b/constructors/constructors.js index 54801f6..7ac6c57 100644 --- a/constructors/constructors.js +++ b/constructors/constructors.js @@ -18,6 +18,72 @@ * * Create properties for these different classes that fit with the character. * - * This is how you would structure the game objects in an actual game + * This is how you would structure the game objectsme in an actual game * application in Unity or another similar framework. - */ \ No newline at end of file + */ + +class NPC { + constructor(name) { + this.alive = true; + this.name = name; + } + attack(target) { + console.log(`${this.name} attacks ${target}`); + } + describe() { + if (this.alive) console.log(`${this.name} is alive`); + else console.log(`${this.name} is dead....`); + } + /*get name() { + return this.name; + }*/ +} + +class Humanoid extends NPC { + constructor(name) { + super(); + } +} +class Animal extends NPC { + +} +class Plant extends NPC { + +} +class Elf extends Humanoid { +} + +class Human extends Humanoid { + +} +class Orc extends Humanoid { + +} +class Peasant extends Humanoid { + +} +class Bandit extends Humanoid { + +} +class Soldier extends Humanoid { + +} +class FleshEatingDaisy extends Plant { + +} +class Wolf extends Animal { + +} +class Bear extends Animal { + +} + +const me = new Humanoid('Matt'); +const me2 = new Elf('Lego'); +const me3 = new NPC('test'); +me3.describe(); +me.attack('nic'); +me.describe(); +me.name; + +//console.log(me2.name;) \ No newline at end of file diff --git a/evenOccurences/evenOccurences.js b/evenOccurences/evenOccurences.js index 1943998..840fdf6 100644 --- a/evenOccurences/evenOccurences.js +++ b/evenOccurences/evenOccurences.js @@ -11,5 +11,22 @@ * */ const evenOccurence = (arr) => { - // Your code here. + const occNums = {}; + arr.forEach((x) => { + if (occNums[x]) occNums[x] += 1; + else occNums[x] = 1; + }); + const occNumKeys = Object.keys(occNums); + let firstEvenOcc; + occNumKeys.forEach((x) => { + if (occNums[x] % 2 === 0) { + if (!firstEvenOcc) firstEvenOcc = x; + return; + } + }); + if (firstEvenOcc) return firstEvenOcc; + return null; }; +const onlyEven = evenOccurence([1, 7, 2, 4, 5, 1, 6, 8, 9, 6, 4, 1]); +console.log(onlyEven); + diff --git a/evenOccurences/evenOccurences.js~ b/evenOccurences/evenOccurences.js~ new file mode 100644 index 0000000..4608cb0 --- /dev/null +++ b/evenOccurences/evenOccurences.js~ @@ -0,0 +1,31 @@ +/* + * * Find the first item that occurs an even number of times in an array. + * * Remember to handle multiple even-occurance items and return the first one. + * * Return null if there are no even-occurance items. + * */ + +/* + * * example usage: + * * console.log(onlyEven); // 4 + * */ + +const evenOccurence = (arr) => { + const occNums = {}; + arr.forEach((x) => { + if (occNums[x]) occNums[x] += 1; + else occNums[x] = 1; + }); + const occNumKeys = Object.keys(occNums); + let firstEvenOcc; + occNumKeys.forEach((x) => { + if (occNums[x] % 2 === 0) { + firstEvenOcc = x; + return; + } + }); + if (firstEvenOcc) return firstEvenOcc; + return null; +}; +const onlyEven = evenOccurence([1, 7, 2, 4, 5, 1, 6, 8, 9, 6, 4, 1]); +console.log(onlyEven); + diff --git a/forLoopTimeout/forLoopTimeout.js b/forLoopTimeout/forLoopTimeout.js index 87522c2..f2c88e8 100644 --- a/forLoopTimeout/forLoopTimeout.js +++ b/forLoopTimeout/forLoopTimeout.js @@ -1,13 +1,20 @@ // Explain what is wrong with this code and how you would fix this. -// With ES6 there is a very, very simple way to solve this. +// With ES6 there is a very, very simple way to solve this. (change var to let) // 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. + +// in this expample, there is closue being created becaues i is call in a higher scope. (i=11 when +//the for loop ends) for (var i = 1; i <= 10; i++) { - setTimeout(function() { + var sT = (function(i) { // 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); -} \ No newline at end of file + return function() { + console.log(i); + } + })(i); + sT(setTimeout,0) +} +//i = 20 \ No newline at end of file diff --git a/isUnique/isUnique.js b/isUnique/isUnique.js index 6c9caf5..a557ca8 100644 --- a/isUnique/isUnique.js +++ b/isUnique/isUnique.js @@ -1,3 +1,20 @@ /* 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 + */ + + + //This function uniqueCh will accept a string and return true or false depending on if it has repeating Characters + //This assumes spaces and other non-letters as characters + //Case sensitive +const uniqueCh = (str) => { + for (let i = 0; i < str.length; i++) { + if (str.slice(i+1, str.length).toUpperCase().includes(str[i].toUpperCase())) { + return false; + } + } + return true +}; + +console.log(uniqueCh("abcdefghii")) +console.log(uniqueCh("abcdefghij")) +console.log(uniqueCh("Halp meh")) \ No newline at end of file diff --git a/largestPrimePalindrome/largestPrimePalindrome.js b/largestPrimePalindrome/largestPrimePalindrome.js index 4cc99c0..859d8e2 100644 --- a/largestPrimePalindrome/largestPrimePalindrome.js +++ b/largestPrimePalindrome/largestPrimePalindrome.js @@ -3,4 +3,36 @@ * 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 isPrime = (num) => { + if (num == 0 || num == 1 || num % 2 == 0) return false; + else if (num == 2) return true; + else { + for (let i = 3; i <= Math.sqrt(num); i += 2) { + if (num%i == 0) return false; + } + return true; + } +} + +const isPalindrome = (num) => { + strNum = num.toString() + for (let i = 0; i < strNum.length / 2; i++) { + if (strNum[i] != strNum[strNum.length - (1+i)]) return false + } + return true +} + +const longestPalindrome = (range) => { + for (let i = range; i > 0; i--) { + if (isPrime(i) && isPalindrome(i)) return i; + } + return undefined; +} +console.log(isPrime(68)) +console.log(isPrime(19)) +console.log(isPalindrome(12345)) +console.log(isPalindrome(123454321)) + +console.log(longestPalindrome(1000)) \ No newline at end of file diff --git a/longestString/longestString.js b/longestString/longestString.js index 35b887c..ed4a5a0 100644 --- a/longestString/longestString.js +++ b/longestString/longestString.js @@ -2,3 +2,21 @@ * Write a function that accepts an array of strings. * Return the longest string in the array. */ + +/* Test Cases +['a', 'bc' , 'acv'] -> 'acv' +strings of the same length with retrn the first longest element +*/ + +// loop through the strings array and search for the longest + +const returnLongestString = (arr) => { + let longestString = ''; + for (let i = 0; i < arr.length; i++) { + if (arr[i].length > longestString.length) { + longestString = arr[i]; + } + } + return longestString; +} + diff --git a/meanMedianMode/meanMedianMode.js b/meanMedianMode/meanMedianMode.js index 821fdc0..5b1c637 100644 --- a/meanMedianMode/meanMedianMode.js +++ b/meanMedianMode/meanMedianMode.js @@ -2,3 +2,37 @@ * Given an array of numbers calculate the mean, median, and mode. * Return an object with properties for the mean, median, and mode. */ + +const mmmArrCalc = arr => { + arr = arr.sort((a,b)=>{return a-b}); + const countEach = {} + let sum = 0; + let med = 0; + if (arr.length %2 === 0) { + med = (arr[arr.length/2] + (arr[(arr.length/2)-1]))/2; + } else med = arr[Math.floor(arr.length/2)]; + for (let i = 0; i < arr.length; i++) { + sum += arr[i]; + if (countEach[arr[i]]) { + countEach[arr[i]] += 1; + } else { + countEach[arr[i]] = 1; + } + } + const countK = Object.keys(countEach); + let most = 0; + let mostValue = 0; + for (let i = 0; i < countK.length; i++) { + if (countEach[countK[i]] > most) { + most = countEach[countK[i]]; + mostValue = countK[i]; + } + } + const mmm = {}; + mmm.median = med; + mmm.mean = sum/arr.length; + mmm.mode = mostValue; + return mmm; +} + +console.log(mmmArrCalc([20,10,30,20])) diff --git a/removeDuplicates/removeDuplicates.js b/removeDuplicates/removeDuplicates.js index 970f719..a093a79 100644 --- a/removeDuplicates/removeDuplicates.js +++ b/removeDuplicates/removeDuplicates.js @@ -9,5 +9,38 @@ */ const removeDuplicates = (arr) => { - //code here... -}; \ No newline at end of file + // 1 + // const newArr = []; + // for (let i = 0; i < arr.length; i++) { + // if (!newArr.includes(arr[i])) { + // newArr.push(arr[i]); + // } + // } + // return newArr; + + // 2 + //assuming the arr is sorted + // let tempNum; + // const newArr = []; + // for (let i = 0; i < arr.length; i++) { + // if (arr[i] !== tempNum) { + // newArr.push(arr[i]); + // } + // tempNum = arr[i]; + // } + // return newArr; + + // 3 + const elSearch = {}; + const newArr = []; + for (let i = 0; i < arr.length; i++) { + if(elSearch[arr[i]] === undefined) { + newArr.push(arr[i]); + elSearch[arr[i]] = true; + } + } + return newArr; +}; + +console.log(removeDuplicates([1,2,3,5,4,6,6,7,9,8])) +console.log(removeDuplicates([4,5,4,2,2,4,5,9,8,7,9,-1,3,2,-1,-2])) \ No newline at end of file diff --git a/reverseCase/reverseCase.js b/reverseCase/reverseCase.js index f1051f5..0587b88 100644 --- a/reverseCase/reverseCase.js +++ b/reverseCase/reverseCase.js @@ -2,4 +2,20 @@ * 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 reverseCase = (str) => { + let newStr = '' + for (let i = 0; i < str.length; i++) { + if (str[i] === str[i].toUpperCase()) { + newStr += str[i].toLowerCase(); + } else { + newStr += str[i].toUpperCase(); + } + } + return newStr; + }; + + console.log(reverseCase('Hello There DUde wHatS upP')) \ No newline at end of file diff --git a/stringCompression/stringCompression.js b/stringCompression/stringCompression.js index 48db571..f9ee5ae 100644 --- a/stringCompression/stringCompression.js +++ b/stringCompression/stringCompression.js @@ -4,3 +4,19 @@ // 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 count = 1; + let newStr = ""; + for (let i = 0; i < str.length; i++) { + if (str[i] === str[i+1]) count += 1; + else { + newStr += `${str[i]}${count}`; + count = 1; + } + } + if (newStr.length < str.length) return newStr; + return str; +} + +console.log(stringCompression('aabcccccaaa')); diff --git a/vowelCount/vowelCount.js b/vowelCount/vowelCount.js index 27fad79..5267243 100644 --- a/vowelCount/vowelCount.js +++ b/vowelCount/vowelCount.js @@ -2,3 +2,19 @@ * Write a function that returns the count of the total number of vowels in a string. * Example: 'Hello World!' -> 3 */ +const vowelCount = (str) => { + const vowels = { + a: true, + e: true, + i: true, + o: true, + u: true + }; + let count = 0; + for (let i = 0; i < str.length; i++){ + if (vowels[str[i].toLowerCase()]) count += 1; + } + return count; +}; + +console.log(vowelCount('Hello WOrld!'));