You are viewing a single comment's thread. Return to all comments →
js solution:
function processData(input) { let S = ''; const [, ...operations] = input.split("\n"); const opsStack = []; for (op of operations) { const [operation, arg] = op.split(" "); switch (operation) { case '3': console.log(S[Number(arg) - 1]); break; case '1': S = S + arg; opsStack.push({ type: 1, arg }) break; case '2': opsStack.push({ type: 2, arg: S }); S = S.slice(0, -(Number(arg))); break; case '4': //undo const lastOp = opsStack.pop(); if (lastOp.type === 1) { const appended = lastOp.arg; S = S.slice(0, - appended.length); } else { S = lastOp.arg; } break; } } }
Seems like cookies are disabled on this browser, please enable them to open this website
Simple Text Editor
You are viewing a single comment's thread. Return to all comments →
js solution: