You are viewing a single comment's thread. Return to all comments →
JavaScript Solution:-
function processData(input) { let currentString = ''; let operationHistory = []; const operations = input.split('\n').slice(1); for (let op of operations) { const opArr = op.split(' '); const type = parseInt(opArr[0]); if (type === 1) { const strToAppend = opArr[1]; currentString += strToAppend; operationHistory.push({ type: 1, value: strToAppend }); } else if (type === 2) { // Delete operation const numToDelete = parseInt(opArr[1]); const deletedString = currentString.slice(-numToDelete); currentString = currentString.slice(0, -numToDelete); operationHistory.push({ type: 2, value: deletedString }); } else if (type === 3) { // Print operation const index = parseInt(opArr[1]) - 1; console.log(currentString[index]); } else if (type === 4) { const lastOperation = operationHistory.pop(); if (lastOperation.type === 1) { // Undo append const appendedString = lastOperation.value; currentString = currentString.slice(0, -appendedString.length); } else if (lastOperation.type === 2) { // Undo delete const deletedString = lastOperation.value; currentString += deletedString; } } } }
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 →
JavaScript Solution:-