Skip to the content

Project: change history

JavaScript · Unit 15: Immutability

You're going to keep the history of a note: an array where every item is a version, and the last one is how the note stands right now.

Every change adds a fresh version and touches none of the old ones. To build it, the three dots twice: what was there, and the change on top.

const v1 = { txt: "bread", ok: false };
const change = { ok: true };
const v2 = { ...v1, ...change };
console.log(v1.ok);
console.log(v2);

Prints

false
{ txt: 'bread', ok: true }

Exercises in this lesson

You do them in the app, which checks them on the spot and explains why.

  1. 1. Complete the code

    Fill in the blank to read the current version, which is the last one in the history.

  2. 2. Write the code

    Write edit(h, change): it returns a new history, with one more version at the end. That fresh version is the last one in the history with change on top. The history you receive stays untouched. The check function below is ready and returns your new history along with the length of the old one.

  3. 3. Write the code

    Write undo(h): it returns a new history without the last version. If there's only one version there's nothing to undo, so you return it with that same one. The history you receive stays untouched here too.

Do this lesson

It opens in your browser. This lesson is part of the full plan; the first unit of each course is free.

See all lessons →