SkillPilotAI

Mini project: a vanilla JS app

This project builds a task app in plain JavaScript using everything so far: state and a render function, event delegation, debounce for search and localStorage so tasks survive a refresh. The state + render pattern is the same core idea React is built on.

20.1 The problem: data only in the DOM

  • The beginner version creates an li on submit and appends it straight to the list.
  • JavaScript has no list of tasks; the data lives only in the DOM.
  • Refresh the page and everything is gone; search and delete would need DOM hunting every time.
JavaScript
form.addEventListener("submit", (e) => {  e.preventDefault();  const li = document.createElement("li");  li.textContent = input.value;  list.append(li);  input.value = "";});
Output
Page after adding Buy milk, Pay bill and refreshing:# Tasks

20.2 How it works: state and render

  • State: one tasks array holds all app data; query holds the search text.
  • render() filters tasks by query and rebuilds the list with replaceChildren.
  • Rule: after every data change, call render. Never edit the DOM by hand.
The app flow
  1. 1.Event from the user: submit, click or input

  2. 2.Handler updates only the state (tasks array, query)

  3. 3.save() writes tasks to localStorage

  4. 4.render() rebuilds the whole list from state

  5. 5.Screen always matches the data

UI = function of state

JavaScript
let tasks = [];let query = ""; function render() {  const shown = tasks.filter((t) =>    t.text.toLowerCase().includes(query)  );  list.replaceChildren(...shown.map(toItem));  count.textContent = `Tasks: ${shown.length}`;}
Output
# TasksTasks: 0

20.3 One task becomes one list item

  • li.dataset.id stores the task id so a click can tell which task it is.
  • classList.toggle("done", task.done) adds the class only when done is true.
  • The Delete button gets data-action="delete".
  • append(task.text, " ", btn) adds user text as text, never as HTML.
JavaScript
function toItem(task) {  const li = document.createElement("li");  li.dataset.id = task.id;  li.classList.toggle("done", task.done);   const btn = document.createElement("button");  btn.textContent = "Delete";  btn.dataset.action = "delete";   li.append(task.text, " ", btn);  return li;}

20.4 Adding a task

  • preventDefault stops the page reload; trim the text and return if it is empty.
  • crypto.randomUUID() creates a unique id (already a string).
  • tasks = [...tasks, task]: create a new array instead of mutating, then render.
JavaScript
form.addEventListener("submit", (e) => {  e.preventDefault();  const text = input.value.trim();  if (!text) return;  const id = crypto.randomUUID();  const task = { id, text, done: false };  tasks = [...tasks, task];  input.value = "";  render();});
Output
# TasksTasks: 3Buy milkPay electricity billBook train ticket

20.5 Toggle and delete with event delegation

  • One listener on the whole list instead of one per li.
  • e.target.closest("li") finds the clicked item; dataset.id gives its id.
  • Delete button: filter removes the task; anywhere else: map toggles done on a copy ({ ...t, done: !t.done }).
JavaScript
list.addEventListener("click", (e) => {  const li = e.target.closest("li");  if (!li) return;  const id = li.dataset.id;  if (e.target.dataset.action === "delete") {    tasks = tasks.filter((t) => t.id !== id);  } else {    tasks = tasks.map((t) =>      t.id === id ? { ...t, done: !t.done } : t    );  }  render();});
Output
# TasksTasks: 2Buy milk (done)Pay electricity bill

20.6 Search with debounce

  • Rendering on every key would rebuild the list 4 times for "Bill".
  • Debounce runs onSearch 300ms after the last key.
  • Both query and task text are lowercased, so Bill and bill match.
JavaScript
function debounce(fn, ms) {  let timer;  return (...args) => {    clearTimeout(timer);    timer = setTimeout(() => fn(...args), ms);  };} const onSearch = debounce((e) => {  query = e.target.value.trim().toLowerCase();  render();}, 300); search.addEventListener("input", onSearch);
Output
# TasksSearch: BillTasks: 1Pay electricity bill

20.7 localStorage: survives a refresh

  • save(): JSON.stringify(tasks) into localStorage (it stores only strings).
  • load(): parse it back; return [] when nothing is saved or the data is corrupt (try/catch).
  • let tasks = load(); call save() before render() after every add/toggle/delete.
  • A versioned key (tasks-v1) lets you change the data shape later.
JavaScript
const KEY = "tasks-v1"; function save() {  const json = JSON.stringify(tasks);  localStorage.setItem(KEY, json);} function load() {  try {    const saved = localStorage.getItem(KEY);    return JSON.parse(saved) ?? [];  } catch {    return [];  }}
Output
Page after refresh:# TasksTasks: 2Buy milk (done)Pay electricity bill

20.8 Checkpoint: dataset turns ids into strings

JavaScript
const tasks = [{ id: 1, text: "Buy milk" }];const li = document.createElement("li"); li.dataset.id = tasks[0].id;const id = li.dataset.id; const left = tasks.filter((t) => t.id !== id);console.log(typeof id, left.length);
Output
string 1
  • dataset always stores strings; "1" !== 1 under strict equality, so nothing was deleted.
  • That is why the app uses crypto.randomUUID(): the id is a string from the start.

20.9 Comparison: DOM-only vs state + render

PointData only in the DOMState + render
Source of truthThe DOM elementsThe tasks array
RefreshAll tasks lostRestored from localStorage
Search / deleteFind and edit elements each timeChange the array, call render
BugsScreen and data drift apartScreen always built from data
Scales toTiny demosReal apps (same idea as React)

20.10 Common mistakes

  • User text in innerHTML: a typed HTML tag runs as real HTML (XSS). Use append or textContent.
  • Comparing a dataset id (string) with a number id using ===.
  • Reading localStorage without try/catch: corrupt data crashes the app.
  • Attaching a listener to every li instead of one on the list.
  • Myth: you need React for a real app. React uses the same DOM APIs; frameworks just automate efficient rendering in big apps.
JavaScript
// Wrong: user HTML runsfunction toItem(task) {  const li = document.createElement("li");  li.innerHTML = `${task.text}    <button>Delete</button>`;  return li;} // Right: just textfunction toItem(task) {  const li = document.createElement("li");  const btn = document.createElement("button");  btn.textContent = "Delete";  li.append(task.text, " ", btn);  return li;}

20.11 Interview tips

  • Without a framework: one state array, a render function that builds the UI from state.
  • Handlers only update state (immutably) and call save + render.
  • Event delegation for the list, debounce for search, localStorage (with try/catch) for persistence.
  • Never put user text into innerHTML.
  • This state -> render loop is exactly the idea behind React.