70.createStack

Write a function named createStack that returns an Object that behaves like a stack.

The Object should have 3 properties which are all functions:

  • add - a function that takes a parameter and adds it to the stack
  • remove - a function that removes the newest inserted item from the stack and returns it
  • list - a function that returns all items from the stack as an Array. The order should be the reverse of the order in which they were added to the stack (so the last item added should be the first item in the Array).

Example 1

const stack = createStack();

stack.add(1);
stack.add(2);
stack.add(3);

console.log(stack.list()); // should print [3, 2, 1]

console.log(stack.remove()); // should print 3
console.log(stack.list()); // should print [2, 1]

console.log(stack.remove()); // should print 2
console.log(stack.list()); // should print [1]

console.log(stack.remove()); // should print 1
console.log(stack.list()); // should print []