Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
19 changes: 19 additions & 0 deletions brainTeasers/waterJugs.md
Original file line number Diff line number Diff line change
@@ -1 +1,20 @@
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
3 qt

=> 4 qt

find out where 3 qt mark is on 5 qt jug, which will
let us know where 2 qts is

fill 3 qt jug
xfer 3 qt water to 5 qt jug
mark 3 qt point on 5 qt jug
fill 5 qt jug
xfer water up to 3 qt mark to 3qt jug (2 qt)
mark 2 qt point on 3 qt jug
empty 5 qt jug
xfer 2 qt of water from 3 qt jug to 5 qt jug
pour 2 qt of water to 3 qt jug
xfer 2 qt of water from 3 qt jug to 5 qt jug
48 changes: 47 additions & 1 deletion callBackPractice/callBackPractice.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,54 +18,100 @@

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

const firstItem = (arr, cb) => {
cb(arr.shift());
};


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

firstItem(foods, (firstItem) => {
console.log(`The first item is ${firstItem}.`);
console.log(firstItem === 'pineapple');
});

// 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}.`);
console.log(length === 6);
});

// Write a function called last which passes the last item of the array into the callback

const last = (arr, cb) => {
cb(arr.pop());
}

last(foods, (lastItem) => {
console.log(`The last item in the array is ${lastItem}.`);
console.log(lastItem === 'mango');
});

// 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}.`);
console.log(15 === 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}.`);
console.log(product === 50);
});

// 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) => {
cb(arr.indexOf(item) !== -1);
};

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

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

var removeDuplicates = (arr, cb) => {
const out = [];
arr.forEach((el) => {
if(!out.includes(el)){
out.push(el);
}
});

cb(out);
}

removeDuplicates(foods, (uniqueFoods) => {
console.log(`foods with duplicates removed: ${uniqueFoods}`);
console.log(uniqueFoods.toString() === "mango,ribeye,curry,tacos");
});

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

const forEach = (arr, cb) => {
arr.forEach(cb);
};


forEach(foods, (value, index) => {
console.log(`${value} is at index ${index}.`);
});
});
64 changes: 63 additions & 1 deletion constructors/constructors.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,66 @@
*
* This is how you would structure the game objects in an actual game
* application in Unity or another similar framework.
*/
*/

class NPC {
constructor(hp, strength, speed){
this.hp = hp;
this.strength = strength;
this.speed = speed;
}
}

class Humanoid extends NPC {
constructor(hp, strength, speed){
super(hp, strength, speed);
}
}

class Animal extends NPC {
constructor(hp, strength, speed){
super(hp, strength, speed);
}
}

class Plant extends NPC {
constructor(hp, strength, speed) {
super(hp, strength, speed);
}
}

class Humanoid extends Human {
constructor(hp, strength, speed) {
super(hp, strength, speed);
}
}

class Elf extends Humanoid {
constructor(hp, strength, speed) {
super(hp, strength, speed);
}
}

class Orc extends Humanoid {
constructor(hp, strength, speed) {
super(hp, strength, speed);
}
}

class Bear extends Animal {
constructor(hp, strength, speed) {
super(hp, strength, speed);
}
}

class Wolf extends Animal {
constructor(hp, strength, speed) {
super(hp, strength, speed);
}
}

class FleshEatingDaisy extends Plant {
constructor(hp, strength, speed) {
super(hp, strength, speed);
}
}
10 changes: 9 additions & 1 deletion 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);
}
<<<<<<< HEAD
}

/* put the for loop in the callback function and wrapping console.log(i) in it. It doesn't work
in its current state as expected because nothing is blocking the incrementing of i from happening and by the time console.log(i) executes, we have already reached the end of the loop
*/
=======
}
>>>>>>> d1daab308de663dad2c7129e60c72bbb70519b4c
26 changes: 25 additions & 1 deletion isUnique/isUnique.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,27 @@
/* Implement an algorithm to determine if a string has all unique characters.
* What if you cannot use additional data structures?
*/
*/
/*
* i: string
* o: boolean
*
* make new str that contains set of chars
*
* for each char in the str
* if curr str is found in new str
* return false
* add curr char to new str *
* return true
*/

const isUnique = (str) => {
let included = '';
for (let i = 0; i < str.length; i++) {
if (included.indexOf(str[i]) !== -1) return false;
included += str[i];
}
return true;
};

console.log(isUnique('aa'));
console.log(isUnique('ab'));
37 changes: 36 additions & 1 deletion largestPrimePalindrome/largestPrimePalindrome.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,39 @@
* 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.
*/
*/

/*
determine if number is prime
determine whether or not it's a palindrome
find largest number of all numbers less than 1000 -> so we start counting down from 999

it's probably more efficient the other way around (palindrome, then prime)
*/

const findLargestPrimePalindromeLessThan1000 = () => {
let i = 999;
while (i > 0) {
if (String(i) === String(i).split('').reverse().join('')) {
if (isPrime(i)) {
return i;
}
}
i--;
}
return 'no prime numbers that are palindromes';
};

const isPrime = (n) => {
if (n < 2) return false;
if (n === 2) return true;
for (let i = Math.floor(n/2); i > 2; i--) {
if (n % i === 0) return false;
}
return true;
};

kisPrime(2);
isPrime(11);
isPrime(22);
isPrime(1);
38 changes: 38 additions & 0 deletions longestString/longestString.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,42 @@
/*
* Write a function that accepts an array of strings.
* Return the longest string in the array.
*
* must track both the longest length as well as the longest string
* will mapping work?
*
* in: arr of strings out: longest string
* to find the longest string in a list of strings
* take a list of strings, choose first string as being longest(assuming
* that a valid list of strings has been inputted) and then continue to loop through list until
* exhausted
*return longest string
*
*
*/




const longest = (arr) => {
if(arr.length === 0) return null;
let longestLength = arr[0].length;
let longestString = arr[0];
arr.forEach((str) => { // whatever...will just use forEach instead of starting at i = 1 using for loop
if(str.length > longestLength){
longestLength = str.length;
longestString = str;
}
});
return longestString;
};

/* expect longest(['a','aaaaa','aa']) to return 'aaaaa'
* expect longest(['aaaaa','a','aa']) to return 'aaaaa'
* expect longest(['a','aa','aaaaa']) to return 'aaaaa'
* expect longest(['aaaaa','a','aaaaa']) to return 'aaaaa'
*
*
*/

longest(['a','aaaaa','aa']) === 'aaaaa';
4 changes: 3 additions & 1 deletion removeDuplicates/removeDuplicates.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,7 @@
*/

const removeDuplicates = (arr) => {
return Array.from(new Set(arr));
//code here...
};
};

21 changes: 20 additions & 1 deletion reverseCase/reverseCase.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,23 @@
* 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.
*/
*/

/*
* in: string
* out: string with reversed case of input string
*
* create empty string
* for each char
* if current char has lower case
* then change to upper
* else change to lower
*
*/

const reverseCase = (str) => {
return str.split('').map((ch) => {
if (ch.toUpperCase() === ch) return ch.toLowerCase();
return ch.toUpperCase();
}).join('');
};