Learn JavaScript · lesson 48 of 60 · about 9 minutes
Capstone: Polishing
Finish Sam's flashcards with saving, keyboard shortcuts and clearer code.
What you'll learn
- Save and load state as JSON
- Map keys to actions
- Review code for readability
Remembering where Sam was
Refreshing the page starts the deck again, which loses Sam's progress. The polish saves the card and the known ids in localStorage after every button, with one listener on the box around the buttons: clicks bubble up from each button to it. When the page opens, it loads them inside try...catch, keeping only ids of cards that still exist and going back to card 1 if the saved card is gone, and says what happened: Starting a new session, Welcome back! 2 known so far, or that the saved progress was broken. Last, the space bar, k and l press Flip, Knew it and Still learning.
Saving progress
Browsers have localStorage, a small store of text that survives a refresh:
localStorage.setItem("flashcards", JSON.stringify(state))savesJSON.parse(localStorage.getItem("flashcards"))loads
Wrap loading in try...catch in case the saved text is missing or broken. Below, a Map stands in for localStorage.
Save, load and keyboard shortcuts
const storage = new Map(); // stands in for localStorage
function save(state) {
storage.set("flashcards", JSON.stringify(state));
}
function load() {
try {
return JSON.parse(storage.get("flashcards")) ?? { index: 0, known: [] };
} catch {
return { index: 0, known: [] };
}
}
const KEYS = { " ": "flip", ArrowRight: "next", k: "knew" };
let state = load();
console.log(state.index);
state.index = 2;
state.known.push(4);
save(state);
console.log(load().known.join(","));
console.log(KEYS["ArrowRight"], KEYS["x"] ?? "ignored");Output
0
4
next ignoredClicks bubble up
A click on a button also reaches the elements around it, so one listener on the buttons' box hears every button, after the button's own listener has run. A keydown listener on document hears every key, and event.key says which one; button.click() then presses the button, so a key and a tap do exactly the same thing.
One listener for every button
const box = document.querySelector("#buttons");
document.querySelector("#a").addEventListener("click", () => console.log("A pressed"));
box.addEventListener("click", () => console.log("saved"));
document.querySelector("#b").click();
document.querySelector("#a").click();Output
saved
A pressed
savedA final review
Before calling it done, read your code as if you were someone else:
- Are names clear?
nextbeatsn2 - Does each function do one job?
- Are magic values like key names collected in one place?
- Do the tests still pass?
Tip
?? gives a fallback only when the left side is null or undefined. That's safer than ||, which also replaces 0 and empty strings.
Now try it yourself
This lesson is free in Codiqo, with 9 questions on what you just read. You can only learn to code by writing code, so it ends with a short program you write yourself, which we run and check straight away.