Hey there! Did you also by any chance get this question as part of a coding challenge for a company you applied to? Not such an easy problem, is it?

Today we will see the solution to this medium difficulty problem.

First of all, let’s take a quick look at the code:

Here’s step by step what the code is doing:


Introduction

If you have been using Javascript, there’s a good chance you have stumbled upon or heard of object prototypes. And even if you haven’t I can assure you that you have been using them.

What is Object.prototype? Array.prototype? String.prototype? __proto__? Scary stuff!

MDN Definition

According to MDN:
“Prototypes are the mechanism by which JavaScript objects inherit features from one another.”
What that means is that objects in Javascript have a prototype object which acts like a template or blueprint from which individual objects instances inherit properties and methods.

Have you ever wondered where do arrays or strings get their native methods from…


Introduction

Promises in Javascript is one of those topics that some people have a rough time with at first. I certainly did.
In this article I will try to explain what Promises are while keeping it as simple as I can.

Before we dive into asynchronous land, please make sure you fully understand what the call stack is and what single threaded means.
Here are two articles I wrote, please read them and then come back to this one.

The Call Stack
Javascript: Single Threaded

What is a Promise?

Let’s start with a textbook definition:

“A Promise is an object representing the eventual completion or…


What does it mean?

Javascript is a single threaded language, meaning it only has one main thread. Without going too much into detail, you can imagine a thread as being the “thing” that executes your code line by line.

To make an analogy, if our program was a food truck, the thread would be the person preparing and serving the food. Each customer would be a line of code and they would have to line up and be served one by one in order.

Now imagine this scene, people coming up to the window, placing an order, being handed their food and leaving. Everything…


What is it?

The call stack is a mechanism that Javascript, and almost all other programming languages, implement behind the scenes to manage function calls and keep track of where they are in the current script.

The call stack is what allows Javascript to “know” what function is currently being run and what other functions are waiting to execute.

The term “stack” comes from the data structure that is used to implement it. A stack, in computer science is a basic structure that holds data and handles it on a LIFO (Last In First Out) basis.

How does it work?


Prerequisites
Before looking at closures, please make sure you fully understand what scope is. I’m working on an article about scope and I will share the link here as soon as it’s done.

What exactly are closures?
MDN says:

“A closure is the combination of a function bundled together (enclosed) with references to its surrounding state (the lexical environment).”

Sounds scary at first.
To put it simply, a closure is nothing more than a scope that gets returned by a function together with its return value.

For example, let’s consider a function that returns an object that has two methods.

Mussin Benarbia

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