Practice: Expense Tracker
Totals by category.
Our last browser project is an expense tracker. Add an amount and a category, and the page shows a list of expenses, and a total for each category, with simple bars to compare them.

- An expense tracker
- Amount and category
- Totals and bars
The tracker
Expenses is the state. Totals uses reduce to add up each category into an object. Render draws one row per category, with a bar whose width matches its share of the biggest total. Submitting the form adds an expense, then redraws. We added four expenses, so food has the longest bar.
<link rel="stylesheet" href="style.css">
<style>.bar { background: #f7df1e; height: 14px; border-radius: 4px }</style>
<form id="f"><input id="amt" placeholder="Amount" size="6">
<input id="cat" placeholder="Category" size="8"> <button>Add</button></form>
<div id="out"></div>
<script>
const expenses = [];
const totals = () => expenses.reduce((t, e) => ({ ...t, [e.cat]: (t[e.cat] ?? 0) + e.amt }), {});
function render() {
const t = Object.entries(totals()), max = Math.max(...t.map(([, v]) => v));
out.innerHTML = t.map(([c, v]) => `<p>${c}: $${v}<div class="bar" style="width:${v / max * 100}%"></div></p>`).join("");
}
f.onsubmit = (e) => {
e.preventDefault();
expenses.push({ amt: Number(amt.value), cat: cat.value.trim().toLowerCase() });
amt.value = cat.value = ""; render();
};
</script>Also uses style.css
body{font-family:-apple-system,Helvetica,Arial,sans-serif;margin:22px;font-size:21px;color:#1d1d1f;background:#fafafa}
h1{font-size:34px;margin:0 0 14px} h2{font-size:26px;margin:0 0 10px}
button{font-size:19px;padding:9px 18px;border-radius:9px;border:0;background:#f7df1e;color:#111;font-weight:600;margin:4px 6px 4px 0}
input{font-size:19px;padding:8px 10px;border:2px solid #bbb;border-radius:8px}
li{margin:5px 0} .box{padding:14px;border-radius:12px;background:#fff;border:2px solid #ddd;margin:8px 0}
.done{text-decoration:line-through;color:#999} small{color:crimson;display:block;font-size:16px} .big{font-size:64px;font-weight:800}Also uses actions.json
[{"type": "#amt", "text": "40"}, {"type": "#cat", "text": "Food"}, {"key": "Enter"}, {"type": "#amt", "text": "15"}, {"type": "#cat", "text": "Travel"}, {"key": "Enter"}, {"type": "#amt", "text": "25"}, {"type": "#cat", "text": "food"}, {"key": "Enter"}, {"type": "#amt", "text": "30"}, {"type": "#cat", "text": "Fun"}, {"key": "Enter"}]Making it real
To finish this app, you'd save the expenses in local storage, add a delete button with event delegation, and escape the category text before putting it into inner HTML, because it comes from the user. Every one of those skills is something you've already learned in this course.
| key | value |
|---|---|
| reduce to an object | totals per category |
| Math.max(...) | the biggest total |
| Bar width | share of the max |
| Escape user text | before innerHTML |
Summary
The expense tracker grouped data with reduce, and drew simple bars from it. Next, iterators and generators.
Key takeaways- State array
- reduce into totals
- Bars from data