Time to put it all together! Let's write a program that prints a receipt for a meal at a café.
// Prices (these never change during the program, so we use const)
const RICE_PRICE = 40;
const CHICKEN_PRICE = 35;
const DRINK_PRICE = 10;
// What the customer ordered
const riceQuantity = 2;
const chickenQuantity = 2;
const drinkQuantity = 3;
// Work out the cost of each item
const riceCost = RICE_PRICE * riceQuantity;
const chickenCost = CHICKEN_PRICE * chickenQuantity;
const drinkCost = DRINK_PRICE * drinkQuantity;
// Add it all up (let, because we'll update it with a tip)
let total = riceCost + chickenCost + drinkCost;
console.log("===== AUNTIE ZOE'S CAFE =====");
console.log("Rice x " + riceQuantity + ": $" + riceCost);
console.log("Chicken x " + chickenQuantity + ": $" + chickenCost);
console.log("Drinks x " + drinkQuantity + ": $" + drinkCost);
console.log("Subtotal: $" + total);
const tip = 15;
total = total + tip;
console.log("Tip: $" + tip);
console.log("TOTAL: $" + total);
console.log("Thank you, come again! 🍗");
It prints
===== AUNTIE ZOE'S CAFE ===== Rice x 2: $80 Chicken x 2: $70 Drinks x 3: $30 Subtotal: $180 Tip: $15 TOTAL: $195 Thank you, come again! 🍗
Look at that! A real, useful program. Notice how the variable names make the code almost read like English. You can look at riceCost = RICE_PRICE * riceQuantity and know exactly what's going on, even without comments.
Change the quantities and prices and run the program again. Then add a new item to the menu (maybe pancakes?) with its own price, quantity, and cost, and include it in the total.
You've finished Grade 1! You can now make your programs remember things. Up next in Grade 2: what kinds of things can go in those boxes? (Spoiler: more than you'd think, and some of them are a bit weird.)