JomMula

Frontend Development: HTML to React · JavaScript · Lesson 21

Capstone part 2: the working calculator

Connect the logic to the page: state, render, form events, add and remove food, live totals, and localStorage. This is the complete vanilla JavaScript app.

12 min readJavaScriptbuildstateUpdated October 2026

This is it — the whole vanilla JavaScript app.

We have the HTML, the CSS, and the math. Now we connect them:

  1. State — the target, the numbers, and the food list.
  2. Render — draw the page from state.
  3. Events — the forms and the remove buttons.
  4. Storage — save and load the state.

The pattern is exactly what we practiced in lessons 17 and 18. The only difference is that it’s all in one app.

The shape of the state

let state = {
  target: 0,
  bmr: 0,
  tdee: 0,
  activity: "moderate",
  foods: [],
};

One object. Everything the UI needs is in there. When something changes, we update the state, save it, and call render().

render() is the heart

function render() {
  const eaten = totalCalories(state.foods);
  const left = state.target ? state.target - eaten : null;

  targetEl.textContent = state.target ? state.target.toLocaleString() : "0";
  eatenEl.textContent = eaten.toLocaleString();
  leftEl.textContent = left === null ? "—" : left.toLocaleString();

  list.replaceChildren();
  for (const food of state.foods) {
    // create a row, set textContent, append
  }
}

Every event handler ends with:

saveState();
render();

Change the state, save it, redraw. That’s the loop.

The full app

Read it in this order: state, helpers, render, events, startup. Then try it: calculate a target, add foods, remove one, and press Run again to see the log persist.

Preview

Console

Console is quiet. Run the code to see output.

Edit the code, then hit Run (or Ctrl + Enter) and watch the preview. This is a safe sandbox — you can't break the page.

Read the code in this order

  • State — let state = loadState(). One object, loaded from storage.
  • Helpers — activityMultiplier, calculateBMR, calculateTarget, totalCalories. Pure functions.
  • render() — reads state, writes the DOM. Total and left are derived, not stored.
  • Events — calculator submit, food submit, remove (delegated), clear.
  • Startup — render() once, then wait for events.

Every handler follows the same rhythm:

// 1. prevent the default
// 2. read and validate input
// 3. update state
// 4. save
// 5. render

Once you notice that pattern, the app stops looking like a lot of code.

Extend the app

the plan's verify step

Add a protein field to the food form, show total protein in the stats, and persist it with the rest of the state. Then add a progress bar that shows eaten vs target. Keep using textContent and derive the numbers in render().

If the bar is wider than 100%, clamp it with Math.min(100, (eaten / target) * 100). A target of 0 should show an empty bar, not divide by zero.

The mistakes everyone makes

  • Forgetting to call render. The state changes; the screen doesn’t.
  • Forgetting to save after delete. The page looks right until you refresh.
  • Using innerHTML for food names. XSS. Use textContent.
  • Storing the total separately. Derive it from foods.
  • Not validating numbers. Number("abc") is NaN; a NaN total poisons everything.
  • Dividing by a zero target. Check for 0 before calculating a percentage.
  • One giant event handler. Keep calculate, add, remove, and clear separate.
  • Assuming localStorage is a database. It’s per browser and easy to clear.

State is one object; the UI is rendered from it.

render() updates every dependent value, including totals.

Every handler: prevent, validate, update state, save, render.

Remove buttons use event delegation and data-id.

Derive totals; don't store duplicate copies.

Use textContent for user data; never innerHTML.

That’s part 3. You’ve built a complete, working, persistent app in vanilla JavaScript — with no framework. Keep this code. In the React part, we’ll rebuild the same calculator and compare the two.

Next: why React exists, using the app you just built as the starting point.

Previous: Capstone part 1: calculator logic
22

Up next

Why React? The same calculator, a new mental model

You already built the calculator in vanilla JavaScript. Now let's look at what gets painful as it grows — and why React's component model fixes it.

Read now