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 k_tsushima
"use strict";
function digitStack(commands){
let sum = 0;
const stack = [];
commands.forEach((command) => {
if (command == 'POP') {
sum += stack.length > 0 ? stack.pop() : 0;
} else if (command == 'PEEK') {
sum += stack.length > 0 ? stack[stack.length - 1] : 0;
} else {
stack.push(parseInt(command.split(' ')[1], 10));
}
});
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!");
}
July 14, 2017