Workshop › Klondike Solitaire › Step 39 of 40
undo takes the last snapshot off the stack and turns it back into piles. A stack is exactly right: the last thing done is
the first thing undone. The Z key calls it.
```js
function undo() {
if (!history.length || won) return
piles = JSON.parse(history.pop())
moves += 1
}
if (event.key === 'z' || event.key === 'Z') undo()
else if (event.key === ' ') flipStock()
ctx.fillText('Best ' + (best ? best + 's' : '-') + ' Z undo', canvas.width - LEFT, 24)
```history.pop() takes the last snapshot off; JSON.parse turns the text back into brand new arrays and cards. Even a card
that was turned face up comes back face down, with no special code.save, leave an empty line and write undo.keydown listener, write the Z line first and turn the Space line's if into else if.+ ' Z undo' to the Best text.Skills: Arrays and objects, Game states
← Snapshots of the table Build it yourself: finish by itself →