From bef6b1dfdc4e332ba76109e37baeeab587262fb0 Mon Sep 17 00:00:00 2001 From: Adam Lower Date: Tue, 8 Aug 2017 12:19:36 -0400 Subject: [PATCH 01/11] Finished longestString --- longestString/longestString.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/longestString/longestString.js b/longestString/longestString.js index 35b887c..670473d 100644 --- a/longestString/longestString.js +++ b/longestString/longestString.js @@ -2,3 +2,12 @@ * Write a function that accepts an array of strings. * Return the longest string in the array. */ +const longestString = (arr) => { + let longest = ''; + for (let i = 0; i < arr.length; i++) { + if (arr[i].length > longest.length) { + longest = arr[i]; + } + } + return longest; +} \ No newline at end of file From eb0013fce50acfff59980847a6b862443cce3834 Mon Sep 17 00:00:00 2001 From: Adam Lower Date: Tue, 8 Aug 2017 14:13:29 -0400 Subject: [PATCH 02/11] Fixed initialization --- longestString/longestString.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/longestString/longestString.js b/longestString/longestString.js index 670473d..130a72e 100644 --- a/longestString/longestString.js +++ b/longestString/longestString.js @@ -3,11 +3,11 @@ * Return the longest string in the array. */ const longestString = (arr) => { - let longest = ''; + let longest = arr[0]; for (let i = 0; i < arr.length; i++) { if (arr[i].length > longest.length) { longest = arr[i]; } } return longest; -} \ No newline at end of file +}; \ No newline at end of file From 365d196d9b0cb82c9ce4e2344e59a7cf70b9705b Mon Sep 17 00:00:00 2001 From: Adam Lower Date: Wed, 9 Aug 2017 12:42:06 -0400 Subject: [PATCH 03/11] Finished reverseCase --- reverseCase/reverseCase.js | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/reverseCase/reverseCase.js b/reverseCase/reverseCase.js index f1051f5..943e0c8 100644 --- a/reverseCase/reverseCase.js +++ b/reverseCase/reverseCase.js @@ -2,4 +2,16 @@ * 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 = (string) => { + let newStr = ''; + for (let i = 0; i < string.length; i++) { + if (string[i] === string[i].toUpperCase()) { + newStr = newStr.concat(string[i].toLowerCase()); + } else { + newStr = newStr.concat(string[i].toUpperCase()); + } + } + return newStr; + } \ No newline at end of file From fff3021db639235db3baf0115767f12edad0b54e Mon Sep 17 00:00:00 2001 From: Adam Lower Date: Thu, 10 Aug 2017 12:18:53 -0400 Subject: [PATCH 04/11] Finished isUnique;js --- isUnique/isUnique.js | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/isUnique/isUnique.js b/isUnique/isUnique.js index 6c9caf5..196d053 100644 --- a/isUnique/isUnique.js +++ b/isUnique/isUnique.js @@ -1,3 +1,13 @@ /* 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 = (str) => { + const uniqueStr = new Set(); + for (let i = 0; i < str.length; i++) { + if (uniqueStr.has(str[i])) { + return false; + } + uniqueStr.add(str[i]); + } + return true; + } \ No newline at end of file From 3011f3a812f62a7b8e232a628a6c2f4f5c07f40b Mon Sep 17 00:00:00 2001 From: Adam Lower Date: Fri, 11 Aug 2017 12:45:35 -0400 Subject: [PATCH 05/11] Finished callBackPractice.js --- callBackPractice/callBackPractice.js | 32 ++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/callBackPractice/callBackPractice.js b/callBackPractice/callBackPractice.js index 8f5d9ac..3501f69 100644 --- a/callBackPractice/callBackPractice.js +++ b/callBackPractice/callBackPractice.js @@ -20,24 +20,31 @@ const foods = ['pineapple', 'mango', 'ribeye', 'curry', 'tacos', 'ribeye', 'mango']; +const firstItem = (arr, cb) => cb(arr[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 = (arr, cb) => 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) => 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}.`); @@ -45,6 +52,8 @@ sumNums(5, 10, (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 +61,14 @@ 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 = (arr, item, cb) => { + for (let i = 0; i < arr.length; i++) { + if (arr[i] === item); + return cb(true); + } + return cb(false); +}; + contains(foods, 'ribeye', (result) => { console.log(result ? 'ribeye is in the array' : 'ribeye is not in the array'); }); @@ -59,12 +76,27 @@ 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 = (arr, cb) => { + const newArr = []; + for (let i = 0; i < arr.length; i++) { + if (!(newArr.includes(arr[i]))) { + newArr.push(arr[i]); + } + } + cb(newArr); +}; + 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}.`); From cea8c8769bc27bab6f2b7100d82d43e66678a268 Mon Sep 17 00:00:00 2001 From: Adam Lower Date: Mon, 14 Aug 2017 12:13:56 -0400 Subject: [PATCH 06/11] Finished removeDuplicates --- removeDuplicates/removeDuplicates.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/removeDuplicates/removeDuplicates.js b/removeDuplicates/removeDuplicates.js index 970f719..9ffa1aa 100644 --- a/removeDuplicates/removeDuplicates.js +++ b/removeDuplicates/removeDuplicates.js @@ -9,5 +9,11 @@ */ const removeDuplicates = (arr) => { - //code here... + const noDuplicates = []; + for (let i = 0; i < arr.length; i++) { + if(!(noDuplicates.includes(arr[i]))) { + noDuplicates.push(arr[i]); + } + } + return noDuplicates; }; \ No newline at end of file From 0c72619d7e8066d9410a94fdf53835a72abee026 Mon Sep 17 00:00:00 2001 From: Adam Lower Date: Tue, 15 Aug 2017 13:19:06 -0400 Subject: [PATCH 07/11] Finished waterJugs.md --- brainTeasers/waterJugs.md | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/brainTeasers/waterJugs.md b/brainTeasers/waterJugs.md index 5276fc0..e740359 100644 --- a/brainTeasers/waterJugs.md +++ b/brainTeasers/waterJugs.md @@ -1 +1,12 @@ 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 +------------------ + 0 | 3 + 3 | 0 + 3 | 3 + 5 | 1 + 0 | 1 + 1 | 0 + 1 | 3 + 4 | 0 \ No newline at end of file From fe53c8afb26f870f2baed6cc211a32ababf61879 Mon Sep 17 00:00:00 2001 From: Adam Lower Date: Tue, 15 Aug 2017 13:19:23 -0400 Subject: [PATCH 08/11] Added 'beast mode' to removeDuplicates.js --- removeDuplicates/removeDuplicates.js | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/removeDuplicates/removeDuplicates.js b/removeDuplicates/removeDuplicates.js index 9ffa1aa..edd7858 100644 --- a/removeDuplicates/removeDuplicates.js +++ b/removeDuplicates/removeDuplicates.js @@ -8,12 +8,26 @@ * you're more than likely using a for loop under the hood. */ +//initial code const removeDuplicates = (arr) => { - const noDuplicates = []; + const newArr = []; for (let i = 0; i < arr.length; i++) { - if(!(noDuplicates.includes(arr[i]))) { - noDuplicates.push(arr[i]); + if(!(newArr.includes(arr[i]))) { + newArr.push(arr[i]); } } - return noDuplicates; -}; \ No newline at end of file + return newArr; +}; + +//"beast mode" +const removeDuplicates = (arr) => { + const newArr = []; + const obj = {}; + for (let i =0; i < arr.length; i++) { + if (!obj[arr[i]]) { + obj[arr[i]] = true; + newArr.push(arr[i]); + } + } + return newArr; +} \ No newline at end of file From 5c6c014e0b92bfebda5409903efba2d7c2c8501c Mon Sep 17 00:00:00 2001 From: Adam Lower Date: Wed, 16 Aug 2017 13:17:45 -0400 Subject: [PATCH 09/11] Finished forLoopTimeout --- forLoopTimeout/forLoopTimeout.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/forLoopTimeout/forLoopTimeout.js b/forLoopTimeout/forLoopTimeout.js index 87522c2..e9e6051 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); +} + +// Closure issue. Var is globally scoped, change to let. + +for (let i = 1; i <= 10; i++) { + setTimeout(function() { + console.log(i); + }, 0); } \ No newline at end of file From f312172a39a16af5257210c129839612b318dceb Mon Sep 17 00:00:00 2001 From: Adam Lower Date: Wed, 23 Aug 2017 13:13:02 -0400 Subject: [PATCH 10/11] Finished --- commonCharacters/commonCharacters.js | 13 ++++++++++++ .../largestPrimePalindrome.js | 21 ++++++++++++++++++- 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/commonCharacters/commonCharacters.js b/commonCharacters/commonCharacters.js index ec31d82..ac64433 100644 --- a/commonCharacters/commonCharacters.js +++ b/commonCharacters/commonCharacters.js @@ -6,3 +6,16 @@ * Example: commonCharacters('acexivou', 'aegihobu') * * Returns: 'aeiou' */ + +const commonCharacters = (str1, str2) => { + let common = []; + + str1.split('').forEach((char) => { + if (str2.split('').includes(char) && !common.includes(char)) { + common.push(char); + } + }) + return common.join('').replace(' ', ''); +} + +console.log(commonCharacters('abcdefg', 'bcd g')); \ No newline at end of file diff --git a/largestPrimePalindrome/largestPrimePalindrome.js b/largestPrimePalindrome/largestPrimePalindrome.js index 4cc99c0..b1f267d 100644 --- a/largestPrimePalindrome/largestPrimePalindrome.js +++ b/largestPrimePalindrome/largestPrimePalindrome.js @@ -3,4 +3,23 @@ * 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 largestPrimePalindrome = () => { + const isPalindrome = (num) => { + if (num === num.toString().split('').reverse().join('')) return true; + } + + const isPrime = (num) => { + if (num % 2 !== 0) { + for (let j = 3; j <= Math.sqrt(num); j++) { + if (num % j === 0) return false; + } + return true; + } + } + + for (let i = 1000; i > 0; i--) { + if (isPalindrome(i) && isPrime(i)) return i; + } + } \ No newline at end of file From 9d51407ac564b7551967bf862934359719dcbd93 Mon Sep 17 00:00:00 2001 From: Adam Lower Date: Mon, 6 Nov 2017 21:04:21 -0500 Subject: [PATCH 11/11] Finished Challenges --- evenOccurences/evenOccurences.js | 19 ++++++++++++++++--- stringCompression/stringCompression.js | 16 ++++++++++++++++ vowelCount/vowelCount.js | 12 ++++++++++++ 3 files changed, 44 insertions(+), 3 deletions(-) diff --git a/evenOccurences/evenOccurences.js b/evenOccurences/evenOccurences.js index 1943998..13f27ec 100644 --- a/evenOccurences/evenOccurences.js +++ b/evenOccurences/evenOccurences.js @@ -10,6 +10,19 @@ * * console.log(onlyEven); // 4 * */ -const evenOccurence = (arr) => { - // Your code here. -}; +function evenOccurrence (arr) { + var storage = {}; + + arr.forEach((item) => { + storage[item] = storage[item] + 1 || 1; + }); + + for (var i = 0; i < arr.length; i++) { + if (storage[arr[i]] % 2 === 0) { + return current; + } + } + return null; +} + +console.log(evenOccurrence([1, 7, 2, 4, 5, 1, 6, 8, 9, 6, 4, 1])); \ No newline at end of file diff --git a/stringCompression/stringCompression.js b/stringCompression/stringCompression.js index 48db571..dd3eeef 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 compressString = (string) => { + let currentChar = string.charAt(0); + let compressedString = ''; + let charCount = 0; + for (let i = 0; i < string.length; i++) { + if (string.charAt(i) === currentChar) charCount++; + if (string.charAt(i) !== currentChar || string.charAt(i) === undefined) { + compressedString += currentChar; + compressedString += charCount; + charCount = 1; + currentChar = string.charAt(i); + } + } + return compressedString.length > string.length ? string : compressedString; +} \ No newline at end of file diff --git a/vowelCount/vowelCount.js b/vowelCount/vowelCount.js index 27fad79..61d55dc 100644 --- a/vowelCount/vowelCount.js +++ b/vowelCount/vowelCount.js @@ -2,3 +2,15 @@ * 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', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U']; + let count = 0; + + for (let i = 0; i < str.length; i++) { + if (vowels.indexOf(str[i]) > -1) { + counter++; + } + } + return counter; + }; \ No newline at end of file