Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
bef6b1d
Finished longestString
amoscato Aug 8, 2017
eb0013f
Fixed initialization
amoscato Aug 8, 2017
fd0a9c5
Merge branch 'master' of https://github.com/LambdaSchool/CS2-Code-Cha…
amoscato Aug 9, 2017
365d196
Finished reverseCase
amoscato Aug 9, 2017
e397aa8
Merge branch 'master' of https://github.com/LambdaSchool/CS2-Code-Cha…
amoscato Aug 10, 2017
fff3021
Finished isUnique;js
amoscato Aug 10, 2017
a55718b
Merge branch 'master' of https://github.com/LambdaSchool/CS2-Code-Cha…
amoscato Aug 11, 2017
3011f3a
Finished callBackPractice.js
amoscato Aug 11, 2017
8334e4d
Merge branch 'master' of https://github.com/LambdaSchool/CS2-Code-Cha…
amoscato Aug 14, 2017
cea8c87
Finished removeDuplicates
amoscato Aug 14, 2017
5985faa
Merge branch 'master' of https://github.com/LambdaSchool/CS2-Code-Cha…
amoscato Aug 15, 2017
0c72619
Finished waterJugs.md
amoscato Aug 15, 2017
fe53c8a
Added 'beast mode' to removeDuplicates.js
amoscato Aug 15, 2017
4a3f605
Merge branch 'master' of https://github.com/LambdaSchool/CS2-Code-Cha…
amoscato Aug 16, 2017
5c6c014
Finished forLoopTimeout
amoscato Aug 16, 2017
b724543
Merge branch 'master' of https://github.com/LambdaSchool/CS2-Code-Cha…
amoscato Aug 17, 2017
e84206d
Merge branch 'master' of https://github.com/LambdaSchool/CS2-Code-Cha…
amoscato Aug 21, 2017
a5064f3
Merge branch 'master' of https://github.com/LambdaSchool/CS2-Code-Cha…
amoscato Aug 22, 2017
f312172
Finished
amoscato Aug 23, 2017
37f622e
Merge branch 'master' of https://github.com/LambdaSchool/CS2-Code-Cha…
amoscato Aug 23, 2017
5ecf015
Merge branch 'master' of https://github.com/LambdaSchool/CS2-Code-Cha…
amoscato Aug 24, 2017
1f8f48b
Merge branch 'master' of https://github.com/LambdaSchool/CS2-Code-Cha…
amoscato Aug 25, 2017
9d51407
Finished Challenges
amoscato Nov 7, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions brainTeasers/waterJugs.md
Original file line number Diff line number Diff line change
@@ -1 +1,12 @@
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 Quart | 3 Quart
------------------
0 | 3
3 | 0
3 | 3
5 | 1
0 | 1
1 | 0
1 | 3
4 | 0
32 changes: 32 additions & 0 deletions callBackPractice/callBackPractice.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,51 +20,83 @@

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}.`);
});

// 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) => {
for (let i = 0; i < arr.length; i++) {
if (arr[i] === item);
return cb(true);
}
return cb(false);
};

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 newArr = [];
for (let i = 0; i < arr.length; i++) {
if (!(newArr.includes(arr[i]))) {
newArr.push(arr[i]);
}
}
cb(newArr);
};

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}.`);
Expand Down
13 changes: 13 additions & 0 deletions commonCharacters/commonCharacters.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,16 @@
* Example: commonCharacters('acexivou', 'aegihobu') *
* Returns: 'aeiou'
*/

const commonCharacters = (str1, str2) => {
let common = [];

str1.split('').forEach((char) => {
if (str2.split('').includes(char) && !common.includes(char)) {
common.push(char);
}
})
return common.join('').replace(' ', '');
}

console.log(commonCharacters('abcdefg', 'bcd g'));
19 changes: 16 additions & 3 deletions evenOccurences/evenOccurences.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,19 @@
* * console.log(onlyEven); // 4
* */

const evenOccurence = (arr) => {
// Your code here.
};
function evenOccurrence (arr) {
var storage = {};

arr.forEach((item) => {
storage[item] = storage[item] + 1 || 1;
});

for (var i = 0; i < arr.length; i++) {
if (storage[arr[i]] % 2 === 0) {
return current;
}
}
return null;
}

console.log(evenOccurrence([1, 7, 2, 4, 5, 1, 6, 8, 9, 6, 4, 1]));
8 changes: 8 additions & 0 deletions forLoopTimeout/forLoopTimeout.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,12 @@ for (var i = 1; i <= 10; i++) {
// It doesn't. Why? How can you make it print 1 - 10.
console.log(i);
}, 0);
}

// Closure issue. Var is globally scoped, change to let.

for (let i = 1; i <= 10; i++) {
setTimeout(function() {
console.log(i);
}, 0);
}
12 changes: 11 additions & 1 deletion isUnique/isUnique.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
/* Implement an algorithm to determine if a string has all unique characters.
* What if you cannot use additional data structures?
*/
*/
const isUnique = (str) => {
const uniqueStr = new Set();
for (let i = 0; i < str.length; i++) {
if (uniqueStr.has(str[i])) {
return false;
}
uniqueStr.add(str[i]);
}
return true;
}
21 changes: 20 additions & 1 deletion largestPrimePalindrome/largestPrimePalindrome.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,23 @@
* 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.
*/
*/

const largestPrimePalindrome = () => {
const isPalindrome = (num) => {
if (num === num.toString().split('').reverse().join('')) return true;
}

const isPrime = (num) => {
if (num % 2 !== 0) {
for (let j = 3; j <= Math.sqrt(num); j++) {
if (num % j === 0) return false;
}
return true;
}
}

for (let i = 1000; i > 0; i--) {
if (isPalindrome(i) && isPrime(i)) return i;
}
}
9 changes: 9 additions & 0 deletions longestString/longestString.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,12 @@
* Write a function that accepts an array of strings.
* Return the longest string in the array.
*/
const longestString = (arr) => {
let longest = arr[0];
for (let i = 0; i < arr.length; i++) {
if (arr[i].length > longest.length) {
longest = arr[i];
}
}
return longest;
};
24 changes: 22 additions & 2 deletions removeDuplicates/removeDuplicates.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,26 @@
* you're more than likely using a for loop under the hood.
*/

//initial code
const removeDuplicates = (arr) => {
//code here...
};
const newArr = [];
for (let i = 0; i < arr.length; i++) {
if(!(newArr.includes(arr[i]))) {
newArr.push(arr[i]);
}
}
return newArr;
};

//"beast mode"
const removeDuplicates = (arr) => {
const newArr = [];
const obj = {};
for (let i =0; i < arr.length; i++) {
if (!obj[arr[i]]) {
obj[arr[i]] = true;
newArr.push(arr[i]);
}
}
return newArr;
}
14 changes: 13 additions & 1 deletion reverseCase/reverseCase.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
*/

const reverseCase = (string) => {
let newStr = '';
for (let i = 0; i < string.length; i++) {
if (string[i] === string[i].toUpperCase()) {
newStr = newStr.concat(string[i].toLowerCase());
} else {
newStr = newStr.concat(string[i].toUpperCase());
}
}
return newStr;
}
16 changes: 16 additions & 0 deletions stringCompression/stringCompression.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,19 @@
// 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 compressString = (string) => {
let currentChar = string.charAt(0);
let compressedString = '';
let charCount = 0;
for (let i = 0; i < string.length; i++) {
if (string.charAt(i) === currentChar) charCount++;
if (string.charAt(i) !== currentChar || string.charAt(i) === undefined) {
compressedString += currentChar;
compressedString += charCount;
charCount = 1;
currentChar = string.charAt(i);
}
}
return compressedString.length > string.length ? string : compressedString;
}
12 changes: 12 additions & 0 deletions vowelCount/vowelCount.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,15 @@
* 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', 'A', 'E', 'I', 'O', 'U'];
let count = 0;

for (let i = 0; i < str.length; i++) {
if (vowels.indexOf(str[i]) > -1) {
counter++;
}
}
return counter;
};