|
| 1 | +/** |
| 2 | + * @description Build stack as an empty array |
| 3 | + */ |
| 4 | +const MyStack = function () { |
| 5 | + this.stack = []; |
| 6 | +}; |
| 7 | + |
| 8 | +/** |
| 9 | + * @description Pushes element to the top of the stack |
| 10 | + * @param {Number} value |
| 11 | + * @return {Void} |
| 12 | + */ |
| 13 | +MyStack.prototype.push = function (value) { |
| 14 | + this.stack.push(value); |
| 15 | +}; |
| 16 | + |
| 17 | +/** |
| 18 | + * @description Takes element out of the stack and return it |
| 19 | + * @return {Number} |
| 20 | + */ |
| 21 | +MyStack.prototype.pop = function () { |
| 22 | + const lastElement = this.stack.pop(); |
| 23 | + return lastElement; |
| 24 | +}; |
| 25 | + |
| 26 | +/** |
| 27 | + * @description Returns the element on the top of the stack |
| 28 | + * @return {Number} |
| 29 | + */ |
| 30 | +MyStack.prototype.top = function () { |
| 31 | + const lastElement = this.stack[this.stack.length - 1]; |
| 32 | + return lastElement; |
| 33 | +}; |
| 34 | + |
| 35 | +/** |
| 36 | + * @description Check if the stack is empty |
| 37 | + * @return {Boolean} |
| 38 | + */ |
| 39 | +MyStack.prototype.empty = function () { |
| 40 | + return this.stack.length === 0; |
| 41 | +}; |
0 commit comments