diff --git a/brainTeasers/waterJugs.md b/brainTeasers/waterJugs.md index 5276fc0..b531409 100644 --- a/brainTeasers/waterJugs.md +++ b/brainTeasers/waterJugs.md @@ -1 +1,10 @@ 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) Fill up 3 quart jug +2) Use the now full 3 quart jug to fill the 5 quart jug, twice +3) Now you're left with 1 quart in the 3 quart jug +4) Empty the 5 quart jug +5) Transfer that 1 quart of water from the 3 qt jug into the now empty 5 quart jug +6) Fill up the 3 quart jug again +7) Dump it into the 5 quart +8) You now have 4 quarts in the 5 qt jug \ No newline at end of file diff --git a/callBackPractice/callBackPractice.js b/callBackPractice/callBackPractice.js index 8f5d9ac..747667a 100644 --- a/callBackPractice/callBackPractice.js +++ b/callBackPractice/callBackPractice.js @@ -24,18 +24,30 @@ firstItem(foods, (firstItem) => { console.log(`The first item is ${firstItem}.`); }); +const firstItem = (arr, cb) => { + cb(arr[0]); +}; + // Write a function called getLength that passes the length of the array into the callback getLength(foods, (length) => { console.log(`The length of the array is ${length}.`); }); +const getLength = (arr, cb) => { + cb(arr.length); +}; + // Write a function called last which passes the last item of the array into the callback last(foods, (lastItem) => { console.log(`The last item in the array is ${lastItem}.`); }); +const last = (arr, cb) => { + cb(arr[arr.length - 1]); +} + // Write a function called sumNums that adds two numbers and passes the result to the callback @@ -43,12 +55,19 @@ sumNums(5, 10, (sum) => { console.log(`The sum is ${sum}.`); }); +const sumNums = (num1, num2, cb) => { + cb(num1 + num2); +}; + // Write a function called multiplyNums that adds two numbers and passes the result to the callback multiplyNums(5, 10, (product) => { console.log(`The product is ${product}.`); }); +const multiplyNums = (num1, num2, cb) => { + cb(num1 * num2); +}; // 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 @@ -56,6 +75,14 @@ contains(foods, 'ribeye', (result) => { console.log(result ? 'ribeye is in the array' : 'ribeye is not in the array'); }); +const contains = (arr, item, cb) => { + if (arr.includes(item)) { + cb(true); + } else { + cb(false); + } +}; + // 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. @@ -63,9 +90,26 @@ removeDuplicates(foods, (uniqueFoods) => { console.log(`foods with duplicates removed: ${uniqueFoods}`); }); +const removeDuplicates = (arr, cb) => { + const unique = []; + + arr.forEach((value) => { + if (!arr.includes(value)) { + arr.push(value); + } + }); + cb(unique); +}; + // Write a function called forEach that iterates over the provided array and passes the value and index into the callback. forEach(foods, (value, index) => { console.log(`${value} is at index ${index}.`); -}); \ No newline at end of file +}); + +const forEach = (arr, cb) => { + for (let i = 0; i < arr.length; i++) { + cb(arr[i], i); + } +}; \ No newline at end of file diff --git a/commonCharacters/commonCharacters.js b/commonCharacters/commonCharacters.js index ec31d82..7fbebaf 100644 --- a/commonCharacters/commonCharacters.js +++ b/commonCharacters/commonCharacters.js @@ -1,8 +1,22 @@ /* * Common Characters: - * 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. + * 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' */ + +const commonCharacters = function(string1, string2) { + let sameChars = ''; + for (let i = 0; i < string2.length; i++) { + if (string1.includes(string2[i])) { + sameChars += string2[i]; + } + } + return `Identical characters: ${sameChars}`; +}; + +console.log(commonCharacters('acexivou', 'aegihobu')); \ No newline at end of file diff --git a/constructors/constructors.js b/constructors/constructors.js index 54801f6..aae8248 100644 --- a/constructors/constructors.js +++ b/constructors/constructors.js @@ -20,4 +20,95 @@ * * 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(stats) { + this.hp = stats.hp; + this.strength = stats.strength; + this.speed = stats.speed; + this.luck = stats.luck; + this.dexterity = stats.dexterity; + this.defense = stats.defense; + } +} + +class Humanoid extends NPC { + constructor(stats) { + super(stats); + this.weapon = stats.weapon; + this.armor = stats.armor; + this.inventory = stats.inventory; + } +} + +class Human extends Humanoid { + constructor(stats) { + super(stats); + this.defenseBoost = stats.defense + 25; + } +} + +class Soldier extends Human { + constructor(stats) { + super(stats); + } +} + +class Peasant extends Human { + constructor(stats) { + super(stats); + } +} + +class Bandit extends Human { + constructor(stats) { + super(stats); + } +} + +class Elf extends Humanoid { + constructor(stats) { + super(stats); + this.dexterityBoost = stats.dexterity + 25; + } +} + +class Orc extends Humanoid { + constructor(stats) { + super(stats); + this.StrengthBoost = stats.strength + 25; + } +} + +class Animal extends NPC { + constructor(stats) { + super(stats); + this.hostility = stats.hostility; + } +} + +class Bear extends Animal { + constructor(stats) { + super(stats); + } +} + +class Wolf extends Animal { + constructor(stats) { + super(stats); + } +} + +class Plant extends NPC { + constructor(stats) { + super(stats); + this.magicProperties = stats.magicProperties; + } +} + +class FleshEatingDaisy extends Plant { + constructor(stats) { + super(stats); + } +} \ No newline at end of file diff --git a/evenOccurences/evenOccurences.js b/evenOccurences/evenOccurences.js index 1943998..1239c27 100644 --- a/evenOccurences/evenOccurences.js +++ b/evenOccurences/evenOccurences.js @@ -10,6 +10,22 @@ * * console.log(onlyEven); // 4 * */ + const evenOccurence = (arr) => { - // Your code here. + let storage = {}; + + arr.forEach(function(value, index) { + storage[value] = storage[value] + 1 || 1; + }); + + for (let i = 0; i < arr.length; i++) { + if (storage[arr[i]] % 2 === 0) { + return arr[i]; + } + } + + return null; }; + +const nums = [1, 2, 2, 3, 4, 4, 4, 5]; +console.log(evenOccurence(nums)); \ No newline at end of file diff --git a/forLoopTimeout/forLoopTimeout.js b/forLoopTimeout/forLoopTimeout.js index 87522c2..edd6fee 100644 --- a/forLoopTimeout/forLoopTimeout.js +++ b/forLoopTimeout/forLoopTimeout.js @@ -4,10 +4,25 @@ // 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 (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); -} \ No newline at end of file + setTimeout(function(x) { + return function() { + console.log(x); + }; }(i), 0); +} + +// We're only passing the refernece to the variable i +// and not the the actual value of i when it goes through each loop +// So by the time the setTimeout function actually runs, the for statement +// has already run and produced the number 11 +// Needed to pass the actual value of i at each iteration, so +// another anon function was needed as a wrapper +// Outer function executes first, and takes in a private variable x, which we pass i into diff --git a/isUnique/isUnique.js b/isUnique/isUnique.js index 6c9caf5..08a47b8 100644 --- a/isUnique/isUnique.js +++ b/isUnique/isUnique.js @@ -1,3 +1,28 @@ /* 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 findUnique = (str) => { +// let unique=''; + +// for (let i = 0; i < str.length; i++) { +// if(unique.indexOf(str[i]) === -1) { +// unique += str[i]; +// } +// } +// return unique; +// }; + +// console.log(findUnique('This is a string')); + +// ^^^ old code because i misunderstood the assignment, lol + + + 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; + }; \ No newline at end of file diff --git a/largestPrimePalindrome/largestPrimePalindrome.js b/largestPrimePalindrome/largestPrimePalindrome.js index 4cc99c0..957313f 100644 --- a/largestPrimePalindrome/largestPrimePalindrome.js +++ b/largestPrimePalindrome/largestPrimePalindrome.js @@ -3,4 +3,33 @@ * 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 isPalindrome = (num) => { + const num2Str = num.toString(); + + if (num2Str === num2Str.split('').reverse().join('')) return true; + return false; +} + +const isPrime = (num) => { + if (num === 1 || num === 3) return false; + for (let i = 2; i < num; i++) { + if (num % i === 0) return false; + } + return true; +} + +const lPPArray = []; + +const largestPrimePalindrome = (num) => { + for (let i = 2; i < 1000; i++) { + if (isPalindrome(i) && isPrime(i)) { + lPPArray.push(i); + } + } + return lPPArray[lPPArray.length - 1]; +} + +console.log(largestPrimePalindrome()); \ No newline at end of file diff --git a/longestString/longestString.js b/longestString/longestString.js index 35b887c..1ef4faa 100644 --- a/longestString/longestString.js +++ b/longestString/longestString.js @@ -1,4 +1,31 @@ -/* +/* * 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 +*/ + +function findLongestWord(array) { + let longestWord = ''; + + array.forEach(word => { + if(word.length > longestString.length) { + longestWord = word; + } + }); + + return longestWord; +} + +const word = findLongestWord(['Another', 'one', 'bites', 'the', 'dust']); +console.log(word); \ No newline at end of file diff --git a/meanMedianMode/meanMedianMode.js b/meanMedianMode/meanMedianMode.js index 821fdc0..a21b0bf 100644 --- a/meanMedianMode/meanMedianMode.js +++ b/meanMedianMode/meanMedianMode.js @@ -2,3 +2,57 @@ * Given an array of numbers calculate the mean, median, and mode. * Return an object with properties for the mean, median, and mode. */ + +const mean = function(arr) { + let total = 0, i; + + for (i = 0; i < arr.length; i++) { + total += arr[i]; + } + return total / arr.length; +} + +const median = function(arr) { + let median = 0; + + if (arr.length % 2 === 0) { + median = (arr[arr.length / 2 - 1] + arr[arr.length / 2] / 2); + } else { + median = numbers[(arr.length - 1) / 2]; + } + return median; +} + +const mode = function(arr) { + let modes = []; + let count = []; + let i = 0; + let number = 0; + let maxIndex = 0; + + for (i = 0; i < arr.length; i++) { + number = arr[i]; + count[number] = (count[number] || 0) + 1; + if (count[number] > maxIndex) { + maxIndex = count[number]; + } + } + + for (i in count) { + if (count.hasOwnProperty(i)) { + if (count[i] === maxIndex) { + modes.push(Number(i)); + } + } + return modes; + } +} + +function meanMedianMode(arr) { + const mMMObj = { + mean: mean(arr), + median: median(arr), + mode: mode(arr) + }; + return mMMObj; +} \ No newline at end of file diff --git a/removeDuplicates/removeDuplicates.js b/removeDuplicates/removeDuplicates.js index 970f719..da3d9ae 100644 --- a/removeDuplicates/removeDuplicates.js +++ b/removeDuplicates/removeDuplicates.js @@ -9,5 +9,14 @@ */ const removeDuplicates = (arr) => { - //code here... -}; \ No newline at end of file + let decluttered = [arr[0]]; + + for (let i = 1; i < arr.length; i++) { + if (arr[i] != arr[i-1]) { + decluttered.push(arr[i]); + } + } + return decluttered; +}; + +console.log(removeDuplicates([0, 1, 1, 2, 3, 3, 3, 4, 4, 5])); diff --git a/reverseCase/reverseCase.js b/reverseCase/reverseCase.js index f1051f5..d884d48 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++) { + let char = str[i]; + if (char === char.toLowerCase()) { + newStr = newStr + char.toUpperCase(); + } else { + newStr = newStr + char.toLowerCase(); + } + } + return newStr; + }; + + console.log(reverseCase('Hello World')); diff --git a/stringCompression/stringCompression.js b/stringCompression/stringCompression.js index 48db571..f8c17cc 100644 --- a/stringCompression/stringCompression.js +++ b/stringCompression/stringCompression.js @@ -4,3 +4,31 @@ // 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 = function(str) { + let count = 1; + let previous = str.charAt(0); + let compressedStr = ''; + + for (let i = 1; i < str.length; i++) { + var current = str[i]; + + if (previous === current) { + count++; + } else { + compressedStr += previous + count; + previous = current; + count = 1; + } + } + + if (count === 1) { + compressedStr += current; + } else { + compressedStr += current + count; + } + + return compressedStr; +} + +console.log(stringCompression('aaaabbbbcddd')); \ No newline at end of file diff --git a/vowelCount/vowelCount.js b/vowelCount/vowelCount.js index 27fad79..1f298ac 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 = function(str) { + let m = str.match(/[aeiou]/gi); + return m === null ? 0 : m.length; + } + + console.log(vowelCount('Hello World!')); \ No newline at end of file