From 9b34384ec4ce146115046d76edcdc56d7f374554 Mon Sep 17 00:00:00 2001 From: Quilty Date: Wed, 9 Aug 2017 09:27:49 -0700 Subject: [PATCH 1/7] Complete reverseCase implementation --- longestString/longestString.js | 38 ++++++++++++++++++++++++++++++++++ reverseCase/reverseCase.js | 21 ++++++++++++++++++- 2 files changed, 58 insertions(+), 1 deletion(-) diff --git a/longestString/longestString.js b/longestString/longestString.js index 35b887c..acff777 100644 --- a/longestString/longestString.js +++ b/longestString/longestString.js @@ -1,4 +1,42 @@ /* * Write a function that accepts an array of strings. * Return the longest string in the array. + * + * must track both the longest length as well as the longest string + * will mapping work? + * + * in: arr of strings out: longest string + * to find the longest string in a list of strings + * take a list of strings, choose first string as being longest(assuming + * that a valid list of strings has been inputted) and then continue to loop through list until + * exhausted + *return longest string + * + * + */ + + + + +const longest = function(arr){ + if(arr.length === 0) return null; + let longestLength = arr[0].length; + let longestString = arr[0]; + arr.forEach((str) => { // whatever...will just use forEach instead of starting at i = 1 using for loop + if(str.length > longestLength){ + longestLength = str.length; + longestString = str; + } + }); + return longestString; +}; + +/* expect longest(['a','aaaaa','aa']) to return 'aaaaa' + * expect longest(['aaaaa','a','aa']) to return 'aaaaa' + * expect longest(['a','aa','aaaaa']) to return 'aaaaa' + * expect longest(['aaaaa','a','aaaaa']) to return 'aaaaa' + * + * */ + +longest(['a','aaaaa','aa']) === 'aaaaa'; diff --git a/reverseCase/reverseCase.js b/reverseCase/reverseCase.js index f1051f5..7f69fcb 100644 --- a/reverseCase/reverseCase.js +++ b/reverseCase/reverseCase.js @@ -2,4 +2,23 @@ * 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 + */ + +/* + * in: string + * out: string with reversed case of input string + * + * create empty string + * for each char + * if current char has lower case + * then change to upper + * else change to lower + * + */ + +const reverseCase = (str) => { + return str.split('').map((ch) => { + if (ch.toUpperCase() === ch) return ch.toLowerCase(); + return ch.toUpperCase(); + }).join(''); +}; From 19f3dc9b957504da580c3aec4349dc3d9b6c32eb Mon Sep 17 00:00:00 2001 From: Choong K Date: Thu, 10 Aug 2017 09:15:02 -0700 Subject: [PATCH 2/7] Complete isUnique without using other data structure --- isUnique/isUnique.js | 26 +++++++++++++++++++++++++- longestString/longestString.js | 2 +- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/isUnique/isUnique.js b/isUnique/isUnique.js index 6c9caf5..9dd2208 100644 --- a/isUnique/isUnique.js +++ b/isUnique/isUnique.js @@ -1,3 +1,27 @@ /* 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 + */ +/* + * i: string + * o: boolean + * + * make new str that contains set of chars + * + * for each char in the str + * if curr str is found in new str + * return false + * add curr char to new str * + * return true + */ + +const isUnique = (str) => { + let included = ''; + for (let i = 0; i < str.length; i++) { + if (included.indexOf(str[i]) !== -1) return false; + included += str[i]; + } + return true; +}; + +console.log(isUnique('aa')); +console.log(isUnique('ab')); diff --git a/longestString/longestString.js b/longestString/longestString.js index acff777..2290622 100644 --- a/longestString/longestString.js +++ b/longestString/longestString.js @@ -18,7 +18,7 @@ -const longest = function(arr){ +const longest = (arr) => { if(arr.length === 0) return null; let longestLength = arr[0].length; let longestString = arr[0]; From 1d5cfa8d8f8c450ca9c3273917d19d912da8d29a Mon Sep 17 00:00:00 2001 From: Choong K Date: Fri, 11 Aug 2017 09:50:27 -0700 Subject: [PATCH 3/7] Finish cb practice --- callBackPractice/callBackPractice.js | 48 +++++++++++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) diff --git a/callBackPractice/callBackPractice.js b/callBackPractice/callBackPractice.js index 8f5d9ac..47d9711 100644 --- a/callBackPractice/callBackPractice.js +++ b/callBackPractice/callBackPractice.js @@ -18,54 +18,100 @@ // Write a function called firstItem that passes the first item of the given array to the callback function +const firstItem = (arr, cb) => { + cb(arr.shift()); +}; + + const foods = ['pineapple', 'mango', 'ribeye', 'curry', 'tacos', 'ribeye', 'mango']; firstItem(foods, (firstItem) => { console.log(`The first item is ${firstItem}.`); + console.log(firstItem === 'pineapple'); }); // 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}.`); + console.log(length === 6); }); // Write a function called last which passes the last item of the array into the callback +const last = (arr, cb) => { + cb(arr.pop()); +} + last(foods, (lastItem) => { console.log(`The last item in the array is ${lastItem}.`); + console.log(lastItem === 'mango'); }); // 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}.`); + console.log(15 === 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}.`); + console.log(product === 50); }); // 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) => { + cb(arr.indexOf(item) !== -1); +}; + contains(foods, 'ribeye', (result) => { console.log(result ? 'ribeye is in the array' : 'ribeye is not in the array'); + console.log(result === true); }); // 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. +var removeDuplicates = (arr, cb) => { + const out = []; + arr.forEach((el) => { + if(!out.includes(el)){ + out.push(el); + } + }); + + cb(out); +} + removeDuplicates(foods, (uniqueFoods) => { console.log(`foods with duplicates removed: ${uniqueFoods}`); + console.log(uniqueFoods.toString() === "mango,ribeye,curry,tacos"); }); // Write a function called forEach that iterates over the provided array and passes the value and index into the callback. +const forEach = (arr, cb) => { + arr.forEach(cb); +}; + forEach(foods, (value, index) => { console.log(`${value} is at index ${index}.`); -}); \ No newline at end of file +}); From e28af5c9df48fcd61171a985a04eb174a5f6c4dc Mon Sep 17 00:00:00 2001 From: Choong K Date: Mon, 14 Aug 2017 09:04:15 -0700 Subject: [PATCH 4/7] Finish removeDuplicates() --- removeDuplicates/removeDuplicates.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/removeDuplicates/removeDuplicates.js b/removeDuplicates/removeDuplicates.js index 970f719..4adc5b8 100644 --- a/removeDuplicates/removeDuplicates.js +++ b/removeDuplicates/removeDuplicates.js @@ -9,5 +9,8 @@ */ const removeDuplicates = (arr) => { + return Array.from(new Set(arr)); //code here... -}; \ No newline at end of file +}; + +console.log(removeDuplicates([1, 1, 1, 2, 2, 3, 4, 5, 5])); \ No newline at end of file From 51622b587ee0a03b3d988f79f9b537bd763e0d1c Mon Sep 17 00:00:00 2001 From: Choong K Date: Tue, 15 Aug 2017 09:13:12 -0700 Subject: [PATCH 5/7] Finish brainTeaser waterJugs --- brainTeasers/waterJugs.md | 19 +++++++++++++++++++ removeDuplicates/removeDuplicates.js | 1 - 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/brainTeasers/waterJugs.md b/brainTeasers/waterJugs.md index 5276fc0..33d1e29 100644 --- a/brainTeasers/waterJugs.md +++ b/brainTeasers/waterJugs.md @@ -1 +1,20 @@ 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 qt +3 qt + +=> 4 qt + +find out where 3 qt mark is on 5 qt jug, which will +let us know where 2 qts is + +fill 3 qt jug +xfer 3 qt water to 5 qt jug +mark 3 qt point on 5 qt jug +fill 5 qt jug +xfer water up to 3 qt mark to 3qt jug (2 qt) +mark 2 qt point on 3 qt jug +empty 5 qt jug +xfer 2 qt of water from 3 qt jug to 5 qt jug +pour 2 qt of water to 3 qt jug +xfer 2 qt of water from 3 qt jug to 5 qt jug \ No newline at end of file diff --git a/removeDuplicates/removeDuplicates.js b/removeDuplicates/removeDuplicates.js index 4adc5b8..ce4adc2 100644 --- a/removeDuplicates/removeDuplicates.js +++ b/removeDuplicates/removeDuplicates.js @@ -13,4 +13,3 @@ const removeDuplicates = (arr) => { //code here... }; -console.log(removeDuplicates([1, 1, 1, 2, 2, 3, 4, 5, 5])); \ No newline at end of file From 7ff13da430c17fcc740cc3e147c2c23486b80032 Mon Sep 17 00:00:00 2001 From: Choong K Date: Wed, 16 Aug 2017 09:24:29 -0700 Subject: [PATCH 6/7] Merge branch 'master' of https://github.com/LambdaSchool/CS2-Code-Challenges Also, finish for loop timeout --- forLoopTimeout/forLoopTimeout.js | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 forLoopTimeout/forLoopTimeout.js diff --git a/forLoopTimeout/forLoopTimeout.js b/forLoopTimeout/forLoopTimeout.js new file mode 100644 index 0000000..9cfa960 --- /dev/null +++ b/forLoopTimeout/forLoopTimeout.js @@ -0,0 +1,17 @@ +// Explain what is wrong with this code and how you would fix this. +// With ES6 there is a very, very simple way to solve this. +// 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); +} + +/* put the for loop in the callback function and wrapping console.log(i) in it. It doesn't work +in its current state as expected because nothing is blocking the incrementing of i from happening and by the time console.log(i) executes, we have already reached the end of the loop +*/ From c10b163ae92c4a331a9e1ed3ca877ef232c21003 Mon Sep 17 00:00:00 2001 From: Choong K Date: Fri, 18 Aug 2017 09:09:21 -0700 Subject: [PATCH 7/7] Finish constructors --- constructors/constructors.js | 64 +++++++++++++++++++++++++++++++++++- 1 file changed, 63 insertions(+), 1 deletion(-) diff --git a/constructors/constructors.js b/constructors/constructors.js index 54801f6..7f0b416 100644 --- a/constructors/constructors.js +++ b/constructors/constructors.js @@ -20,4 +20,66 @@ * * 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(hp, strength, speed){ + this.hp = hp; + this.strength = strength; + this.speed = speed; + } +} + +class Humanoid extends NPC { + constructor(hp, strength, speed){ + super(hp, strength, speed); + } +} + +class Animal extends NPC { + constructor(hp, strength, speed){ + super(hp, strength, speed); + } +} + +class Plant extends NPC { + constructor(hp, strength, speed) { + super(hp, strength, speed); + } +} + +class Humanoid extends Human { + constructor(hp, strength, speed) { + super(hp, strength, speed); + } +} + +class Elf extends Humanoid { + constructor(hp, strength, speed) { + super(hp, strength, speed); + } +} + +class Orc extends Humanoid { + constructor(hp, strength, speed) { + super(hp, strength, speed); + } +} + +class Bear extends Animal { + constructor(hp, strength, speed) { + super(hp, strength, speed); + } +} + +class Wolf extends Animal { + constructor(hp, strength, speed) { + super(hp, strength, speed); + } +} + +class FleshEatingDaisy extends Plant { + constructor(hp, strength, speed) { + super(hp, strength, speed); + } +}