ILILCode
HomeJavaScriptProjectsproject

Project: To-do list

Pure state functions plus a small render step — the pattern behind most real interfaces.

Project 20 of 23 in JavaScript35 min2 exercises

A to-do list looks like a UI problem. It is really two problems: keeping the list correct, and drawing it. Separate them and both become easy to test — which is why this project is split into two checkpoints.

  1. 01Core — addTodo, toggleTodo and removeTodo take the current list and return a new one. No DOM, no mutation.
  2. 02Render — turn that array into <li> elements inside <ul id="list">.
  3. 03In a real page you would also save the array with localStorage.setItem("todos", JSON.stringify(todos)) and load it back on start.
The shape of a todo
{ id: 1, title: "Buy milk", done: false }

Exercises

Checked in your browser · hints and solutions included

01

Exercise

Checkpoint 1 — the core functions

Write three functions that never mutate their input:

  • addTodo(todos, title) — return a new array with { id, title: title.trim(), done: false } appended. The id is the highest existing id plus one, or 1 when the list is empty.
  • toggleTodo(todos, id) — return a new array with done flipped on the matching todo.
  • removeTodo(todos, id) — return a new array without the matching todo.

Unknown ids leave the list unchanged.

exercise.js
console · run your code or check the answer
02

Exercise

Checkpoint 2 — render the list

Write render(todos) that replaces the contents of #app with <ul id="list"> containing one <li> per todo:

  • the <li> text is the todo title
  • it has data-id set to the todo id
  • it has the class done when the todo is done

The list must exist even when there are no todos.

exercise.js
console · run your code or check the answer