This is it — the whole vanilla JavaScript app.
We have the HTML, the CSS, and the math. Now we connect them:
- State — the target, the numbers, and the food list.
- Render — draw the page from state.
- Events — the forms and the remove buttons.
- 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.
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 stepAdd 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")isNaN; aNaNtotal 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.
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.