Project: change history
JavaScript · Unit 15: Immutability
- Full plan
- JavaScript
- Project
- 3 exercises
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. Complete the code
Fill in the blank to read the current version, which is the last one in the history.
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 withchangeon top. The history you receive stays untouched. Thecheckfunction below is ready and returns your new history along with the length of the old one.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.
It opens in your browser. This lesson is part of the full plan; the first unit of each course is free.