From c1c71e67ada36acf2db94810ea99fd9e734abd2f Mon Sep 17 00:00:00 2001 From: Raymond Date: Tue, 8 Aug 2017 18:30:00 -0400 Subject: [PATCH 01/11] Challenge complete --- longestString/longestString.js | 60 ++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/longestString/longestString.js b/longestString/longestString.js index 35b887c..bc0dc21 100644 --- a/longestString/longestString.js +++ b/longestString/longestString.js @@ -2,3 +2,63 @@ * 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 +// ['abc', 'a', 'b'] -> 'abc' +// ['abc', 'def'] -> 'def' +// [] -> null + +/* +function longestString strings + loop over the array of strings + do something in the loop + return longestString +*/ +// let longest = 0; +// let strings = ['Hello there', 'go HOME NOW PLEASE', 'NOPE']; + +// const longestString = (strings) => { +// for(let i = 0; i < strings.length; i++){ +// if(strings[i].length > longest){ +// longest = strings[i]; +// } +// } +// return longest; +// }; + +// console.log(longestString(strings)); + +// let longest = 0; +// let strings = ['Hello there please go to the basement', 'go HOME NOW PLEASE', 'NOPE']; + +// const longestString = (strings) => { +// for(let i = 0; i < strings.length; i++){ +// if(strings[i].length > longest){ +// if(strings[i] > strings){ +// longest = strings[i]; +// } +// } +// } +// return longest; +// }; + +// console.log(longestString(strings)); + + +let strings = ['This is a story all about how ', 'my life got twist turned upside down', 'NOPE']; + +const longestString = (strings) => { + let longest = strings[0]; + for(let i = 0; i < strings.length; i++){ + if(strings[i].length > longest.length){ + longest = strings[i]; + } + } + return longest; +}; + +console.log(longestString(strings)); \ No newline at end of file From f699df384afcac6cdfbbf327e013c4e196ef99b4 Mon Sep 17 00:00:00 2001 From: Raymond Date: Thu, 10 Aug 2017 10:20:22 -0400 Subject: [PATCH 02/11] Complete Reverse Case --- reverseCase/reverseCase.js | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/reverseCase/reverseCase.js b/reverseCase/reverseCase.js index f1051f5..14d89cf 100644 --- a/reverseCase/reverseCase.js +++ b/reverseCase/reverseCase.js @@ -2,4 +2,19 @@ * 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 stringChange = (string)=>{ + let newString =''; + for (let i = 0; i < string.length; i++){ + if(string[i] === string[i].toUpperCase()){ + newString += string[i].toLowerCase(); + } else { + newString += string[i].toUpperCase(); + } + + } + console.log(newString); +}; + +(stringChange('rAYMOND Rosario')); \ No newline at end of file From 26d922b4884fcac3b088920ea85d9951bc319b3a Mon Sep 17 00:00:00 2001 From: Raymond Date: Mon, 14 Aug 2017 12:47:18 -0400 Subject: [PATCH 03/11] Complete Challenge --- callBackPractice/callBackPractice.js | 45 ++++++++++- callBackPractice/solution.js | 107 +++++++++++++++++++++++++++ isUnique/isUnique.js | 24 +++++- isUnique/solution.js | 16 ++++ removeDuplicates/removeDuplicates.js | 13 ++++ reverseCase/solution.js | 23 ++++++ 6 files changed, 226 insertions(+), 2 deletions(-) create mode 100644 callBackPractice/solution.js create mode 100644 isUnique/solution.js create mode 100644 removeDuplicates/removeDuplicates.js create mode 100644 reverseCase/solution.js diff --git a/callBackPractice/callBackPractice.js b/callBackPractice/callBackPractice.js index 8f5d9ac..50b567e 100644 --- a/callBackPractice/callBackPractice.js +++ b/callBackPractice/callBackPractice.js @@ -20,31 +20,50 @@ const foods = ['pineapple', 'mango', 'ribeye', 'curry', 'tacos', 'ribeye', 'mango']; +const firstItem = (foods, cb) => { + cb(foods[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 = (foods, cb) => { + cb(foods.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 = (foods, cb) => { + cb(foods[foods.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 +71,15 @@ 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 = (foods, item, cb) => { + for(let i = 0; i < foods.length; i++){ + if(foods[i] === item){ + cb(true); + } + } +return false; +} + contains(foods, 'ribeye', (result) => { console.log(result ? 'ribeye is in the array' : 'ribeye is not in the array'); }); @@ -59,11 +87,26 @@ 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 = (foods, cb) => { + for (let i = 0; i < foods.length; i++){ + if(foods[i]===foods[i]) + + } +} + + 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 = (foods, cb) => { + for (let i = 0; i < foods.length; i++){ + cb(foods[i], i); + } +} + + forEach(foods, (value, index) => { diff --git a/callBackPractice/solution.js b/callBackPractice/solution.js new file mode 100644 index 0000000..9331068 --- /dev/null +++ b/callBackPractice/solution.js @@ -0,0 +1,107 @@ +/* For today's coding challenge your job is to write functions + * so that each function call works. + * + * Example: + * + * greeting('Hey guys', (message) => { + * console.log(message); + * }); + * + * You would then define the greeting function to make the invocation work. + * + * 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']; + +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 = (x, y, cb) => { + cb(x + y); +}; + +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 = (x, y, cb) => { + cb(x * y); +}; + +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 = (collection, item, cb) => { + cb(collection.includes(item)); +}; + +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 set = new Set(); + arr.forEach(item => { + set.add(item); + }); + cb(Array.from(set)); +}; + +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}.`); +}); \ No newline at end of file diff --git a/isUnique/isUnique.js b/isUnique/isUnique.js index 6c9caf5..eb228f7 100644 --- a/isUnique/isUnique.js +++ b/isUnique/isUnique.js @@ -1,3 +1,25 @@ /* 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) => { + let uniqueStr = ' '; + let nonUnique = ' '; + for (let i = 0; i < string.length; i++){ + if(uniqueStr[i] !== string[i]){ + uniqueStr += string[i]; + } else if (string[i]== string[i]){ + nonUnique += string[i]; + } + } + console.log(uniqueStr); +} + +isUnique('Goodbyee') + +// Issue is that it catches a nonUniqe and stores it only once +// Ex: Goodbyee turns into Godbyee +//Try to figure out why that is + +//Incrementing (++) won't work. Re-ordering of test statements doesn't work. \ No newline at end of file diff --git a/isUnique/solution.js b/isUnique/solution.js new file mode 100644 index 0000000..c02ec60 --- /dev/null +++ b/isUnique/solution.js @@ -0,0 +1,16 @@ +/* If not allowed another data structure then it would probably be O(n^2). + * You could use a O(nlogn) sorting algorithm though to sort the array + * and then just check to see if neighbors match. This would be linear. + */ + +const isUnique = (str) => { + const strSet = new Set(); + for (let i = 0; i < str.length; i++) { + if (strSet.has(str[i])) return false; + strSet.add(str[i]); + } + return true; +}; + +console.log(isUnique('abcdhijklmnopqrstuv')); // true +console.log(isUnique('abcdefga')); // false \ No newline at end of file diff --git a/removeDuplicates/removeDuplicates.js b/removeDuplicates/removeDuplicates.js new file mode 100644 index 0000000..cd3bf7e --- /dev/null +++ b/removeDuplicates/removeDuplicates.js @@ -0,0 +1,13 @@ +/* + * Write a function that takes an array and removes all duplicate items. + * [1, 1, 1, 2, 2, 3, 4, 5, 5] -> [1, 2, 3, 4, 5] + * + * beast mode: try not to use two for loops. + * hint: most array methods native to JS iterate in some way. + * So if you're using 'indexOf' 'sort' 'forEach' etc, + * you're more than likely using a for loop under the hood. + */ + +const removeDuplicates = arr.filter( (el, i, arr) => array.indexOf(el) === i); + +console.log(removeDuplicates); \ No newline at end of file diff --git a/reverseCase/solution.js b/reverseCase/solution.js new file mode 100644 index 0000000..5b4e47b --- /dev/null +++ b/reverseCase/solution.js @@ -0,0 +1,23 @@ +// 'abcDEF' -> 'ABCdef' +// 'Hello World' -> 'hELLO wORLD' + +// split the string into an array +// iterate over the array + // if character is uppercase flip it to lowercase + // make lowercase character uppercase +// join the array back into a string +// return the string + +const reverseCase = (str) => { + const characters = str.split(''); + characters.forEach((character, i) => { // 'A' === 'A' + if (character.toUpperCase() === character) { + characters[i] = character.toLowerCase(); + } else { + characters[i] = character.toUpperCase(); + } + }); + return characters.join(''); +}; + +console.log(reverseCase('Hello World')); \ No newline at end of file From 285593510d3364742e0ca818e3428a6847999b96 Mon Sep 17 00:00:00 2001 From: Raymond Date: Wed, 16 Aug 2017 12:38:31 -0400 Subject: [PATCH 04/11] challenge complete --- forLoopTimeout/forLoopTimeout.js | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/forLoopTimeout/forLoopTimeout.js b/forLoopTimeout/forLoopTimeout.js index 87522c2..2ed67d3 100644 --- a/forLoopTimeout/forLoopTimeout.js +++ b/forLoopTimeout/forLoopTimeout.js @@ -10,4 +10,33 @@ for (var i = 1; i <= 10; i++) { // It doesn't. Why? How can you make it print 1 - 10. console.log(i); }, 0); -} \ No newline at end of file +} + +////////////////////\\\\\\\\\\\\\\\\\\ +for (var i = 1; i <= 10; i++) { + setDelay(i); +} + +function setDelay(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. // **Written answer below*** + console.log(i); + },0); +} + +//The code fix was the result of some googling. +// Looking this over again and again didn't make much sense as to why this was not working as intended +//After reading the explanation- it all became apparent. +//The explanation is as follows: +/* We have to arrange a distinct copy of 'i' to be present for each of the timeout functions. +If we don't do something like this, then each of the timer handler functions will share the same variable i. +*/ + +/* My take away from the above is this: We need to make a call back function in order to separate the 'i' values +so the 'i' values don't overwrite each other and make a grand sum to print 10 times. When making a call back function, +we allow the 'i' value to be separated by the loop which in turn allows the loop to properly iterate through the numbers. */ + +//To be completely honest alot of this stuff is really foggy at the moment and I have a fingertip grasp on much of these concepts +//so my explanation can be completely off- trying my hardest, feel like a dumby, but pushing through in the hopes that this all makes +//sense soon. \ No newline at end of file From fe815c807ab2d9812c81aed081833de5674e44c7 Mon Sep 17 00:00:00 2001 From: Raymond Date: Thu, 17 Aug 2017 12:43:16 -0400 Subject: [PATCH 05/11] finish work --- .../largestPrimePalindrome.js | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/largestPrimePalindrome/largestPrimePalindrome.js b/largestPrimePalindrome/largestPrimePalindrome.js index 4cc99c0..1787b15 100644 --- a/largestPrimePalindrome/largestPrimePalindrome.js +++ b/largestPrimePalindrome/largestPrimePalindrome.js @@ -3,4 +3,21 @@ * 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 checkForPrimePalindrome = (num) => { + num = num + ''; + let reverseNum = num.split("").reverse().join(""); + if(reverseNum === num){ + if(reverseNum % num === 0){ + return true; + } else if (reverseNum === 0 || reverseNum === 1 ) { + return false; + } + } + return false; +} + + +console.log(checkForPrimePalindrome(929)); \ No newline at end of file From 3dd08568db282f89630df167ea192c228053b815 Mon Sep 17 00:00:00 2001 From: Raymond Date: Fri, 18 Aug 2017 12:44:16 -0400 Subject: [PATCH 06/11] complete challenge --- constructors/constructors.js | 112 ++++++++++++++++++++++++++++++++++- 1 file changed, 111 insertions(+), 1 deletion(-) diff --git a/constructors/constructors.js b/constructors/constructors.js index 54801f6..90f44a1 100644 --- a/constructors/constructors.js +++ b/constructors/constructors.js @@ -20,4 +20,114 @@ * * 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(options) { + this.hp = options.hp; + this.strength = options.strength; + this.agility = options.agility; + this.intellect = options.intellect; + } +} +class Humanoid extends NPC { + constructor(options) { + super(options); + + } +} +class Animal extends NPC { + constructor(options) { + super(options); + } +} +class Plant extends NPC { + constructor(options) { + super(options); + } +} +class Human extends Humanoid { + constructor(options) { + super(options); + this.name = options.name; + this.hairColor = options.hairColor; + this.skinColor = options.skinColor; + this.facialHair = options.facialHair; + this.height = options.height; + } +} +class Elf extends Humanoid { + constructor(options) { + super(options); + this.name = options.name; + this.hairColor = options.hairColor; + this.skinColor = options.skinColor; + this.height = options.height; + } +} +class Orc extends Humanoid { + constructor(options) { + super(options); + this.name = options.name; + this.hairColor = options.hairColor; + this.skinColor = options.skinColor; + this.scars = options.scars; + this.facialHair = options.facialHair; + this.height = options.height; + } +} +class Bear extends Animal { + constructor(options) { + super(options); + this.typeOfBear = options.typeOfBear; + this.size = options.size; + this.ferocity = options.ferocity; + } + +} +class Wolf extends Animal { + constructor(options) { + super(options); + this.typeOfWolf = options.typeOfWolf; + this.size = options.size; + this.ferocity = options.ferocity; + + } +} +class FleshEatingdaisy extends Plant { + constructor(options) { + super(options); + this.size = options.size; + this.color = options.color; + } +} +class Solider extends Human { + constructor(options) { + super(options); + this.tacticalMind = options.tacticalMind; + this.weaponMastery = options.weaponMastery; + this.willToLive = options.willToLive; + } +} +class Peasant extends Human { + constructor(options) { + super(options); + this.workEthic = options.workEthic; + this.barteringSkill = options.barteringSkill; + this.woodWorking = options.woodWorking; + this.fishing = options.fishing; + this.hunting = options.hunting; + this.blacksmithing = options.blacksmithing; + } +} +class Bandit extends Human { + constructor(options) { + super(options); + this.guile = options.guile; + this.archery = options.archery; + this.speed = options.speed; + this.pickPocket = options.pickPocket; + this.traps = options.traps; + this.morality = options.morality; + } +} \ No newline at end of file From 11f6678f83585db989feac73db498b5fef7634b1 Mon Sep 17 00:00:00 2001 From: Raymond Date: Mon, 21 Aug 2017 12:26:56 -0400 Subject: [PATCH 07/11] complete challenge --- commonCharacters/commonCharacters.js | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 commonCharacters/commonCharacters.js diff --git a/commonCharacters/commonCharacters.js b/commonCharacters/commonCharacters.js new file mode 100644 index 0000000..625bea9 --- /dev/null +++ b/commonCharacters/commonCharacters.js @@ -0,0 +1,21 @@ +/* + * 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. + * Do not return duplicate characters and ignore whitespace in your returned string. * + * Example: commonCharacters('acexivou', 'aegihobu') * + * Returns: 'aeiou' +*/ +const commonChars = (string1, string2) => { + let combinedStr = ''; + for(var i = 0; i < string1.length; i++){ + for(var j = 0; j < string2.length; j++){ + if(string1[i] === string2[j]){ + combinedStr += string1[i]; + } + } + } + return combinedStr; + } + + console.log(commonChars('acexivou', 'aegihobu')); \ No newline at end of file From f9085448aebd12860e1fdb489b1bf14b49126b93 Mon Sep 17 00:00:00 2001 From: Raymond Date: Mon, 21 Aug 2017 12:46:04 -0400 Subject: [PATCH 08/11] refactored for es6 --- commonCharacters/commonCharacters.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/commonCharacters/commonCharacters.js b/commonCharacters/commonCharacters.js index 625bea9..92f90c4 100644 --- a/commonCharacters/commonCharacters.js +++ b/commonCharacters/commonCharacters.js @@ -8,8 +8,8 @@ */ const commonChars = (string1, string2) => { let combinedStr = ''; - for(var i = 0; i < string1.length; i++){ - for(var j = 0; j < string2.length; j++){ + for(let i = 0; i < string1.length; i++){ + for(let j = 0; j < string2.length; j++){ if(string1[i] === string2[j]){ combinedStr += string1[i]; } From e26bb60f4b7d121ec677d29de3b5833ef6c4874d Mon Sep 17 00:00:00 2001 From: Raymond Date: Tue, 22 Aug 2017 12:00:06 -0400 Subject: [PATCH 09/11] commit --- commonCharacters/commonCharacters.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/commonCharacters/commonCharacters.js b/commonCharacters/commonCharacters.js index 92f90c4..1a503f0 100644 --- a/commonCharacters/commonCharacters.js +++ b/commonCharacters/commonCharacters.js @@ -18,4 +18,4 @@ const commonChars = (string1, string2) => { return combinedStr; } - console.log(commonChars('acexivou', 'aegihobu')); \ No newline at end of file + console.log(commonChars('acexivou', 'aegihobu'));`` \ No newline at end of file From 81e2fe461b6d5ceaf7933203b4d4925b37fa4803 Mon Sep 17 00:00:00 2001 From: Raymond Date: Thu, 24 Aug 2017 12:04:43 -0400 Subject: [PATCH 10/11] finish challenge --- vowelCount/vowelCount.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/vowelCount/vowelCount.js b/vowelCount/vowelCount.js index 27fad79..2008727 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 = (string) => { + let vowels = ['a', 'e', 'i', 'o', 'u']; + let count = 0; + for (let i = 0; i < string.length; i++){ + for(let j = 0; j < vowels.length; j++){ + if(string[i] === vowels[j]){ + count++; + } + } + } + return count; +} + +console.log(vowelCount('Hello World!')); \ No newline at end of file From 6716de85952d592321025d6a484b543681c59a2c Mon Sep 17 00:00:00 2001 From: Raymond Date: Thu, 24 Aug 2017 21:11:14 -0400 Subject: [PATCH 11/11] fix bug regarding last vowel in word --- vowelCount/vowelCount.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/vowelCount/vowelCount.js b/vowelCount/vowelCount.js index 2008727..c12135f 100644 --- a/vowelCount/vowelCount.js +++ b/vowelCount/vowelCount.js @@ -5,8 +5,8 @@ const vowelCount = (string) => { let vowels = ['a', 'e', 'i', 'o', 'u']; let count = 0; - for (let i = 0; i < string.length; i++){ - for(let j = 0; j < vowels.length; j++){ + for (let i = 0; i <= string.length; i++){ + for(let j = 0; j <= vowels.length; j++){ if(string[i] === vowels[j]){ count++; }