Small projects are the best way to practice JavaScript, especially when they solve a real everyday problem. In this tutorial, we will build a masala blend calculator: you enter how much blend you want, and the app scales every ingredient for you.
If you have ever tried to halve or double a spice recipe in your head, you already know why this is useful. Spices are measured in small amounts, and one wrong ratio changes the whole flavour.
No framework, no build step. Just HTML, CSS and plain JavaScript.
What We Are Building
A single page where the user:
Picks the total amount of blend they want (in grams).
Clicks a button.
Sees each spice with its scaled amount.
The Idea: Store Ratios, Not Amounts
Instead of hard-coding grams, we store each ingredient as a ratio of the whole blend. Then scaling is one multiplication.
For the demo, we will use a simple garam masala-style mix. The ratios below are only sample values for the exercise, so adjust them to your own taste:
Spice Share of the blend
Coriander seeds 35%
Cumin seeds 20%
Black peppercorns 15%
Cinnamon 10%
Green cardamom 10%
Cloves 5%
Bay leaf 5%
The shares add up to 100%, so the total is easy to reason about.
Step 1: The HTML
html
<!DOCTYPE html>
Masala Blend Calculator
<label for="total">Total blend (grams)</label>
<input id="total" type="number" min="1" value="100" />
<button id="calc">Calculate</button>
<p id="message" role="alert"></p>
<table>
<thead>
<tr><th>Spice</th><th>Amount (g)</th></tr>
</thead>
<tbody id="results"></tbody>
</table>
Step 2: The JavaScript
Create app.js:
js
const blend = [
{ name: "Coriander seeds", share: 0.35 },
{ name: "Cumin seeds", share: 0.20 },
{ name: "Black peppercorns", share: 0.15 },
{ name: "Cinnamon", share: 0.10 },
{ name: "Green cardamom", share: 0.10 },
{ name: "Cloves", share: 0.05 },
{ name: "Bay leaf", share: 0.05 },
];
const totalInput = document.getElementById("total");
const button = document.getElementById("calc");
const message = document.getElementById("message");
const results = document.getElementById("results");
function scaleBlend(total) {
return blend.map((item) => ({
name: item.name,
grams: Math.round(item.share * total * 10) / 10, // 1 decimal place
}));
}
function render(rows) {
results.innerHTML = "";
rows.forEach((row) => {
const tr = document.createElement("tr");
const name = document.createElement("td");
const amount = document.createElement("td");
name.textContent = row.name;
amount.textContent = row.grams;
tr.append(name, amount);
results.appendChild(tr);
});
}
button.addEventListener("click", () => {
const total = Number(totalInput.value);
if (!Number.isFinite(total) || total <= 0) {
message.textContent = "Please enter a number greater than zero.";
results.innerHTML = "";
return;
}
message.textContent = "";
render(scaleBlend(total));
});
Open index.html in your browser, enter 250, and click Calculate. You should see each spice scaled to a 250 g batch.
How It Works
blend holds the recipe as ratios, so the data never changes when the batch size does.
scaleBlend multiplies each ratio by the requested total and rounds to one decimal place.
render builds table rows with textContent, which avoids injecting raw HTML.
Validation checks for empty, negative or non-numeric input before calculating.
Step 3: Make It Better
Here are a few easy upgrades to practice with:
Switch units. Add a dropdown for grams and ounces, then convert before rendering.
Multiple recipes. Store several blends in an object and let the user choose one:
js
const recipes = {
garamMasala: [/* ratios here /],
sambarPowder: [/ ratios here */],
};
Live update. Listen to the input event so results change as the user types.
Save the last value. Use localStorage to remember the previous batch size.
Print-friendly view. Add a simple CSS @media print style so the recipe prints cleanly.
A Quick Test
Test the maths in the console:
js
const total = 200;
const sum = scaleBlend(total).reduce((acc, item) => acc + item.grams, 0);
console.log(sum); // should be 200 (or very close, due to rounding)
If the sum is off by a tiny amount, that is just rounding. For a kitchen scale, that is perfectly fine.
Real-World Notes
Real recipes vary a lot. Families and regions use different ratios, and the best blend is the one that tastes right to you. If you would rather skip the roasting and grinding, a ready-made garam masala powder is a convenient alternative for everyday cooking. And since South Indian dishes like sambar use their own dedicated mix, you can add a second recipe to the calculator, using a ratio set that matches your preferred sambar powder style.
Common Mistakes
Storing grams instead of ratios, which makes scaling messy.
Forgetting to check that the shares add up to 1.
Using innerHTML with user-controlled text.
Comparing floating-point numbers directly without rounding.
FAQ
Do I need a framework for this?
No. Plain JavaScript is enough for a small tool like this, and it is a good way to learn DOM basics.
Why store ratios instead of fixed grams?
Ratios make the recipe independent of batch size. One multiplication scales everything.
Why textContent instead of innerHTML?
It treats the value as plain text, which is safer and simpler.
Can I turn this into a React or Vue app later?
Yes. The blend array and scaleBlend function can be reused as they are.
Wrapping Up
In under 100 lines, we built a small but useful tool: a masala blend calculator that scales a recipe by ratio, validates input and renders a clean table. It is a good beginner project because it covers arrays, functions, DOM updates and input checks, all with something you can actually use in the kitchen.
Spice and kitchen inspiration: Sabari Home Foods.
Top comments (0)