From 13228e116f7b4acd9726048bbab55018511a8f84 Mon Sep 17 00:00:00 2001 From: Allan Tito Date: Tue, 8 Aug 2017 13:00:49 -0400 Subject: [PATCH 1/9] lomgestString commit by Allan T. --- longestString/longestString.js | 28 ++++++++++++++++++++++++++++ npm-debug.log | 20 ++++++++++++++++++++ 2 files changed, 48 insertions(+) create mode 100644 npm-debug.log 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/npm-debug.log b/npm-debug.log new file mode 100644 index 0000000..26a840f --- /dev/null +++ b/npm-debug.log @@ -0,0 +1,20 @@ +0 info it worked if it ends with ok +1 verbose cli [ '/usr/local/bin/node', '/usr/local/bin/npm', 'test' ] +2 info using npm@3.10.10 +3 info using node@v6.11.2 +4 verbose stack Error: ENOENT: no such file or directory, open '/Users/ALPHA/Lambda/package.json' +4 verbose stack at Error (native) +5 verbose cwd /Users/ALPHA/Lambda/CS2-Code-Challenges +6 error Darwin 15.6.0 +7 error argv "/usr/local/bin/node" "/usr/local/bin/npm" "test" +8 error node v6.11.2 +9 error npm v3.10.10 +10 error path /Users/ALPHA/Lambda/package.json +11 error code ENOENT +12 error errno -2 +13 error syscall open +14 error enoent ENOENT: no such file or directory, open '/Users/ALPHA/Lambda/package.json' +15 error enoent ENOENT: no such file or directory, open '/Users/ALPHA/Lambda/package.json' +15 error enoent This is most likely not a problem with npm itself +15 error enoent and is related to npm not being able to find a file. +16 verbose exit [ -2, true ] From 30c547f44e1a0aca90f17ceac511dc7461c4e784 Mon Sep 17 00:00:00 2001 From: Allan Tito Date: Wed, 9 Aug 2017 14:14:42 -0400 Subject: [PATCH 2/9] Code Challenge commit 9UG --- npm-debug.log | 20 -------------------- reverseCase/reverseCase.js | 14 +++++++++++++- 2 files changed, 13 insertions(+), 21 deletions(-) delete mode 100644 npm-debug.log diff --git a/npm-debug.log b/npm-debug.log deleted file mode 100644 index 26a840f..0000000 --- a/npm-debug.log +++ /dev/null @@ -1,20 +0,0 @@ -0 info it worked if it ends with ok -1 verbose cli [ '/usr/local/bin/node', '/usr/local/bin/npm', 'test' ] -2 info using npm@3.10.10 -3 info using node@v6.11.2 -4 verbose stack Error: ENOENT: no such file or directory, open '/Users/ALPHA/Lambda/package.json' -4 verbose stack at Error (native) -5 verbose cwd /Users/ALPHA/Lambda/CS2-Code-Challenges -6 error Darwin 15.6.0 -7 error argv "/usr/local/bin/node" "/usr/local/bin/npm" "test" -8 error node v6.11.2 -9 error npm v3.10.10 -10 error path /Users/ALPHA/Lambda/package.json -11 error code ENOENT -12 error errno -2 -13 error syscall open -14 error enoent ENOENT: no such file or directory, open '/Users/ALPHA/Lambda/package.json' -15 error enoent ENOENT: no such file or directory, open '/Users/ALPHA/Lambda/package.json' -15 error enoent This is most likely not a problem with npm itself -15 error enoent and is related to npm not being able to find a file. -16 verbose exit [ -2, true ] diff --git a/reverseCase/reverseCase.js b/reverseCase/reverseCase.js index f1051f5..9be2d5a 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 swapCase = (word) => { + const 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); \ No newline at end of file From 5dc0e74aed3f1102a64068613e1dd5154b42c80a Mon Sep 17 00:00:00 2001 From: Allan Tito Date: Mon, 14 Aug 2017 12:59:36 -0400 Subject: [PATCH 3/9] removeDuplicates coding challenge commit 14UG --- callBackPractice/callBackPractice.js | 8 ++++--- isUnique/isUnique.js | 15 +++++++++++++- removeDuplicates/removeDuplicates.js | 31 +++++++++++++++++++++++++--- reverseCase/reverseCase.js | 8 ++++--- 4 files changed, 52 insertions(+), 10 deletions(-) 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/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/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 9be2d5a..e545ef1 100644 --- a/reverseCase/reverseCase.js +++ b/reverseCase/reverseCase.js @@ -2,9 +2,9 @@ * 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. - */ + const swapCase = (word) => { - const swap = { + let swap = " "; for (let i=0; i < word.length; i++) { const str = word.charAt(i); swap += (str == str.toUpperCase() ? str.toLowerCase() : str.toUpperCase()); @@ -14,4 +14,6 @@ return swap; }; const swapper = swapCase('Hello'); - console.log(swapper); \ No newline at end of file + console.log(swapper); + */ + From 1d02ef1038ab913fe80e15d7b33de9045da9276b Mon Sep 17 00:00:00 2001 From: Allan Tito Date: Tue, 15 Aug 2017 12:39:35 -0400 Subject: [PATCH 4/9] Coding Challenge commit 15UG --- brainTeasers/waterJugs.md | 1 + 1 file changed, 1 insertion(+) 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 From b6de972377aab1bdb50c8f3e1e7bef237c9b69f3 Mon Sep 17 00:00:00 2001 From: Allan Tito Date: Wed, 16 Aug 2017 12:52:44 -0400 Subject: [PATCH 5/9] Coding Challenge commit 16UG #HalaMadrid --- forLoopTimeout/forLoopTimeout.js | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/forLoopTimeout/forLoopTimeout.js b/forLoopTimeout/forLoopTimeout.js index 87522c2..88e3d03 100644 --- a/forLoopTimeout/forLoopTimeout.js +++ b/forLoopTimeout/forLoopTimeout.js @@ -3,11 +3,21 @@ // 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); + }); } \ No newline at end of file From 2fa3c855efffb738cabbb05bc2ad7d6d69141bd2 Mon Sep 17 00:00:00 2001 From: Allan Tito Date: Fri, 18 Aug 2017 12:54:30 -0400 Subject: [PATCH 6/9] Coding Challenge commit 18AUG --- constructors/constructors.js | 39 ++++++++++++- forLoopTimeout/forLoopTimeout.js | 15 +++++ .../largestPrimePalindrome.js | 58 ++++++++++++++++++- 3 files changed, 110 insertions(+), 2 deletions(-) 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/forLoopTimeout/forLoopTimeout.js b/forLoopTimeout/forLoopTimeout.js index 88e3d03..ee74593 100644 --- a/forLoopTimeout/forLoopTimeout.js +++ b/forLoopTimeout/forLoopTimeout.js @@ -20,4 +20,19 @@ 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/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); From 502edf720e63b58a3eee4784cf61927e602eeb59 Mon Sep 17 00:00:00 2001 From: Allan Tito Date: Tue, 22 Aug 2017 18:19:58 -0400 Subject: [PATCH 7/9] Coding Challenge commit 22AUG --- commonCharacters/commonCharacters.js | 8 +++++++- evenOccurences/evenOccurences.js | 23 ++++++++++++++++++++--- 2 files changed, 27 insertions(+), 4 deletions(-) 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/evenOccurences/evenOccurences.js b/evenOccurences/evenOccurences.js index 35da569..d145be8 100644 --- a/evenOccurences/evenOccurences.js +++ b/evenOccurences/evenOccurences.js @@ -9,7 +9,24 @@ * * const onlyEven = evenOccurence([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); From 531c52403222c041015f2df590d0123043bcff1d Mon Sep 17 00:00:00 2001 From: Allan Tito Date: Wed, 23 Aug 2017 13:34:48 -0400 Subject: [PATCH 8/9] Coding Challenge commit 23AUG --- stringCompression/stringCompression.js | 30 ++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) 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 From 08975c6bfe0e35e1f9488a6a629a87fd1c308156 Mon Sep 17 00:00:00 2001 From: Allan Tito Date: Thu, 24 Aug 2017 13:00:20 -0400 Subject: [PATCH 9/9] Coding Challenge commit 24AUG --- vowelCount/vowelCount.js | 7 +++++++ 1 file changed, 7 insertions(+) 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