Medium
This question is a daily question and will count towards your daily streak.
In a collaborative document editor, you need to implement an undo/redo system that tracks changes efficiently. What's missing in this implementation?
1class DocumentHistory {2 constructor(initialContent = '') {3 this.undoStack = [{ content: initialContent, timestamp: Date.now() }];4 this.redoStack = [];5 this.currentContent = initialContent;6 }78 recordChange(newContent) {9 // Missing implementation10 _______________11 }1213 undo() {14 if (this.undoStack.length <= 1) return this.currentContent;1516 const current = this.undoStack.pop();17 this.redoStack.push(current);18 const previous = this.undoStack[this.undoStack.length - 1];19 this.currentContent = previous.content;2021 return this.currentContent;22 }2324 redo() {25 if (this.redoStack.length === 0) return this.currentContent;2627 const next = this.redoStack.pop();28 this.undoStack.push(next);29 this.currentContent = next.content;3031 return this.currentContent;32 }33}