Step 2 of 4
Take it back
You start from the build so far — your own work where you have written it, the reference build where you have not. Either way this step stands on its own.
Now the undoing half.
Give each command a revert(doc) that returns the document to what it was
before that command applied — Insert drops what it added, Remove puts back
what it saved.
Then History.undo() pops the newest command off done, reverts the document
with it, and pushes it onto undone so step 3 can find it.
insert("hello") -> "hello"
remove(3) -> "he"
undo() -> "hello"Undoing when there is nothing to undo must do nothing at all — not throw, not empty the document. Someone holding ctrl-Z has not made a mistake.
Your build
Running is free — Submit is what records the step. Or press ⌘↩
Tests
4 cases, 1 hidden| call | type | expected | result |
|---|---|---|---|
| runOps([["insert","ab"],["insert","cd"],["undo"]]) | undo takes back the last command | ["ab","abcd","ab"] | — |
| runOps([["insert","hello"],["remove",3],["undo"]]) | undoing a removal puts the text back | ["hello","he","hello"] | — |
| runOps([["undo"],["text"]]) | undo with nothing to undo does nothing | ["",""] | — |
| withheld | hidden | withheld | — |
Hidden cases run too — their inputs aren't listed here, so aim for a general solution rather than one fitted to the cases above.
Hints
Stuck? Hints open one at a time, each giving a little more away.
3 hints left