Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
1676b00
Completed longestString challenge
Mjax511 Aug 8, 2017
ece4ddf
Merge branch 'master' of https://github.com/LambdaSchool/CS2-Code-Cha…
Mjax511 Aug 9, 2017
79267cd
complete reverseCase.js
Mjax511 Aug 9, 2017
10e809f
Merge branch 'master' of https://github.com/LambdaSchool/CS2-Code-Cha…
Mjax511 Aug 10, 2017
4ebb667
complete isUnique challenge
Mjax511 Aug 10, 2017
e081df9
Merge branch 'master' of https://github.com/LambdaSchool/CS2-Code-Cha…
Mjax511 Aug 11, 2017
6fb695c
complete callback practice
Mjax511 Aug 11, 2017
ef87c0c
Merge branch 'master' of https://github.com/LambdaSchool/CS2-Code-Cha…
Mjax511 Aug 14, 2017
994c933
complete removeDuplicates
Mjax511 Aug 14, 2017
801cc0c
complete beast mode removeDuplicates
Mjax511 Aug 14, 2017
85b4176
Merge branch 'master' of https://github.com/LambdaSchool/CS2-Code-Cha…
Mjax511 Aug 15, 2017
29af134
water jugs solution
Mjax511 Aug 15, 2017
f43d751
updated water jugs
Mjax511 Aug 15, 2017
0082ae2
Merge branch 'master' of https://github.com/LambdaSchool/CS2-Code-Cha…
Mjax511 Aug 16, 2017
03b53ab
create closure for forLoopTimeout
Mjax511 Aug 16, 2017
02a2288
Merge branch 'master' of https://github.com/LambdaSchool/CS2-Code-Cha…
Mjax511 Aug 17, 2017
f7242cb
longest palindrome complete
Mjax511 Aug 17, 2017
0905c94
Merge branch 'master' of https://github.com/LambdaSchool/CS2-Code-Cha…
Mjax511 Aug 18, 2017
017d041
unfinished constructors
Mjax511 Aug 18, 2017
f49ce4f
saved file
Mjax511 Aug 18, 2017
f83a0f3
Merge branch 'master' of https://github.com/LambdaSchool/CS2-Code-Cha…
Mjax511 Aug 21, 2017
1a248a9
complete common characters
Mjax511 Aug 21, 2017
15c4a8b
Merge branch 'master' of https://github.com/LambdaSchool/CS2-Code-Cha…
Mjax511 Aug 22, 2017
643918e
complete evenOccurene
Mjax511 Aug 22, 2017
25e4896
?
Mjax511 Aug 23, 2017
abf5d46
solve merge conflict
Mjax511 Aug 23, 2017
f6147a1
complete stringCompression
Mjax511 Aug 23, 2017
9539b5d
Merge branch 'master' of https://github.com/LambdaSchool/CS2-Code-Cha…
Mjax511 Aug 24, 2017
c2105a0
complete vowel count
Mjax511 Aug 24, 2017
d1b1e79
Merge branch 'master' of https://github.com/LambdaSchool/CS2-Code-Cha…
Mjax511 Aug 25, 2017
4ed51d3
complet mean median mode
Mjax511 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
7 changes: 7 additions & 0 deletions brainTeasers/waterJugs.md
Original file line number Diff line number Diff line change
@@ -1 +1,8 @@
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.


fill up the 5 quart jug with water and then fill the 3 quart jug just to the top. The 5 quart jug will be left with 2 quarts. Put the 2 quarts aside and repeat again. Add 2 and 2 together to get 4 quarts.

or

fill the 5 quarts with 3 quarts. repeat but leave 1 quart in the 3 quart jug. empty the 5 quart jug and fill it with the 1 quart of water. fill up the 3 quart jug and add it to the 1 quart to get 4 quarts.
40 changes: 32 additions & 8 deletions callBackPractice/callBackPractice.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,54 +17,78 @@


// Write a function called firstItem that passes the first item of the given array to the callback function

const firstItem = (arr, cb) => {
cb(arr[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 = (arr, cb) => {
return 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) => {
return 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 = (a, b, cb) => {
cb(a * b);
};
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, check, cb) => {
cb(arr.includes(check));
}
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 uniqArr = [];
for (let i = 0; i < arr.length; i++) {
if (!(arr.slice(i+1, arr[arr.length]).includes(arr[i]))) {
uniqArr.push(arr[i]);
}
}
cb(uniqArr);
}
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
12 changes: 12 additions & 0 deletions commonCharacters/commonCharacters.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,15 @@
* Example: commonCharacters('acexivou', 'aegihobu') *
* Returns: 'aeiou'
*/

const commonCharacters = (str1, str2) => {
const commonChArr = [];
for (let i = 0; i < str1.length; i++){
if (!commonChArr.includes(str1[i])){
if (str2.includes(str1[i])) commonChArr.push(str1[i]);
}
}
return commonChArr;
}

console.log(commonCharacters('apple','bananas'));
70 changes: 68 additions & 2 deletions constructors/constructors.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,72 @@
*
* Create properties for these different classes that fit with the character.
*
* This is how you would structure the game objects in an actual game
* This is how you would structure the game objectsme in an actual game
* application in Unity or another similar framework.
*/
*/

class NPC {
constructor(name) {
this.alive = true;
this.name = name;
}
attack(target) {
console.log(`${this.name} attacks ${target}`);
}
describe() {
if (this.alive) console.log(`${this.name} is alive`);
else console.log(`${this.name} is dead....`);
}
/*get name() {
return this.name;
}*/
}

class Humanoid extends NPC {
constructor(name) {
super();
}
}
class Animal extends NPC {

}
class Plant extends NPC {

}
class Elf extends Humanoid {
}

class Human extends Humanoid {

}
class Orc extends Humanoid {

}
class Peasant extends Humanoid {

}
class Bandit extends Humanoid {

}
class Soldier extends Humanoid {

}
class FleshEatingDaisy extends Plant {

}
class Wolf extends Animal {

}
class Bear extends Animal {

}

const me = new Humanoid('Matt');
const me2 = new Elf('Lego');
const me3 = new NPC('test');
me3.describe();
me.attack('nic');
me.describe();
me.name;

//console.log(me2.name;)
19 changes: 18 additions & 1 deletion evenOccurences/evenOccurences.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,22 @@
* */

const evenOccurence = (arr) => {
// Your code here.
const occNums = {};
arr.forEach((x) => {
if (occNums[x]) occNums[x] += 1;
else occNums[x] = 1;
});
const occNumKeys = Object.keys(occNums);
let firstEvenOcc;
occNumKeys.forEach((x) => {
if (occNums[x] % 2 === 0) {
if (!firstEvenOcc) firstEvenOcc = x;
return;
}
});
if (firstEvenOcc) return firstEvenOcc;
return null;
};
const onlyEven = evenOccurence([1, 7, 2, 4, 5, 1, 6, 8, 9, 6, 4, 1]);
console.log(onlyEven);

31 changes: 31 additions & 0 deletions evenOccurences/evenOccurences.js~
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* * 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:
* * console.log(onlyEven); // 4
* */

const evenOccurence = (arr) => {
const occNums = {};
arr.forEach((x) => {
if (occNums[x]) occNums[x] += 1;
else occNums[x] = 1;
});
const occNumKeys = Object.keys(occNums);
let firstEvenOcc;
occNumKeys.forEach((x) => {
if (occNums[x] % 2 === 0) {
firstEvenOcc = x;
return;
}
});
if (firstEvenOcc) return firstEvenOcc;
return null;
};
const onlyEven = evenOccurence([1, 7, 2, 4, 5, 1, 6, 8, 9, 6, 4, 1]);
console.log(onlyEven);

17 changes: 12 additions & 5 deletions forLoopTimeout/forLoopTimeout.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
// 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.
// With ES6 there is a very, very simple way to solve this. (change var to let)
// 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.


// in this expample, there is closue being created becaues i is call in a higher scope. (i=11 when
//the for loop ends)
for (var i = 1; i <= 10; i++) {
setTimeout(function() {
var sT = (function(i) {
// 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);
}
return function() {
console.log(i);
}
})(i);
sT(setTimeout,0)
}
//i = 20
19 changes: 18 additions & 1 deletion isUnique/isUnique.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
/* Implement an algorithm to determine if a string has all unique characters.
* What if you cannot use additional data structures?
*/
*/


//This function uniqueCh will accept a string and return true or false depending on if it has repeating Characters
//This assumes spaces and other non-letters as characters
//Case sensitive
const uniqueCh = (str) => {
for (let i = 0; i < str.length; i++) {
if (str.slice(i+1, str.length).toUpperCase().includes(str[i].toUpperCase())) {
return false;
}
}
return true
};

console.log(uniqueCh("abcdefghii"))
console.log(uniqueCh("abcdefghij"))
console.log(uniqueCh("Halp meh"))
34 changes: 33 additions & 1 deletion largestPrimePalindrome/largestPrimePalindrome.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,36 @@
* 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 isPrime = (num) => {
if (num == 0 || num == 1 || num % 2 == 0) return false;
else if (num == 2) return true;
else {
for (let i = 3; i <= Math.sqrt(num); i += 2) {
if (num%i == 0) return false;
}
return true;
}
}

const isPalindrome = (num) => {
strNum = num.toString()
for (let i = 0; i < strNum.length / 2; i++) {
if (strNum[i] != strNum[strNum.length - (1+i)]) return false
}
return true
}

const longestPalindrome = (range) => {
for (let i = range; i > 0; i--) {
if (isPrime(i) && isPalindrome(i)) return i;
}
return undefined;
}
console.log(isPrime(68))
console.log(isPrime(19))
console.log(isPalindrome(12345))
console.log(isPalindrome(123454321))

console.log(longestPalindrome(1000))
18 changes: 18 additions & 0 deletions longestString/longestString.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,21 @@
* Write a function that accepts an array of strings.
* Return the longest string in the array.
*/

/* Test Cases
['a', 'bc' , 'acv'] -> 'acv'
strings of the same length with retrn the first longest element
*/

// loop through the strings array and search for the longest

const returnLongestString = (arr) => {
let longestString = '';
for (let i = 0; i < arr.length; i++) {
if (arr[i].length > longestString.length) {
longestString = arr[i];
}
}
return longestString;
}

34 changes: 34 additions & 0 deletions meanMedianMode/meanMedianMode.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,37 @@
* Given an array of numbers calculate the mean, median, and mode.
* Return an object with properties for the mean, median, and mode.
*/

const mmmArrCalc = arr => {
arr = arr.sort((a,b)=>{return a-b});
const countEach = {}
let sum = 0;
let med = 0;
if (arr.length %2 === 0) {
med = (arr[arr.length/2] + (arr[(arr.length/2)-1]))/2;
} else med = arr[Math.floor(arr.length/2)];
for (let i = 0; i < arr.length; i++) {
sum += arr[i];
if (countEach[arr[i]]) {
countEach[arr[i]] += 1;
} else {
countEach[arr[i]] = 1;
}
}
const countK = Object.keys(countEach);
let most = 0;
let mostValue = 0;
for (let i = 0; i < countK.length; i++) {
if (countEach[countK[i]] > most) {
most = countEach[countK[i]];
mostValue = countK[i];
}
}
const mmm = {};
mmm.median = med;
mmm.mean = sum/arr.length;
mmm.mode = mostValue;
return mmm;
}

console.log(mmmArrCalc([20,10,30,20]))
Loading