Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
OOP solution in Clear category for [old] Digit Stack by IvanProdaiko94
"use strict";
function Stack() {
this.sum = 0;
this.stack = [];
}
Stack.prototype.push = function(elem) {
this.stack.push(elem);
return this;
};
Stack.prototype.pop = function() {
this.stack.length ? this.sum += this.stack.pop() : this.sum;
return this;
}
Stack.prototype.peek = function() {
this.stack.length ? this.sum += this.stack[this.stack.length - 1] : this.sum;
return this;
}
function digitStack(commands){
let stack = new Stack();
commands.forEach(command => {
let separated = command.split(' ');
separated[1] ? stack[separated[0].toLowerCase()](Number(separated[1])) : stack[separated[0].toLowerCase()]()
})
return stack.sum;
}
Nov. 6, 2016