Home
← All questions

Undo/redo system for a text input editor in JavaScript

MediumPremiumJavaScript

JavaScript interview practice: Implement a TextInput class with edit, undo, redo, and getCurrentState.

Problem

Implement TextInput with edit(text), undo(), redo(), and getCurrentState(). A new edit after undo discards the redo history.

const editor = new TextInput();
editor.edit("First version");
editor.edit("Second version");
editor.undo();  // "First version"
editor.redo();  // "Second version"
editor.undo();  // "First version"
editor.edit("Third version");
editor.undo();  // "First version"
editor.redo();  // "Third version"
Premium question

Unlock the in-browser editor, test cases, and model solution with Mentoxis Premium.

Unlock with Premium