JavaScript lesson 3 of 50 · Beginner · About 20 minutes
Work out how much energy Pip will spend and what will remain at the end of a rescue.

Get ready
Open Complete checkpoint below. Paste the JavaScript into a blank JavaScript editor and run it. Language to look for numbers, arithmetic, subtraction, addition, multiplication, division, parentheses. Set the starting energy and the three travel costs first. These values are written without quotes because you want to do arithmetic. Writing Put the cost calculation after the declarations. In the first expression, JavaScript looks up all three names and adds their values with Use Add the print statements at the end. Predict the four outputs on paper and then run the whole program. If your energy total differs, check the costs in the opening lines rather than editing any of the printed labels. Each number in the output comes from a calculation above.
Understand the JavaScript in this lesson
1. Store quantities as numbers
12 gives a number. Writing "12" gives text that looks like a number. In expressions, keep a numeric value for every amount of energy.const startingEnergy = 12;
const workshopCost = 2;
const gardenCost = 3;
const beaconCost = 4;2. Work out the whole journey
+. In the second, it subtracts the total using -. JavaScript evaluates the expression on the right before it assigns the result to the name on the left. That order lets the second statement read the journeyCost calculated in the first.const journeyCost = workshopCost + gardenCost + beaconCost;
const energyLeft = startingEnergy - journeyCost;3. Model a top-up
* for multiplication and / for division. Two chargers each contribute three units, giving six extra units. If you combine operations, JavaScript usually multiplies and divides before adding and subtracting. To make a different order clear, group the part to calculate first in parentheses, such as (energyLeft + recharge) / 2.const chargers = 2;
const energyPerCharger = 3;
const recharge = chargers * energyPerCharger;
const energyAfterRecharge = energyLeft + recharge;
const halfOfCharge = recharge / 2;4. Compare the outputs
console.log("Journey cost:", journeyCost);
console.log("Energy left:", energyLeft);
console.log("Energy after recharge:", energyAfterRecharge);
console.log("Half the top-up:", halfOfCharge);
2 + 3 + 4 gives 9.Why The route contains three legs.
12 - 9 leaves 3.Why Moving uses energy.
The recharge is 6 and the new total is 9.Why The top-up is added to the remaining energy.
Predict before you run If the Beacon costs 5 energy instead of 4, how much energy remains before recharge?
Reveal the answer
The journey costs 10, so 12 minus 10 leaves 2.
Complete checkpoint
JavaScript · the instructions
const startingEnergy = 12;
const workshopCost = 2;
const gardenCost = 3;
const beaconCost = 4;
const journeyCost = workshopCost + gardenCost + beaconCost;
const energyLeft = startingEnergy - journeyCost;
const chargers = 2;
const energyPerCharger = 3;
const recharge = chargers * energyPerCharger;
const energyAfterRecharge = energyLeft + recharge;
const halfOfCharge = recharge / 2;
console.log("Journey cost:", journeyCost);
console.log("Energy left:", energyLeft);
console.log("Energy after recharge:", energyAfterRecharge);
console.log("Half the top-up:", halfOfCharge);
Read the JavaScript line by line
Open a small group of lines while you read the complete checkpoint. Blank lines separate ideas. Closing braces mark the end of a block. An ellipsis here means you can read the rest in the full program.
Lines 1–15
-
Line 1
const startingEnergy = 12; - Set up startingEnergy. Keep this name attached to the same value.
-
Line 2
const workshopCost = 2; - Set up workshopCost. Keep this name attached to the same value.
-
Line 3
const gardenCost = 3; - Set up gardenCost. Keep this name attached to the same value.
-
Line 4
const beaconCost = 4; - Set up beaconCost. Keep this name attached to the same value.
-
Line 6
const journeyCost = workshopCost + gardenCost + beaconCost; - Set up journeyCost. Keep this name attached to the same value. Plus adds numbers or joins text, depending on its values.
-
Line 7
const energyLeft = startingEnergy - journeyCost; - Set up energyLeft. Keep this name attached to the same value. Minus subtracts the right value.
-
Line 9
const chargers = 2; - Set up chargers. Keep this name attached to the same value.
-
Line 10
const energyPerCharger = 3; - Set up energyPerCharger. Keep this name attached to the same value.
-
Line 11
const recharge = chargers * energyPerCharger; - Set up recharge. Keep this name attached to the same value. The asterisk multiplies the values.
-
Line 12
const energyAfterRecharge = energyLeft + recharge; - Set up energyAfterRecharge. Keep this name attached to the same value. Plus adds numbers or joins text, depending on its values.
-
Line 13
const halfOfCharge = recharge / 2; - Set up halfOfCharge. Keep this name attached to the same value. The slash divides the left value by the right one.
-
Line 15
console.log("Journey cost:", journeyCost); - Show a message or value in the output. Use it to watch the game work.
Lines 16–18
-
Line 16
console.log("Energy left:", energyLeft); - Show a message or value in the output. Use it to watch the game work.
-
Line 17
console.log("Energy after recharge:", energyAfterRecharge); - Show a message or value in the output. Use it to watch the game work.
-
Line 18
console.log("Half the top-up:", halfOfCharge); - Show a message or value in the output. Use it to watch the game work.
Mission challenge
Increase the number of chargers to three. Predict the top-up and final energy before you run the code.
Change one piece at a time. Run the checkpoint again and compare what you expected with what actually happened.