Step 1 of 4

Make every change a command

Start with the doing half.

Two commands, both operating on the document as a value: apply(doc) takes the document and returns the new one.

  • Insert(text) adds text to the end.
  • Remove(n) drops n characters from the end — and remembers what it removed, in this.removed. It does not need that yet. It will in step 2, and capturing it at the moment of removal is the only time it is available.

Then History.run(command) applies a command and pushes it onto the done stack.

insert("he")     ->  "he"
insert("llo")    ->  "hello"
remove(3)        ->  "he"

Removing more characters than there are should empty the document rather than fail.

Your build

Running is free — Submit is what records the step. Or press ⌘↩

Tests

4 cases, 1 hidden
calltypeexpectedresult
runOps([["insert","he"],["insert","llo"]])inserting appends to the end["he","hello"]
runOps([["insert","hello"],["remove",3]])removing drops from the end["hello","he"]
runOps([["insert","ab"],["remove",5]])removing more than there is empties the document["ab",""]
withheldhiddenwithheld

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