Simple Text Editor

  • + 0 comments

    JS

    function processData(input) {
        const undo = [""];
        const operations = [
            (append) => undo.push(undo.at(-1) + append),
            (_delete) => undo.push(undo.at(-1).slice(0, undo.at(-1).length - _delete)),
            (print) => console.log(undo.at(-1).at(print - 1)),
            () => undo.pop()
        ];
        input.split("\n").slice(1).forEach((operation) => operations[operation.split(" ")[0] - 1](operation.split(" ")[1]));
    }