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;
}
}
}
}

Why currying?

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

--

--

--

Michael Bade is a Full Stack Web Developer, with a passion for making abstract ideas come to life! Find me on LinkedIn to connect and talk code!

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Using Routing Table creating a setup to ping Google & not able to ping Facebook | Networking | Task…

JavaScript Objects and Prototypal Inheritance

Building a Micro-Journaling Application with HTML, CSS, and Javascript

First React Project

Verify User in Laravel (send verification mail efficiently)

Full-stack TypeScript and GraphQL

An Analysis of React Redux Express Pattern

From JavaScript to Node JS

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
Michael Bade

Michael Bade

Michael Bade is a Full Stack Web Developer, with a passion for making abstract ideas come to life! Find me on LinkedIn to connect and talk code!

More from Medium

Understanding React Class Components lifecycle methods

Real Life Example — Recursive Components

Why I quit using inheritance & instead started using object composition.

Building a ‘Fire Water Tree’ game in React