Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
87000a3
Completed
frankfaustino Aug 8, 2017
c9322bb
Completed
frankfaustino Aug 8, 2017
ff97901
Completed
frankfaustino Aug 8, 2017
b5c23a1
Merge branch 'master' of https://github.com/LambdaSchool/CS2-Code-Cha…
frankfaustino Aug 9, 2017
c80df55
Completed reverseCase.js
frankfaustino Aug 9, 2017
14f9a2b
Completed reverseCase.js
frankfaustino Aug 9, 2017
a27f2f9
Completed reverseCase.js
frankfaustino Aug 9, 2017
d9c59ac
Completed reverseCase.js
frankfaustino Aug 9, 2017
f0528dd
Merge branch 'master' of https://github.com/LambdaSchool/CS2-Code-Cha…
frankfaustino Aug 10, 2017
246bfc3
Completed isUnique.js
frankfaustino Aug 10, 2017
eb80a8f
Completed isUnique.js
frankfaustino Aug 10, 2017
8c2c4d0
Completed isUnique.js
frankfaustino Aug 10, 2017
ac6497f
Completed isUnique.js
frankfaustino Aug 10, 2017
bce446b
Merge branch 'master' of https://github.com/LambdaSchool/CS2-Code-Cha…
frankfaustino Aug 11, 2017
bd705c3
Completed callBackPractice
frankfaustino Aug 11, 2017
c978e40
Merge branch 'master' of https://github.com/LambdaSchool/CS2-Code-Cha…
Aug 14, 2017
a47cf9a
Completed removeDuplicates.js
Aug 14, 2017
1aacd4c
Completed removeDuplicates.js
Aug 14, 2017
2de9600
Merge branch 'master' of https://github.com/LambdaSchool/CS2-Code-Cha…
Aug 15, 2017
f0c6e02
Completed waterJugs.md
Aug 15, 2017
08c93f6
Completed waterJugs.md
Aug 15, 2017
77a3bfd
Completed waterJugs.md
Aug 15, 2017
82d431a
Merge branch 'master' of https://github.com/LambdaSchool/CS2-Code-Cha…
Aug 16, 2017
550f803
Completed forLoopTimeout.js
Aug 16, 2017
dfdff89
Completed forLoopTimeout.js
Aug 16, 2017
874b5ee
Completed forLoopTimeout.js
Aug 16, 2017
e0fb5ad
Completed forLoopTimeout.js
Aug 16, 2017
03cec3e
Merge branch 'master' of https://github.com/LambdaSchool/CS2-Code-Cha…
Aug 17, 2017
e0b2ac1
Completed largestPrimePalindrome.js
frankfaustino Aug 18, 2017
ac9f2c8
Completed largestPrimePalindrome.js
frankfaustino Aug 18, 2017
9f4190f
Merge branch 'master' of https://github.com/LambdaSchool/CS2-Code-Cha…
frankfaustino Aug 18, 2017
07346e0
Completed constructors.js
frankfaustino Aug 21, 2017
c35ab7e
Merge branch 'master' of https://github.com/LambdaSchool/CS2-Code-Cha…
frankfaustino Aug 21, 2017
3bbb0c0
Merge branch 'master' of https://github.com/LambdaSchool/CS2-Code-Cha…
frankfaustino Aug 22, 2017
7f3fb3b
Completed evenOccurences.js
frankfaustino Aug 23, 2017
cbd8df2
Completed evenOccurences.js
frankfaustino Aug 23, 2017
8ddaf17
Completed evenOccurences.js
frankfaustino Aug 23, 2017
4298d68
Completed evenOccurences.js
frankfaustino Aug 23, 2017
2cb8696
Completed stringCompression.js
frankfaustino Aug 23, 2017
c560f28
Merge branch 'master' of https://github.com/LambdaSchool/CS2-Code-Cha…
frankfaustino Aug 24, 2017
aa8af7e
Completed stringCompression.js / Added check for 'compressed' string.…
frankfaustino Aug 24, 2017
832a0c9
Completed vowelCount.js
frankfaustino Aug 25, 2017
9609043
Completed vowelCount.js
frankfaustino Aug 25, 2017
275a2b3
Completed vowelCount.js
frankfaustino Aug 25, 2017
3af821d
Completed vowelCount.js
frankfaustino Aug 25, 2017
79a26d6
Completed meanMedianMode.js
frankfaustino Aug 25, 2017
3706c3f
Completed meanMedianMode.js
frankfaustino Aug 25, 2017
465aaf3
Completed meanMedianMode.js
frankfaustino Aug 25, 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
12 changes: 12 additions & 0 deletions brainTeasers/waterJugs.md
Original file line number Diff line number Diff line change
@@ -1 +1,13 @@
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 jug │ 3 qt jug
─────────────┼────────────
0 │ 3 fill up 3 quart jug
3 │ 0 transfer 3 quarts to the 5 quart jug
3 │ 3 fill up 3 quart jug again
5 │ 1 fill up the 5 quart jug from the 3 quart jug with 1 quart remaining in the 3 quart jug
0 │ 1 empty the 5 quart jug
1 │ 0 transfer 1 quart from the 3 quart jug to the 5 quart jug
1 │ 3 fill up the 3 quart jug
4 │ 0 transfer the 3 quart jug to the 5 quart jug to get the desired 4 quarts of water
45 changes: 31 additions & 14 deletions callBackPractice/callBackPractice.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,57 +15,74 @@
*
*/


// 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'];

firstItem(foods, (firstItem) => {
const firstItem = (foods, cb) => cb(foods[0]);

const foods = [
'pineapple',
'mango',
'ribeye',
'curry',
'tacos',
'ribeye',
'mango'
];

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 = (length, cb) => cb(foods.length);

getLength(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) => {
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 = (a, b, cb) => cb(a + b);


sumNums(5, 10, (sum) => {
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 = (a, b, cb) => cb(a * b);

multiplyNums(5, 10, (product) => {
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 = (foods, i, cb) => cb(foods.indexOf(i) > -1);

contains(foods, 'ribeye', (result) => {
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 = (foods, cb) => cb(Array.from(new Set(foods)));

removeDuplicates(foods, (uniqueFoods) => {
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) => {
console.log(`${value} is at index ${index}.`);
});
});
129 changes: 127 additions & 2 deletions constructors/constructors.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* Human -> Soldier, Peasant, Bandit
*
* NPC should be a general class for a non-player character in the game.
* This class will probably include general attributes like hp, strength, speed, etc.
* This class will probably include general attributes like hp, str, speed, etc.
*
* Humanoid, Animal, and Plant should all inherit from NPC. The classes
* shown to the right of the arrow are classes that will inherit
Expand All @@ -20,4 +20,129 @@
*
* This is how you would structure the game objects in an actual game
* application in Unity or another similar framework.
*/
*/

class NPC {
constructor(options) {
this.con = options.con;
this.str = options.str;
this.dex = options.dex;
this.int = options.int;
this.wis = options.wis;
this.cha = options.cha;
}
}

// Humanoid
// ↳ Human
// ↳ Fighter → Thief → Wizard
// ↳ Elf
// ↳ Ranger → Rogue → Druid
// ↳ Orc
// ↳ Barbarian → Warlock → Shaman
class Humanoid extends NPC {
constructor(options) {
super(options);
}
}

class Human extends Humanoid {
constructor(options) {
super(options);
this.name = options.name;
}
}

class Fighter extends Human {
constructor(options) {
super(options);
}
}

class Thief extends Human {
constructor(options) {
super(options);
}
}

class Wizard extends Human {
constructor(options) {
super(options);
this.abilities = ['Magic Missile', 'Shock Pulse', 'Spectral Blade', 'Electrocute'];
}
}
class Elf extends NPC {
constructor(options) {
super(options);
}
}

class Ranger extends Elf {
constructor(options) {
super(options);
}
}

class Rogue extends Elf {
constructor(options) {
super(options);
}
}

class Druid extends Elf {
constructor(options) {
super(options);
}
}

class Orc extends NPC {
constructor(options) {
super(options);
}
}

class Barbarian extends Orc {
constructor(options) {
super(options);
}
}

class Warlock extends Orc {
constructor(options) {
super(options);
}
}

class Shaman extends Orc {
constructor(options) {
super(options);
}
}
// Animal
// ↳ Tiny
// ↳ Rat → Lizard → Hawk
// ↳ Large
// ↳ Brown Bear → Dire Wolf → Horse
// ↳ Huge
// ↳ Giant Octopus → Nemean Lion → Roc
class Animal extends NPC {
constructor(options) {
super(options);
}
}

// Monster
// ↳ Dragon
// ↳ Adult Dragon → Wyrm → Faerie Dragon
// ↳ Giant
// ↳ Ogre → Ettin → Cyclops
// ↳ Undead
// ↳ Lich → Zombie → Ghoul
class Monster extends NPC {
constructor(options) {
super(options);
}
}

var frank = new Wizard({ name: 'Frank', con: 4, str: 3, dex: 3, int: 10, wis: 10, cha: 8 });
console.log(frank);
50 changes: 37 additions & 13 deletions evenOccurences/evenOccurences.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,39 @@
/*
* * Find the first item that occurs an even number of times in an array.
* * Remember to handle multiple even-occurance items and return the first one.
* * Return null if there are no even-occurance items.
* */

/*
* * example usage:
* * const onlyEven = evenOccurance([1, 7, 2, 4, 5, 1, 6, 8, 9, 6, 4, 1]);
* * console.log(onlyEven); // 4
* */
/* Instructions
* Find the first item that occurs an even number of times in an array.
* Remember to handle multiple even-occurance items and return the first one.
* Return null if there are no even-occurance items.
*/

// Test Cases
const onlyEven = [1, 7, 2, 4, 5, 1, 6, 8, 9, 6, 4, 1]; // 4

// Solution #1
const evenOccurence = (arr) => {
// Your code here.
};
let a = [...new Set(arr)],
b = 0;
for (i in a) {
b = 0;
for (j in arr) {
if (a[i] === arr[j]) {
b++;
}
}
if (b % 2 === 0) return a[i];
}
return null;
}

// Solution #2
// const evenOccurence = arr => {
// let a = {};
// arr.forEach((i) => {
// if (a[i]) a[i] += 1;
// else a[i] = 1;
// })
// for (let k in a) {
// if (a[k] % 2 === 0) return Number(k);
// }
// return null;
// };

console.log(evenOccurence(onlyEven));
30 changes: 25 additions & 5 deletions forLoopTimeout/forLoopTimeout.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,33 @@
// 1. Instructions
// 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.

// 2a. Solution: ES6 fix. let instead of var encloses the scope of i to the for loop
// for (let i = 1; i <= 10; i++) {
// setTimeout(() => console.log(i), 0);
// }

// 2b. Solution: Passing a new copy of i with each iteration to setTimeout function callback
// function x(i) {
// setTimeout(() => console.log(i), 0)
// }
// for (var i = 1; i <= 10; i++) {
// x(i);
// }

// 2c. Solution: New scope for each iteration using IIFE, setTimeout function callback
// provided a variable j that evaluates to i within the scope of for loop
// for (var i = 1; i <= 10; i++) {
// (() => {
// var j = i;
// setTimeout(() => console.log(j), 0);
// })();
// }

// 2d. Solution: New scope for each iteration using IIFE (passing i as an argument)
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);
((i) => setTimeout(() => console.log(i), 0))(i);
}
42 changes: 40 additions & 2 deletions isUnique/isUnique.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,41 @@
/* Implement an algorithm to determine if a string has all unique characters.
/* 0. Instructions
* Implement an algorithm to determine if a string has all unique characters.
* What if you cannot use additional data structures?
*/
*/

// 1. Identify Test Cases
// const a = 'Bacon ipsum dolor amet spare ribs drumstick strip steak.' // false
// const b = 'HeLLo WORLd!' // false
// const c = 'bvAlh!(@^$%#)atQBq' // true

/* 2. Pseudo-Code
* function with string param
* initialize empty string
* store unique characters in empty string
* compare new string with original string
* return true if has unique characters, otherwise return false
*/

// 3a. Solution using for loop / indexOf()
const isUnique = str => {
let x = '';

for (let i = 0; i < str.length; i++) {
if (x.indexOf(str[i]) === -1) {
x += str[i];
}
}
console.log(x === str); // return true if string contains unique characters
};

// 3b. Solution using an Array.from() / new Set() / .join()
const isUnique = str => {
const x = Array.from(new Set(str)).join('');
console.log(x === str); // return true if string contains unique characters
};

// 3c. Solution using spread operator / new Set() / .join()
const isUnique = str => {
const x = [...new Set(str)].join('');
console.log(x === str); // return true if string contains unique characters
};
Loading