Course outline · 0% complete

0/29 lessons0%

Course overview →

Project: a to-do data model

lesson 9-1 · ~13 min · 28/29

Quiz

Warm-up. This project combines arrays of objects (lesson 5-1) with the toolkit from unit 7. Which method finds the one todo whose id matches?

The data model behind every to-do app

Strip the buttons off any to-do app and what remains is a data model: an array of objects plus a few functions that change it. That is what we build, and it runs entirely in the console.

The design:

  • Each todo is an object: { id: 1, title: "Learn JavaScript", done: false }
  • The id is a number that never repeats, so we can point at one todo later.
  • addTodo(title) pushes a new object (lesson 4-2) and bumps the id counter.
  • completeTodo(id) uses find (lesson 7-2) to locate the todo and flips its done to true.
  • pendingCount() uses filter (lesson 7-1) to count what is left. Note !t.done means not done, the ! from lesson 3-1.

This shape — data plus the functions that manage it — is what engineers call the application's state: the data a program remembers while it runs. Real apps are organized exactly this way because the screen never invents anything; it only redraws whatever the state currently says. That separation is why we can build and test the entire model in the console, before any buttons exist.

Code exercise · javascript

Read every function before running, and match each to its lesson: push from 4-2, find from 7-2, filter from 7-1, template literals from 1-3. Then run.

Code exercise · javascript

Your turn. Extend the model: write pendingTitles() that returns an ARRAY of the titles of todos that are not done. Chain filter and map, then print the result.

Quiz

Why is each todo an object like { id, title, done } instead of just the title string?