Skip to content
Open
1 change: 1 addition & 0 deletions brainTeasers/waterJugs.md
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
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 five quart jug with water from the three quart jug. Then fill up the three quart jug again. Add water from the three quart jug until you completely fill up the five quart jug. Now dump out the five quart jug and add the quart left over from the three quart jug. Finally fill up the three quart jug again and add it to the one quart within the five quart jug.
55 changes: 54 additions & 1 deletion callBackPractice/callBackPractice.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,53 +18,106 @@

// 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']; => The first item is pineapple
//Create function taking array and callback as arguements
// pass the first item of the array to the callback

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 foods = ['pineapple', 'mango', 'ribeye', 'curry', 'tacos', 'ribeye', 'mango']; => The length of the array is 7
// write function
// pass arr.length into 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 foods = ['pineapple', 'mango', 'ribeye', 'curry', 'tacos', 'ribeye', 'mango']; => The last item of the array is mango
//create function
// pass arr at last index to 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
// 5, 10 => The sum is 15.
// create function
// add numbers together and pass to cb

const sumNums = (n1, n2, cb) => cb(n1 + n2);

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
//assuming mean multiply two numbers not add
// 5, 10 => The product is 15
//create function, multiply numbers pass to cb

const multiplyNums = (n1, n2, cb) => cb(n1 * n2);

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
// ribeye => ribeye is in the array
// create function
// if array includes string pass true to callback otherwise pass false

const contains = (array, str, cb) => array.includes(str) ? cb(true) : 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.
//['pineapple', 'mango', 'ribeye', 'curry', 'tacos', 'ribeye', 'mango'] = foods with duplicates removed: 'pineapple', 'mango', 'ribeye', 'curry', 'tacos'
// create function
// make new array
// if not present in new array than push to new array

const removeDuplicates = (array, cb) => {
const uniq = [];
array.forEach((str) => {
if (!uniq.includes(str)) uniq.push(str);
});
cb(uniq);
}

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.

// ['pineapple', 'mango', 'ribeye', 'curry', 'tacos', 'ribeye', 'mango'] => pineapple is at index 0, etc...
// create function
// iterate over the array and push the value and index to callback for each element

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

// create function
// create new string
// create memo that contains a whitespace as a key value pair (this will contain every character weve seen)
// create an object that will hold the characters from s2 (this will allow us to only iterate throgh s2 once instead of once for every charcter in s1)
// iterate through s2 creating a key value pair in the s2 object
// iterate through s1
// if it contains a character that is a key in the s2obj and isnt yet in memo then add it to the string common
// add to the common character to the memo so we dont add duplicates
// return common

//TEST
// commonCharacters('acexivou', 'aegihobu') => "aeiou"
// commonCharacters('ad fsaaa u', 'aefdfa aaaa r u') => "adfu"

const commonCharacters = (s1, s2) => {
let common = '';
const memo = {
' ': ' '
};
const s2obj = {};
for (let j = 0; j < s2.length; j++) {
s2obj[s2[j]] = s2[j];
}
for (let i = 0; i < s1.length; i++) {
if (s2obj[s1[i]] && !memo[s1[i]]) {
common += s1[i];
memo[s1[i]] = s1[i];
}
}
return common;
};
88 changes: 87 additions & 1 deletion constructors/constructors.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,90 @@
*
* This is how you would structure the game objects in an actual game
* application in Unity or another similar framework.
*/
*/

/* create initial NPC class
each class than extends the proper class
use super to bring over the attributes from the previous class
pass in arguements
*/
class NPC {
constructor(hp, strength, speed) {
this.hp = hp;
this.strength = strength;
this.speed = speed;
}
}
class Humanoid extends NPC {
constructor(hp, strength, speed, swordsmanship, bowSkills) {
super(hp, strength, speed);
this.swordmanship = swordsmanship;
this.bowSkills = bowSkills
}
}
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 Human extends Humanoid {
constructor(swordmanship, hp, strength) {
super(hp || 8, strength || 5, 3, 7, 4);
this.swordmanship = swordmanship || 5;
}
}
class Elf extends Humanoid {
constructor() {
super(7, 4, 5, 3, 9);
this.swordmanship = 5;
}
}
class Orc extends Humanoid {
constructor() {
super(9, 9, 2, 3, 1);
this.swordmanship = 5;
}
}
class Bear extends Animal {
constructor() {
super(10, 8, 3);
}
}
class Wolf extends Animal {
constructor() {
super(3, 3, 8);
}
}
class FleshEatingDaisy extends Plant {
constructor() {
super(10, 10, 10);
this.hunger = 10;
this.size = 1;
}
playerNearby() {
this.size = 10;
}
}
//The playernearby method can be used to increase the size of the daisy when a player enters within a certain range
class Soldier extends Human {
constructor() {
super(8, 7, 7);
this.defense = 6;
}
}
class Peasant extends Human {
constructor() {
super(3, 2, 1);
}
}
class Bandit extends Human {
constructor() {
super(3, 3, 3);
this.thiefery = 8;
}
}
25 changes: 24 additions & 1 deletion evenOccurences/evenOccurences.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,29 @@
* * console.log(onlyEven); // 4
* */

/*
create function
intialize empty object
loop through the array
if the object contains the key then add one to the keys value
if the object doesn't contain the key then create it with a value of 1
loop through the objects keys
if the key has a value thats even return that value
*/

// Test Cases
//[1, 7, 2, 4, 5, 1, 6, 8, 9, 6, 4, 1] => 4
//[1, 2, 3, 4] => null

const evenOccurence = (arr) => {
// Your code here.
const obj = {};
for (let i = 0; i < arr.length; i++) {
if (obj[arr[i]]) obj[arr[i]] += 1;
if (!obj[arr[i]]) obj[arr[i]] = 1;
}
for (let key in obj) {
if (obj[key] % 2 === 0) return Number(key);
}
return null;
};

22 changes: 18 additions & 4 deletions forLoopTimeout/forLoopTimeout.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,24 @@
// The output should be 1, 2, 3, .... 10. Right now it just prints 11.
// I've been asked this three times in separate interviews.


/*
By the time console.log is being called the final value of i is 11 because it has already ran through the loop
this is due to closure
when creating settimeout it gets hoisted to the global scope. i is not within its scope until after the loop runs
this is why creating a function within a loop is bad practice
in es6 let would not have a block scope
*/
for (var i = 1; i <= 10; i++) {
wrapper(i);
}

function wrapper(num) {
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);
console.log(num);
}, 0);
}
};

// By setting a function around setTimeout called wrapper we can take i each time 1 is added and
// call wrapper with i as the argument. This i value is being saved at that point in time and
// console logged by setTimeout at each instance
52 changes: 51 additions & 1 deletion isUnique/isUnique.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,53 @@
/* Implement an algorithm to determine if a string has all unique characters.
* What if you cannot use additional data structures?
*/
*/

/*Identify test cases
string = 'abcd' => true
string = 'abcdc' => false
*/

/* function isUnique
create new string
iterate over string
if newstring does not include original string at index than add it to string
if lengths of string are equal return true
*/

const isUnique = (string) => {
let uniqueString = '';
for(let i = 0; i < string.length; i++) {
if(!uniqueString.includes(string[i])) {
uniqueString += string[i];
}
}
if(string.length === uniqueString.length) {
return true;
} else {
return false;
}
};

//Alt
//Changes string to array but more simple
//set a variable to true
//run for each on array of characters
// if character is the array after the first incidence it changes the variable to false
//return the variable
//must set variable because you can not make a return midway through forEach (it will not stop and keep going)

const uniqAlt = (string) => {
let boo = true;
string.split('').forEach((char, i) => {
if(string.split('').includes(char, i + 1)) {
boo = false;
}
});
return boo;
};

console.log(uniqAlt('abcd'));
console.log(uniqAlt('abcdc'));

console.log(isUnique('abcd'));
console.log(isUnique('abcdc'));
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.
*/
*/

/* test cases:
1000 => 929
200 => 191
1 => undefined
*/

// create function that takes in num
// iterate from top down to find the highest number that is a palindrome
// this way for higher numbers you dont have to iterate as far
// set a counter at 0 and j at 2
// use a while loop to test whether prime
// the while loop will help to cut down on time because as soon as you find out its not
// if the the i is found to be divisible by j the loop is stopped
// also stop the loop once j surpases i/2 because then we know no number beyond that can be evenly divide i
// add one to j each time through
// if no number is found divisible evenly then return that number because it is the highest prime palindrome

const largestPrimePalindrome = (num) => {
for (let i = num; i > 0; i--) {
const str = i.toString();
if (str === str.split('').reverse().join('')) {
let j = 2;
let count = 0;
while (count === 0 && j <= (i/2)) {
if (i % j === 0) count++;
j++;
}
if (count === 0) return i;
}
}
};
Loading