Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for [old] Digit Stack by Moff
function digitStack(commands) {
let stack = [], result = 0;
for (let row of commands) {
let cmd = row.split(' ');
if (cmd[0] === 'PUSH') {
stack.push(parseInt(cmd[1]));
} else if (cmd[0] === 'POP' && stack.length > 0) {
result += stack.pop();
} else if (cmd[0] === 'PEEK' && stack.length > 0) {
result += stack[stack.length - 1];
}
}
return result;
}
July 7, 2017