JS Interview Help (pt 2): Hoisting, Currying, and Memoization

Hoisting

console.log(toby);
let toby = "My name is Toby, I'm the goodest dog, and I want to be first in line for pets!";
//=> VM246:1 Uncaught ReferenceError: toby is not defined
at <anonymous>:1:13
let toby;
console.log(toby);
toby = "My name is Toby, I'm the goodest dog, and I want to be first in line for pets!";
let tobyIsFirstInLineForPets = () => {
let first = "first";
let last = "all other doggos";
console.log(`Toby is ALWAYS ${first} in line, in front of ${last}
for pets.`);
}
tobyIsFirstInLineForPets();
//=> Toby is ALWAYS first in line, in front of all other doggos for pets.

Currying

function countTobysDugHoles(north, south, east, west) {
return north + south + east + west;
}
function countTobysDugHoles(north) {
return (south) => {
return (east) => {
return (west) => {
return north + south + east + west;
}
}
}
}

Memoization

let cache = {};function addingTobysToys(toys) {
if(toys in cache) {
let splitted = cache[toys].split(' ');
let sliced = splitted.slice(0, 2).join(' ');
return sliced + ' toy is already in your bin.';
} else {
console.log('Adding a new toy!');
return cache[toys] = toys + ' toy has been added.';
}
}
console.log('Toy 1: ', addingTobysToys("Blue Ball"))
//=> Adding a new toy!
Toy 1: Blue Ball toy has been added.
console.log('Toy 2: ', addingTobysToys("Red Dinosaur"))
//=> Adding a new toy!
Toy 2: Red Dinosaur toy has been added.
console.log('Toy 3: ', addingTobysToys("Green Turtle"))
//=> Adding a new toy!
Toy 3: Green Turtle toy has been added.
console.log('Toy 1: ', addingTobysToys("Blue Ball"))
//=> Toy 1:  Blue Ball toy is already in your bin.

Resources

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store