Workshop › Sokoban › Step 25 of 29
undo() takes the last photo off history and puts everything back as it was. The Z key calls it.
```js
function undo() {
const previous = history.pop()
if (!previous) return
;({ player, boxes, moves } = JSON.parse(previous))
}
if (event.key.toLowerCase() === 'z') undo()
```history.pop() removes and gives back the last photo, or undefined if there is none; then we return.JSON.parse turns the text back into an object, and ({ player, boxes, moves } = ...) puts its three values back
into the three variables. The ; and the parentheses are needed because a line cannot start with { here.toLowerCase() makes Z (Caps Lock) work too.move function, leave an empty line and write undo.if block, write the z line. Press Run.Skills: Arrays and objects, Game states
← A photo before every move Restart →