Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
2389e17
longestString challenge complete
sam-crabtree Aug 8, 2017
86f0dbf
reverseCase.js complete
sam-crabtree Aug 9, 2017
0a08c4c
Resolved merge conflicts
sam-crabtree Aug 10, 2017
39ddef0
Merge branch 'master' of https://github.com/LambdaSchool/CS2-Code-Cha…
sam-crabtree Aug 10, 2017
52fbbe2
isUnique complete
sam-crabtree Aug 10, 2017
0a41e44
Merge branch 'master' of https://github.com/LambdaSchool/CS2-Code-Cha…
sam-crabtree Aug 11, 2017
40a26d7
callBackPractice complete
sam-crabtree Aug 11, 2017
32d3597
Redid isUnique due to misunderstanding
sam-crabtree Aug 11, 2017
c9b355a
Merge branch 'master' of https://github.com/LambdaSchool/CS2-Code-Cha…
sam-crabtree Aug 14, 2017
8a529d2
removeDuplicates complete
sam-crabtree Aug 14, 2017
6a1988b
Merge branch 'master' of https://github.com/LambdaSchool/CS2-Code-Cha…
sam-crabtree Aug 15, 2017
ad1992e
waterJugs complete
sam-crabtree Aug 15, 2017
c541ad0
Merge branch 'master' of https://github.com/LambdaSchool/CS2-Code-Cha…
sam-crabtree Aug 16, 2017
42c874c
forLoopTimeout completed
sam-crabtree Aug 16, 2017
dee86a5
Merge branch 'master' of https://github.com/LambdaSchool/CS2-Code-Cha…
sam-crabtree Aug 17, 2017
5708e7f
largestPrimePalindrome complete
sam-crabtree Aug 17, 2017
d89f5d0
Merge branch 'master' of https://github.com/LambdaSchool/CS2-Code-Cha…
sam-crabtree Aug 18, 2017
6471859
Merge branch 'master' of https://github.com/LambdaSchool/CS2-Code-Cha…
sam-crabtree Aug 18, 2017
70d0d09
Constructors complete
sam-crabtree Aug 18, 2017
2cddb18
Merge branch 'master' of https://github.com/LambdaSchool/CS2-Code-Cha…
sam-crabtree Aug 21, 2017
e2f5846
commonCharacters complete
sam-crabtree Aug 21, 2017
406e6d8
Merge branch 'master' of https://github.com/LambdaSchool/CS2-Code-Cha…
sam-crabtree Aug 22, 2017
1d74ed5
evenOccurences complete
sam-crabtree Aug 22, 2017
73b1fcd
Merge branch 'master' of https://github.com/LambdaSchool/CS2-Code-Cha…
sam-crabtree Aug 23, 2017
e864a78
stringCompression complete (for now)
sam-crabtree Aug 23, 2017
7dd077c
Merge branch 'master' of https://github.com/LambdaSchool/CS2-Code-Cha…
sam-crabtree Aug 24, 2017
534e76d
vowelCount complete
sam-crabtree Aug 24, 2017
1c039f0
Merge branch 'master' of https://github.com/LambdaSchool/CS2-Code-Cha…
sam-crabtree Aug 25, 2017
ba5556d
meanMedianMode complete
sam-crabtree 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
9 changes: 9 additions & 0 deletions brainTeasers/waterJugs.md
Original file line number Diff line number Diff line change
@@ -1 +1,10 @@
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 3 quart jug
2) Use the now full 3 quart jug to fill the 5 quart jug, twice
3) Now you're left with 1 quart in the 3 quart jug
4) Empty the 5 quart jug
5) Transfer that 1 quart of water from the 3 qt jug into the now empty 5 quart jug
6) Fill up the 3 quart jug again
7) Dump it into the 5 quart
8) You now have 4 quarts in the 5 qt jug
46 changes: 45 additions & 1 deletion callBackPractice/callBackPractice.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,48 +24,92 @@ firstItem(foods, (firstItem) => {
console.log(`The first item is ${firstItem}.`);
});

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

// Write a function called getLength that passes the length of the array into the callback

getLength(foods, (length) => {
console.log(`The length of the array is ${length}.`);
});

const getLength = (arr, cb) => {
cb(arr.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}.`);
});

const last = (arr, cb) => {
cb(arr[arr.length - 1]);
}

// Write a function called sumNums that adds two numbers and passes the result to the callback


sumNums(5, 10, (sum) => {
console.log(`The sum is ${sum}.`);
});

const sumNums = (num1, num2, cb) => {
cb(num1 + num2);
};

// Write a function called multiplyNums that adds two numbers and passes the result to the callback

multiplyNums(5, 10, (product) => {
console.log(`The product is ${product}.`);
});

const multiplyNums = (num1, num2, cb) => {
cb(num1 * num2);
};
// 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

contains(foods, 'ribeye', (result) => {
console.log(result ? 'ribeye is in the array' : 'ribeye is not in the array');
});

const contains = (arr, item, cb) => {
if (arr.includes(item)) {
cb(true);
} else {
cb(false);
}
};

// 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.

removeDuplicates(foods, (uniqueFoods) => {
console.log(`foods with duplicates removed: ${uniqueFoods}`);
});

const removeDuplicates = (arr, cb) => {
const unique = [];

arr.forEach((value) => {
if (!arr.includes(value)) {
arr.push(value);
}
});
cb(unique);
};

// Write a function called forEach that iterates over the provided array and passes the value and index into the callback.


forEach(foods, (value, index) => {
console.log(`${value} is at index ${index}.`);
});
});

const forEach = (arr, cb) => {
for (let i = 0; i < arr.length; i++) {
cb(arr[i], i);
}
};
18 changes: 16 additions & 2 deletions commonCharacters/commonCharacters.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,22 @@
/*
* 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.
* 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 commonCharacters = function(string1, string2) {
let sameChars = '';
for (let i = 0; i < string2.length; i++) {
if (string1.includes(string2[i])) {
sameChars += string2[i];
}
}
return `Identical characters: ${sameChars}`;
};

console.log(commonCharacters('acexivou', 'aegihobu'));
93 changes: 92 additions & 1 deletion constructors/constructors.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,95 @@
*
* This is how you would structure the game objects in an actual game
* application in Unity or another similar framework.
*/
*/

class NPC {
constructor(stats) {
this.hp = stats.hp;
this.strength = stats.strength;
this.speed = stats.speed;
this.luck = stats.luck;
this.dexterity = stats.dexterity;
this.defense = stats.defense;
}
}

class Humanoid extends NPC {
constructor(stats) {
super(stats);
this.weapon = stats.weapon;
this.armor = stats.armor;
this.inventory = stats.inventory;
}
}

class Human extends Humanoid {
constructor(stats) {
super(stats);
this.defenseBoost = stats.defense + 25;
}
}

class Soldier extends Human {
constructor(stats) {
super(stats);
}
}

class Peasant extends Human {
constructor(stats) {
super(stats);
}
}

class Bandit extends Human {
constructor(stats) {
super(stats);
}
}

class Elf extends Humanoid {
constructor(stats) {
super(stats);
this.dexterityBoost = stats.dexterity + 25;
}
}

class Orc extends Humanoid {
constructor(stats) {
super(stats);
this.StrengthBoost = stats.strength + 25;
}
}

class Animal extends NPC {
constructor(stats) {
super(stats);
this.hostility = stats.hostility;
}
}

class Bear extends Animal {
constructor(stats) {
super(stats);
}
}

class Wolf extends Animal {
constructor(stats) {
super(stats);
}
}

class Plant extends NPC {
constructor(stats) {
super(stats);
this.magicProperties = stats.magicProperties;
}
}

class FleshEatingDaisy extends Plant {
constructor(stats) {
super(stats);
}
}
18 changes: 17 additions & 1 deletion evenOccurences/evenOccurences.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,22 @@
* * console.log(onlyEven); // 4
* */


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

arr.forEach(function(value, index) {
storage[value] = storage[value] + 1 || 1;
});

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

return null;
};

const nums = [1, 2, 2, 3, 4, 4, 4, 5];
console.log(evenOccurence(nums));
27 changes: 21 additions & 6 deletions forLoopTimeout/forLoopTimeout.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,25 @@
// 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);
// }

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);
}
setTimeout(function(x) {
return function() {
console.log(x);
}; }(i), 0);
}

// We're only passing the refernece to the variable i
// and not the the actual value of i when it goes through each loop
// So by the time the setTimeout function actually runs, the for statement
// has already run and produced the number 11
// Needed to pass the actual value of i at each iteration, so
// another anon function was needed as a wrapper
// Outer function executes first, and takes in a private variable x, which we pass i into
27 changes: 26 additions & 1 deletion isUnique/isUnique.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,28 @@
/* Implement an algorithm to determine if a string has all unique characters.
* What if you cannot use additional data structures?
*/
*/

// const findUnique = (str) => {
// let unique='';

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

// console.log(findUnique('This is a string'));

// ^^^ old code because i misunderstood the assignment, lol


const isUnique = (str) => {
for (let i = 0; i < str.length; i++) {
for (let j = i +1; j < str.length; j++) {
if (str[i] == str[j]) return false;
}
}
return true;
};
31 changes: 30 additions & 1 deletion largestPrimePalindrome/largestPrimePalindrome.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,33 @@
* 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 isPalindrome = (num) => {
const num2Str = num.toString();

if (num2Str === num2Str.split('').reverse().join('')) return true;
return false;
}

const isPrime = (num) => {
if (num === 1 || num === 3) return false;
for (let i = 2; i < num; i++) {
if (num % i === 0) return false;
}
return true;
}

const lPPArray = [];

const largestPrimePalindrome = (num) => {
for (let i = 2; i < 1000; i++) {
if (isPalindrome(i) && isPrime(i)) {
lPPArray.push(i);
}
}
return lPPArray[lPPArray.length - 1];
}

console.log(largestPrimePalindrome());
29 changes: 28 additions & 1 deletion longestString/longestString.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,31 @@
/*
/*
* 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
*/

function findLongestWord(array) {
let longestWord = '';

array.forEach(word => {
if(word.length > longestString.length) {
longestWord = word;
}
});

return longestWord;
}

const word = findLongestWord(['Another', 'one', 'bites', 'the', 'dust']);
console.log(word);
Loading