JavaScript required
We’re sorry, but Coda doesn’t work properly without JavaScript enabled.
Skip to content
Gallery
JS Fundamentals: Objects, Arrays, and Functions
Introduction
Content
Bonus: Merge Sort
More
Share
Explore
Objects & Arrays
Exercise: Create an array-like object
Tweet me that you did the exercise!
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
Repl.it
below to get started!
Code here
Loading…
Solution
The solution is in the solution branch, which can be checked out in the
Repl.it
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
repl.it,
the solutions are also available below.
push Solution
pop Solution
Next 👉
Functions
©2021
Code and Coffee
for
Frontend Masters
Prompt
Code here
Solution
push Solution
pop Solution
Next 👉 Functions
Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
Ctrl
P
) instead.