A stationery shop wants a simple program that calculates a customer’s bill.
For this project, the customer buys:
| Item | Price per item | Quantity | Total |
|---|---|---|---|
| Notebook | ₹60 | 3 | ₹180 |
| Pen | ₹10 | 2 | ₹20 |
| Pencil | ₹5 | 4 | ₹20 |
| Bill total | ₹220 |
The customer pays ₹250, so the change is ₹30.
What your program must do
Use const for this version because these values are not reassigned.
Project setup
Create a new folder named stationery-bill.
Inside it, create index.html and script.js.
Add this to index.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Stationery Bill | NHPlearning</title>
<script src="script.js" defer></script>
</head>
<body>
<main>
<h1>Stationery Bill Calculator</h1>
<p>Open the browser Console to view the bill.</p>
</main>
</body>
</html>
Complete JavaScript solution
Add this to script.js:
// Store prices and quantities.
const notebookPrice = 60;
const notebookQuantity = 3;
const penPrice = 10;
const penQuantity = 2;
const pencilPrice = 5;
const pencilQuantity = 4;
// Calculate the total for each item.
const notebookTotal = notebookPrice * notebookQuantity;
const penTotal = penPrice * penQuantity;
const pencilTotal = pencilPrice * pencilQuantity;
// Calculate the complete bill.
const billTotal = notebookTotal + penTotal + pencilTotal;
// Store payment and calculate change.
const amountPaid = 250;
const change = amountPaid - billTotal;
const isPaymentEnough = amountPaid >= billTotal;
// Display the bill.
console.log("NHP STATIONERY BILL");
console.log(
`Notebooks: ${notebookQuantity} × ₹${notebookPrice} = ₹${notebookTotal}`
);
console.log(
`Pens: ${penQuantity} × ₹${penPrice} = ₹${penTotal}`
);
console.log(
`Pencils: ${pencilQuantity} × ₹${pencilPrice} = ₹${pencilTotal}`
);
console.log(`Total bill: ₹${billTotal}`);
console.log(`Amount paid: ₹${amountPaid}`);
console.log(`Change: ₹${change}`);
console.log(`Payment is enough: ${isPaymentEnough}`);
NHP STATIONERY BILL
Notebooks: 3 × ₹60 = ₹180
Pens: 2 × ₹10 = ₹20
Pencils: 4 × ₹5 = ₹20
Total bill: ₹220
Amount paid: ₹250
Change: ₹30
Payment is enough: true
The program first stores the input values. It then calculates item totals, adds them, and compares the payment with the bill.
Notice that later calculations use the named values:
const billTotal = notebookTotal + penTotal + pencilTotal;
const billTotal = 180 + 20 + 20;
Test your program
Change one value at a time, save, and refresh.
| Change | Expected result |
|---|---|
Notebook quantity becomes 4 |
Bill becomes ₹280 |
Amount paid becomes 220, with original quantities |
Change becomes ₹0 |
Pen quantity becomes 0, with other original values |
Bill becomes ₹200 |
Amount paid becomes 200, with original quantities |
Payment check becomes false; difference is −₹20 |
In the last case, the negative difference means the customer still owes ₹20. It is not change to return. In Section 3, you will use conditions to display “Amount still due” instead.
Practice extensions
2.shopName.let notebookQuantity = 2, update it to 3, and then calculate the totals.With the eraser added to the original bill, the new total should be ₹236.
Completion checkpoint
| Skill | Complete when… |
|---|---|
| Use variables | Prices and quantities have meaningful names |
| Choose declarations | You can explain your use of const or let |
| Understand types | Prices and quantities are numbers |
| Calculate values | The original bill total is ₹220 |
| Compare values | The original payment check returns true |
| Build messages | Template literals display readable bill lines |
| Test changes | Changing an input produces the expected result |