Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Clear&Readable solution in Clear category for [old] Digit Stack by SaintDron
"use strict";
function digitStack(commands){
let stack = [],
sum = 0;
for (let c of commands) {
if (c === 'POP') {
sum += stack.pop() || 0;
} else if (c === 'PEEK') {
sum += stack[stack.length - 1] || 0;
} else {
stack.push(+c.slice(5));
}
}
return sum;
}
var assert = require('assert');
if (!global.is_checking) {
assert.equal(digitStack(["PUSH 3", "POP", "POP", "PUSH 4", "PEEK",
"PUSH 9", "PUSH 0", "PEEK", "POP", "PUSH 1", "PEEK"]),
8, "Example");
assert.equal(digitStack(["POP", "POP"]), 0, "pop, pop, zero");
assert.equal(digitStack(["PUSH 9", "PUSH 9", "POP"]), 9, "Push the button");
assert.equal(digitStack([]), 0, "Nothing");
console.log("Coding complete? Click 'Check' to review your tests and earn cool rewards!");
}
March 12, 2018