Code Lab is in development. New lessons and improvements are on the way. Progress can be deleted.
Loading…
Code Lab is in development. New lessons and improvements are on the way. Progress can be deleted.
Loading…
Code Lab is in development. New lessons and improvements are on the way. Progress can be deleted.
JAVASCRIPT STORAGE / CHAPTER 06 / Learn / LESSON 11
Project. We combine everything into a small expense app that remembers:
1. load(): getItem → parse in try/catch → shape guard; on damage show "load-error", do not overwrite. 2. render(): a list of "Title: amount" and a total "Total: N". 3. Add: read the fields, normalize (trim, Number), add a record with a stable id (max + 1), save() inside try/catch. 4. Status: "save-failed" on error — never a false "Saved". 5. Start: load(), render().
The host adds records, then reloads the page with the same storage and runs your code again — the list must come back from labStorage. So the start (load + render) is mandatory.
Record keys in exact order: id, title, amount. Do not store passwords or tokens in browser storage.
Common mistake: rendering only after an add; loading without a guard; the total not being recomputed after a reload.
<form id="f"><input id="title" name="title" type="text"><input id="amount" name="amount" type="text"><button type="submit">Add</button></form><ul id="list"></ul><p id="total">Total: 0</p><p id="status"></p>Capstone: load, guard, render, honest save, and restore.
(empty)Press Run — HTML loads in a sandbox iframe; Stop removes the iframe (also kills infinite loops).
›