From 25d73c663513c1b885820262844885286a756f4b Mon Sep 17 00:00:00 2001 From: cmontoya77 Date: Tue, 15 Aug 2017 12:40:57 -0400 Subject: [PATCH 1/3] Completed CS Challenge --- Basic-JavaScript | 1 + LS-Data-Structures-I | 1 + brainTeasers/waterJugs.md | 6 ++++++ callBackPractice/callBackPractice.js | 7 +++++++ isUnique/isUnique.js | 16 +++++++++++++++- longestString/longestString.js | 23 +++++++++++++++++++++++ removeDuplicates/removeDuplicates.js | 13 ++++++++++++- reverseCase/reverseCase.js | 14 +++++++++++++- 8 files changed, 78 insertions(+), 3 deletions(-) create mode 160000 Basic-JavaScript create mode 160000 LS-Data-Structures-I diff --git a/Basic-JavaScript b/Basic-JavaScript new file mode 160000 index 0000000..e3cf824 --- /dev/null +++ b/Basic-JavaScript @@ -0,0 +1 @@ +Subproject commit e3cf824bacb8dafc3347ca08e86762b0751e84c3 diff --git a/LS-Data-Structures-I b/LS-Data-Structures-I new file mode 160000 index 0000000..6dd9343 --- /dev/null +++ b/LS-Data-Structures-I @@ -0,0 +1 @@ +Subproject commit 6dd9343f04697598ab46c1211a1a00098059e1ef diff --git a/brainTeasers/waterJugs.md b/brainTeasers/waterJugs.md index 5276fc0..af351ee 100644 --- a/brainTeasers/waterJugs.md +++ b/brainTeasers/waterJugs.md @@ -1 +1,7 @@ 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. + +1.Fill up the 5 quart jug with the 3 quart jug twice +2.You will have exactly 1 quart left on the second fill +3.Empty the 5 quart jug +4.Use the 1 quart left to measure the 4 quarts + and fill the 5 quart jug with 4 quarts diff --git a/callBackPractice/callBackPractice.js b/callBackPractice/callBackPractice.js index 8f5d9ac..81ef990 100644 --- a/callBackPractice/callBackPractice.js +++ b/callBackPractice/callBackPractice.js @@ -28,24 +28,30 @@ firstItem(foods, (firstItem) => { getLength(foods, (length) => { console.log(`The length of the array is ${length}.`); + cb(food.length); }); // Write a function called last which passes the last item of the array into the callback last(foods, (lastItem) => { console.log(`The last item in the array is ${lastItem}.`); + cb(foods(foods.length -1)); }); // Write a function called sumNums that adds two numbers and passes the result to the callback sumNums(5, 10, (sum) => { + const numSums = x + y; + cb(numSums); console.log(`The sum is ${sum}.`); }); // Write a function called multiplyNums that adds two numbers and passes the result to the callback multiplyNums(5, 10, (product) => { + const numsMulti = x * y; + cb(numsMulti); console.log(`The product is ${product}.`); }); @@ -53,6 +59,7 @@ multiplyNums(5, 10, (product) => { // Pass true to the callback if it is, otherwise pass false contains(foods, 'ribeye', (result) => { +cb(foods.includes(item)); console.log(result ? 'ribeye is in the array' : 'ribeye is not in the array'); }); diff --git a/isUnique/isUnique.js b/isUnique/isUnique.js index 6c9caf5..ab1a75f 100644 --- a/isUnique/isUnique.js +++ b/isUnique/isUnique.js @@ -1,3 +1,17 @@ /* 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 + */ +public static boolean isUniqueChars(String str) { + if (str.length() > 256) { + return false; + } + int checker = 0; + for (int i = 0; i < str.length(); i++) { + int val = str.charAt(i) - 'a'; + if ((checker & (1 << val)) > 0) return false; + checker |= (1 << val); + } + return true; + +//Test +console.log ( str ("abcds")) \ No newline at end of file diff --git a/longestString/longestString.js b/longestString/longestString.js index 35b887c..c902dd0 100644 --- a/longestString/longestString.js +++ b/longestString/longestString.js @@ -2,3 +2,26 @@ * 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 */ + + const longestString= (array) => { + let longest = 0; + for(let i=0; i array[i+1].length ){ + longest = array[i]; + } else { + longest = array[i+1]; + } + return longest; // than do something else +} \ No newline at end of file diff --git a/removeDuplicates/removeDuplicates.js b/removeDuplicates/removeDuplicates.js index 970f719..4e8bd41 100644 --- a/removeDuplicates/removeDuplicates.js +++ b/removeDuplicates/removeDuplicates.js @@ -7,7 +7,18 @@ * So if you're using 'indexOf' 'sort' 'forEach' etc, * you're more than likely using a for loop under the hood. */ +var arr = [1, 1, 1, 2, 2, 3, 4, 5, 5]; +function newNum(arr){ + let newArr = []; + for(var i = 0; i < arr.length; i++){ + if(newArr.indexOf(arr[i]) == -1){ + newArr.push(arr[i]); + } + } + return newArr; +} -const removeDuplicates = (arr) => { +console.log(newNum(arr)); + //code here... }; \ No newline at end of file diff --git a/reverseCase/reverseCase.js b/reverseCase/reverseCase.js index f1051f5..e4eafde 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 convertString = (str); => { + let s = ''; + for (let i=0; i Date: Wed, 16 Aug 2017 12:53:27 -0400 Subject: [PATCH 2/3] CS2 Code Challenge updated --- forLoopTimeout/forLoopTimeout.js | 21 +++++++++++++++++++++ removeDuplicates/removeDuplicates.js | 3 +-- 2 files changed, 22 insertions(+), 2 deletions(-) create mode 100644 forLoopTimeout/forLoopTimeout.js diff --git a/forLoopTimeout/forLoopTimeout.js b/forLoopTimeout/forLoopTimeout.js new file mode 100644 index 0000000..eed00c1 --- /dev/null +++ b/forLoopTimeout/forLoopTimeout.js @@ -0,0 +1,21 @@ +// 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 = 0; 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); +} // You have to include the loop witin a funtion to create a scope +for (var i = 0; i < =10; i++) { + (function (i) { + setTimeout(function () { + console.log(i) + }, 0); + })(i); +} \ No newline at end of file diff --git a/removeDuplicates/removeDuplicates.js b/removeDuplicates/removeDuplicates.js index 4e8bd41..7e4ac62 100644 --- a/removeDuplicates/removeDuplicates.js +++ b/removeDuplicates/removeDuplicates.js @@ -7,7 +7,7 @@ * So if you're using 'indexOf' 'sort' 'forEach' etc, * you're more than likely using a for loop under the hood. */ -var arr = [1, 1, 1, 2, 2, 3, 4, 5, 5]; +var arr = [1, 1, 1, 2, 2, 3, 4, 5, 5, 6]; function newNum(arr){ let newArr = []; for(var i = 0; i < arr.length; i++){ @@ -21,4 +21,3 @@ function newNum(arr){ console.log(newNum(arr)); //code here... -}; \ No newline at end of file From 05e9c921cc7470e9ee2e272601f6d62b6acc75b9 Mon Sep 17 00:00:00 2001 From: cmontoya77 Date: Wed, 23 Aug 2017 12:23:45 -0400 Subject: [PATCH 3/3] Completed --- commonCharacters/commonCharacters.js | 1 + constructors/constructors.js | 9 ++++++++- evenOccurences/evenOccurences.js | 28 ++++++++++++++++++++++++++-- 3 files changed, 35 insertions(+), 3 deletions(-) diff --git a/commonCharacters/commonCharacters.js b/commonCharacters/commonCharacters.js index ec31d82..e26bf1d 100644 --- a/commonCharacters/commonCharacters.js +++ b/commonCharacters/commonCharacters.js @@ -6,3 +6,4 @@ * Example: commonCharacters('acexivou', 'aegihobu') * * Returns: 'aeiou' */ + diff --git a/constructors/constructors.js b/constructors/constructors.js index 54801f6..12475b1 100644 --- a/constructors/constructors.js +++ b/constructors/constructors.js @@ -20,4 +20,11 @@ * * 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(Humanoid, Animal, Plant) { + this.NPC= 'Charles'; + this.strenght = strenght; + this.speed = speed; + + \ No newline at end of file diff --git a/evenOccurences/evenOccurences.js b/evenOccurences/evenOccurences.js index 35da569..bcfce3a 100644 --- a/evenOccurences/evenOccurences.js +++ b/evenOccurences/evenOccurences.js @@ -10,6 +10,30 @@ * * console.log(onlyEven); // 4 * */ -const evenOccurence = (arr) => { +//const evenOccurence = (arr) => { // Your code here. -}; +//}; + + +// Prompt: Find the first item that occurs an even number of times in an array. Remember to handle multiple even-occurrence items and return the first one. Return null if there are no even-occurrence items. + +function evenOccurrence (array)=> { + // Store counts + var storage = {}; + + // Store each value within the storage object to keep count + array.forEach(function(value, index) { + storage[value] = storage[value] + 1 || 1; + }); + + // loop through array to find first occurence of an even count + for (var i = 0; i < array.length; i++) { + var current = arr[i]; + + if (storage[current] % 2 === 0) { + return current; + } + } + // If no even occurrence found, return null + return null; +}