Skip to content
Gallery
JS Fundamentals: Objects, Arrays, and Functions
Share
Explore
Objects & Arrays

icon picker
Exercise: Create an array-like object

Prompt

Implement push and pop and array-like behavior using an object.
function createArray() {
const storage = {};

return {
length: 0,
push: (value) => { /* TODO */ },
pop: () => { /* TODO */ }
};
}

const myArray = createArray();

myArray.push(11);
console.log(myArray);// myArray looks like {0: 11}
myArray.push(12);
console.log(myArray); // => {0: 11, 1: 12}

myArray.length; // => returns 2

myArray.pop(); // => returns 12
myArray.length; // => returns 1
Copy and paste the starter code above or clone the below to get started!

Code here

Loading…

Solution

The solution is in the solution branch, which can be checked out in the by selecting the version control tab on the right and then selecting the solution branch from the drop down menu.
If you are having trouble with the solutions are also available below.

push Solution

pop Solution

Next 👉


©2021 for


Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.