Learn JavaScript · lesson 45 of 60 · about 8 minutes
Capstone: Planning and Modelling
Plan Sam's flashcards: list the features, choose the data shape and write the state object.
What you'll learn
- Turn an idea into features
- Design the data model
- Keep all app state in one place
Sam's flashcards
Sam revises JavaScript with flashcards: a question on the front, the answer on the back, and a topic such as arrays. The capstone puts them on Sam's page as a small app with buttons. Before any buttons, the app needs a plan and a model: one deck object that holds the cards, which card is showing, whether it's flipped, and the ids of the cards Sam knew. The first part of the page sums the deck up, like 4 flashcards on 2 topics: arrays, basics.
The app
Your capstone is a flashcards web app for revising code. Before writing any code, list what it must do:
1. Show the front of a card
2. Flip to see the answer
3. Mark it "knew it" or "still learning"
4. Move to the next card and show a score
5. Save progress so a refresh doesn't lose it
One state object
Keep everything the app needs to remember in a single state object. The screen is just a picture of the state.
Changing state, then reading it
const state = { index: 0, flipped: false };
state.flipped = !state.flipped;
console.log(state.flipped);Output
trueThe shape of the state
| Part | Shape |
|---|---|
| cards | Array of { id, front, back } |
| index | Which card is showing |
| flipped | true or false |
| known | Array of card ids you knew |
The model
const state = {
cards: [
{ id: 1, front: "Keyword for a constant?", back: "const" },
{ id: 2, front: "Add to the end of an array?", back: "push()" },
{ id: 3, front: "Strict equality operator?", back: "===" },
],
index: 0,
flipped: false,
known: [],
};
const current = state.cards[state.index];
console.log(current.front);
console.log(`Card ${state.index + 1} of ${state.cards.length}`);Output
Keyword for a constant?
Card 1 of 3Tip
A Set keeps one of each value, so [...new Set(list)] is a quick way to list the different topics without repeats.
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.