In computer science, a stack is an abstract data type that serves as a collection of elements, with two principal operations: push, which adds an element to the collection, and pop, which removes the most recently added element that was not yet removed. The order in which elements come off a stack gives rise to its alternative name, LIFO (for last in, first out). Additionally, a peek operation may give access to the top without modifying the stack. -- Stack (abstract data type) - Wikipedia
- JavaScript Objects: Referencing Own Properties
- JavaScript Functions: Ins and Outs
- Stack (abstract data type) - Wikipedia
- Fork and clone this repository.
- Change into the new directory.
- Install dependencies.
- Create and checkout a new branch to work on.
- Fulfill the listed requirements.
Starter code is available in lib/challenge.js
. A pull
request is not required, but it is necessary if you want a code review.
You may wish to refer to FAQs related to forking, cloning.
The goal of this challenge is to practice writing prototype methods by
implementing one on a construction function that models a stack as an object. As
a reminder of how prototypes are used, the provided starter code includes a
constructor function for Stack
and a completed prototype method on it,
pop()
. The challenge is to implement the remaining method, push()
.
- It should be possible to create a new stack by invoking the
Stack
constructor function. e.g.,const stack = new Stack();
. - The stack should have two methods,
push()
, andpop()
.push()
takes one argument and adds it to the stack.pop()
does not take any arguments, removes the most recently added element from the stack, and returns it.
- The
Array
methodspop()
andpush()
should not be used.
You should be running grunt nag
before diagnosing any bugs, since it finds
some of the most common sources of errors. After grunt nag
passes, you should
run grunt test
to run the included tests. Tests will tell you whether or not
you've met these requirements.
Change xit
to it
in spec/stack.spec.js
to have grunt test
check the
bonuses.
Modify the constructor function to take an arbitrary number of arguments and store them as elements in the stack upon instantiation.
const stack = new Stack(1, 2, 3);
stack.pop(); // 3
Modify push()
to allow method chaining.
const stack = new Stack();
stack.push(1).push(2).push(3);
stack.pop(); // 3
Which parts of each method are the side effects and which parts are the "main" effect?
Developers should run these often!
grunt nag
: runs code quality analysis tools on your code and complains.grunt test
: runs any automated tests; may depend ongrunt build
.grunt
: runs bothnag
and thentest
grunt make-standard
: reformats all your code in the standard style.
- All content is licensed under a CCBYNCSA 4.0 license.
- All software code is licensed under GNU GPLv3. For commercial use or alternative licensing, please contact legal@ga.co.