From ef5a1db71e7647432a5fd11daec6cccae4763fd4 Mon Sep 17 00:00:00 2001 From: Joram Clervius Date: Tue, 8 Aug 2017 12:25:14 -0400 Subject: [PATCH 01/10] longestSting completed --- longestString/longestString.js | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/longestString/longestString.js b/longestString/longestString.js index 35b887c..fdb9e30 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. */ + + /* 1. Identify test cases + ['abc', 'a', 'b'] -> 'abc' + ['def', 'hijk', 'l', 'm', 'nop'] -> 'hijk' + [] -> null +accepts array, +loop over items + keeps tabs of length of looped items, changes variable if current item is longer. + reaches the end and returns the variable. + */ +const longestString = (array) => { + let string = array[0]; + + for (let i = 0; i < array.length; i++) { + if (array[i].length > string.length) { + string = array[i] + } + } + return string; +} + +const arr1 = ['abc', 'a', 'b']; +const arr2 = ['def', 'hijk', 'l', 'm', 'nop'] +const arr3 = [] + +longestString(arr1); +longestString(arr2); +longestString(arr3); \ No newline at end of file From 858cd1e3daba1312c9c2eaadc6e078238cc6b99e Mon Sep 17 00:00:00 2001 From: Joram Clervius Date: Wed, 9 Aug 2017 12:21:51 -0400 Subject: [PATCH 02/10] Problem solved but the code is long. --- reverseCase/reverseCase.js | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/reverseCase/reverseCase.js b/reverseCase/reverseCase.js index f1051f5..1f6abc3 100644 --- a/reverseCase/reverseCase.js +++ b/reverseCase/reverseCase.js @@ -2,4 +2,28 @@ * 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 changeCases = (string) => { + let newString = ''; + for (let i = 0; i < string.length; i++) { + + if (string[i] === ' ') { + newString += string[i]; + } else { + if(string[i] === string[i].toUpperCase()){ + newString += string[i].toLowerCase(); + } else if( string[i] === string[i].toLowerCase()) { + newString += string[i].toUpperCase(); + + } + } + + } + console.log(newString); + return newString; +} + +changeCases('Testing This String'); +changeCases('yOu ThInk yOu Soo SMUUrt!'); +changeCases(' hail hydra'); \ No newline at end of file From fcdc08fe59aa02cf5d5ef619c62821c7d900e401 Mon Sep 17 00:00:00 2001 From: Joram Clervius Date: Thu, 10 Aug 2017 12:29:08 -0400 Subject: [PATCH 03/10] isUnique done. --- isUnique/isUnique.js | 12 +++++++++++- reverseCase/reverseCase.js | 6 +----- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/isUnique/isUnique.js b/isUnique/isUnique.js index 6c9caf5..4a3fbbd 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 = (string) => { + return new Set(string).size === string.length; +} + + + +console.log(isUnique('abcdefghijklmnaa')); +console.log(isUnique('abcdefghijklmnop')); diff --git a/reverseCase/reverseCase.js b/reverseCase/reverseCase.js index 1f6abc3..d65b98b 100644 --- a/reverseCase/reverseCase.js +++ b/reverseCase/reverseCase.js @@ -7,20 +7,16 @@ const changeCases = (string) => { let newString = ''; for (let i = 0; i < string.length; i++) { - if (string[i] === ' ') { newString += string[i]; } else { if(string[i] === string[i].toUpperCase()){ newString += string[i].toLowerCase(); } else if( string[i] === string[i].toLowerCase()) { - newString += string[i].toUpperCase(); - + newString += string[i].toUpperCase(); } } - } - console.log(newString); return newString; } From a0044fa6d417d36efd63e4315064a07a54ef4c38 Mon Sep 17 00:00:00 2001 From: Joram Clervius Date: Fri, 11 Aug 2017 12:22:53 -0400 Subject: [PATCH 04/10] callBackPractice Done. --- callBackPractice/callBackPractice.js | 33 ++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/callBackPractice/callBackPractice.js b/callBackPractice/callBackPractice.js index 8f5d9ac..5be2710 100644 --- a/callBackPractice/callBackPractice.js +++ b/callBackPractice/callBackPractice.js @@ -20,30 +20,47 @@ 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}.`); }); // 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 +69,10 @@ 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, string, cb) => { + cb(new Set(arr).has(string)); +} + contains(foods, 'ribeye', (result) => { console.log(result ? 'ribeye is in the array' : 'ribeye is not in the array'); }); @@ -59,12 +80,20 @@ 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) => { + cb(Array.from(new Set(arr))); +} + 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 f39f0ce21ddfc0698c0352feea0d30aa0e6f61c6 Mon Sep 17 00:00:00 2001 From: Joram Clervius Date: Mon, 14 Aug 2017 12:00:20 -0400 Subject: [PATCH 05/10] Done with remove duplicates. --- removeDuplicates/removeDuplicates.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/removeDuplicates/removeDuplicates.js b/removeDuplicates/removeDuplicates.js index 970f719..50852e0 100644 --- a/removeDuplicates/removeDuplicates.js +++ b/removeDuplicates/removeDuplicates.js @@ -9,5 +9,5 @@ */ const removeDuplicates = (arr) => { - //code here... -}; \ No newline at end of file + return Array.from(new Set(arr)); +}; From d2121a245c5af1e47b634dd74694226d7ad43c5b Mon Sep 17 00:00:00 2001 From: Joram Clervius Date: Tue, 15 Aug 2017 12:10:55 -0400 Subject: [PATCH 06/10] waterJugs completed. --- brainTeasers/waterJugs.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/brainTeasers/waterJugs.md b/brainTeasers/waterJugs.md index 5276fc0..26fa61c 100644 --- a/brainTeasers/waterJugs.md +++ b/brainTeasers/waterJugs.md @@ -1 +1,3 @@ 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 five-quart jug to three quarts using the three-quart jug. Fill up the three-qart jug again and use that to fill up the remaining two quarts of the five-quart jug. You now have one quart left in the three-quart jug. Dispose of all of the water in the five-quart jug, pour the one-quart from the three-quart jug into the five-quart jug. Fill up the three-quart jug and pour it into the five-quart jug. Now you have four quarts of water. From c300aea26f5e1643d1eae936b417ab9bc3f73bde Mon Sep 17 00:00:00 2001 From: Joram Clervius Date: Wed, 16 Aug 2017 12:20:21 -0400 Subject: [PATCH 07/10] Done with for loop. --- forLoopTimeout/forLoopTimeout.js | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/forLoopTimeout/forLoopTimeout.js b/forLoopTimeout/forLoopTimeout.js index 87522c2..6e0d2c6 100644 --- a/forLoopTimeout/forLoopTimeout.js +++ b/forLoopTimeout/forLoopTimeout.js @@ -3,11 +3,14 @@ // 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. +// Do not define function inside for loops, you end up sharing variables. -for (var i = 1; i <= 10; i++) { + +function logVar(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++) { + logVar(i); } \ No newline at end of file From a6b654cd6a341c56a0cb5fb518aa793c3b34a4f8 Mon Sep 17 00:00:00 2001 From: Joram Clervius Surface Date: Mon, 21 Aug 2017 12:27:35 -0400 Subject: [PATCH 08/10] Common characters. --- commonCharacters/commonCharacters.js | 8 +++++ .../largestPrimePalindrome.js | 32 ++++++++++++++++++- 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/commonCharacters/commonCharacters.js b/commonCharacters/commonCharacters.js index ec31d82..1548b96 100644 --- a/commonCharacters/commonCharacters.js +++ b/commonCharacters/commonCharacters.js @@ -6,3 +6,11 @@ * Example: commonCharacters('acexivou', 'aegihobu') * * Returns: 'aeiou' */ +const commonCharacters = (string1,string2) => { + const first = new Set(string1); + const second = new Set(string2); + const returnedStr = [...first].filter(char => second.has(char)); + return returnedStr; +} + +console.log(commonCharacters('acexivou', 'aegihobu')); \ No newline at end of file diff --git a/largestPrimePalindrome/largestPrimePalindrome.js b/largestPrimePalindrome/largestPrimePalindrome.js index 4cc99c0..abfba73 100644 --- a/largestPrimePalindrome/largestPrimePalindrome.js +++ b/largestPrimePalindrome/largestPrimePalindrome.js @@ -3,4 +3,34 @@ * 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 = (input) => { + for (let i = 0; i < Math.floor(input.length / 2); i++) { + if (input[i] !== input[input.length - i - 1]) { + return false; + } + } + return true; +} + +const primePal = () => { + let primePalindrome = 0; + for (let counter = 0; counter <= 100; counter++) { + var notPrime = false; + for (let i = 2; i <= counter; i++) { + if (counter%i===0 && i!==counter) { + notPrime = true; + } + } + if (notPrime === false) { + console.log(counter); + if (isPalindrome(counter) && counter !== primePalindrome) { + primePalindrome = counter; + } + } + } + return primePalindrome; +} + +primePal(); \ No newline at end of file From 7cf26d0cd4601ad4262d42a18adcfcd907b79146 Mon Sep 17 00:00:00 2001 From: Joram Clervius Surface Date: Tue, 22 Aug 2017 12:30:34 -0400 Subject: [PATCH 09/10] evenOccurences done. --- evenOccurences/evenOccurences.js | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/evenOccurences/evenOccurences.js b/evenOccurences/evenOccurences.js index 35da569..65b8d61 100644 --- a/evenOccurences/evenOccurences.js +++ b/evenOccurences/evenOccurences.js @@ -12,4 +12,22 @@ const evenOccurence = (arr) => { // Your code here. + const uniqueSet = Array.from(new Set(arr)); + let counter = 0; + for (let i = 0; i < uniqueSet.length; i++) { + counter = 0; + for (let j = 0; j < arr.length; j++) { + if (uniqueSet[i] === arr[j]) { + counter++; + } + } + if (counter % 2 === 0) { + console.log(counter); + return arr[i]; + } + } + return null; }; + +const onlyEven = evenOccurence([1, 7, 2, 4, 5, 1, 6, 8, 9, 6, 4, 1]); +console.log(onlyEven); \ No newline at end of file From 52cee286bbfa4d98595a30ada131c64a25e34c34 Mon Sep 17 00:00:00 2001 From: Joram Clervius Date: Thu, 24 Aug 2017 12:33:16 -0400 Subject: [PATCH 10/10] Done with vowel count. --- vowelCount/vowelCount.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/vowelCount/vowelCount.js b/vowelCount/vowelCount.js index 27fad79..275fe8c 100644 --- a/vowelCount/vowelCount.js +++ b/vowelCount/vowelCount.js @@ -2,3 +2,17 @@ * 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']; + let counter = 0; + for (let i = 0; i < str.length; i++) { + for (let j = 0; j < vowels.length; j++) { + if (vowels[j] === str[i].toLowerCase()) { + counter++; + } + } + } + return counter; +} + +console.log(vowelCount('Hello World!')); \ No newline at end of file