JavaScript 08 · Make a move with reusable functions

JavaScript lesson 8 of 50 · Beginner · About 30 minutes

Package Pip’s travel maths into a function you can call for journeys with different costs.

A distress signal lights up Pip’s antenna at Sky Dock while two cloud creatures wait on a distant platform.
Mission Identify a function name, its parameters, the arguments supplied and the value it returns.

Get ready

Open Complete checkpoint below. Paste the JavaScript into a blank JavaScript editor and run it.

Define the travel rule. Give the function two named inputs. → Call it for each leg. Hand the returned energy to the next call. → Test an edge case. A blocked move leaves the energy as it was.
Define the travel rule → Call it for each leg → Test an edge case

Understand the JavaScript in this lesson

Language to look for function declarations, parameters, arguments, return, function scope, guard checks.

1. Define a rule once

Start with the keyword function, the name travel and a pair of parentheses. energy and cost are parameters, the names the function uses for its information. The braces hold the function body. Writing a function definition tells JavaScript how to do the work when a call asks for it. The body does not begin running when you write the definition.

function travel(energy, cost) {
  if (energy < cost) {
    return energy;
  }
  return energy - cost;
}

2. Call the rule for every leg

The values you pass in are arguments. In the first call, startingEnergy is looked up as 12 and the next argument is 2. Inside that call, JavaScript uses 12 for energy and 2 for cost. When the function returns the answer, the next line can save the result and use it as the starting energy for the following leg. The names created for one call belong inside that call.

const startingEnergy = 12;
const afterWorkshop = travel(startingEnergy, 2);
const afterGarden = travel(afterWorkshop, 3);
const afterBeacon = travel(afterGarden, 4);

3. Try a move that costs too much

Run the reports and follow the last call. The comparison energy < cost detects a blocked move. return hands back a value and stops the current function call immediately. The later subtraction line runs only if the early return was skipped. Since travel() returns the earlier energy for a blocked move, the battery never goes below zero in this route.

console.log("Workshop energy:", afterWorkshop);
console.log("Garden energy:", afterGarden);
console.log("Beacon energy:", afterBeacon);
console.log("After a blocked move:", travel(afterBeacon, 8));
Follow what happens step by step
  1. Call travel with 12 and 2
    It gives back 10.Why The rule subtracts the cost when there is enough energy.
  2. Reuse the rule with the latest energy
    10 becomes 7, then 7 becomes 3.Why Each call feeds the next with its returned energy.
  3. Try travel with 3 and 8
    It gives back 3.Why The early return takes effect and skips subtraction.

Predict before you run What happens if Pip has 4 energy and the move costs exactly 4?

Reveal the answer

The blocked comparison is false because 4 is not less than 4. The function subtracts 4 and returns 0.

Complete checkpoint

JavaScript · the instructions

function travel(energy, cost) {
  if (energy < cost) {
    return energy;
  }
  return energy - cost;
}

const startingEnergy = 12;
const afterWorkshop = travel(startingEnergy, 2);
const afterGarden = travel(afterWorkshop, 3);
const afterBeacon = travel(afterGarden, 4);

console.log("Workshop energy:", afterWorkshop);
console.log("Garden energy:", afterGarden);
console.log("Beacon energy:", afterBeacon);
console.log("After a blocked move:", travel(afterBeacon, 8));

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–16
Line 1 function travel(energy, cost) {
Define travel. The body runs when the function is called, with inputs named energy, cost.
Line 2 if (energy < cost) {
Run this part when the test is truthy. Check whether the first value is less than the second.
Line 3 return energy;
Leave this function and send the value back to the caller.
Line 5 return energy - cost;
Leave this function and send the value back to the caller. Minus subtracts the right value.
Line 8 const startingEnergy = 12;
Set up startingEnergy. Keep this name attached to the same value.
Line 9 const afterWorkshop = travel(startingEnergy, 2);
Set up afterWorkshop. Keep this name attached to the same value. Call travel with the values inside the parentheses.
Line 10 const afterGarden = travel(afterWorkshop, 3);
Set up afterGarden. Keep this name attached to the same value. Call travel with the values inside the parentheses.
Line 11 const afterBeacon = travel(afterGarden, 4);
Set up afterBeacon. Keep this name attached to the same value. Call travel with the values inside the parentheses.
Line 13 console.log("Workshop energy:", afterWorkshop);
Show a message or value in the output. Use it to watch the game work.
Line 14 console.log("Garden energy:", afterGarden);
Show a message or value in the output. Use it to watch the game work.
Line 15 console.log("Beacon energy:", afterBeacon);
Show a message or value in the output. Use it to watch the game work.
Line 16 console.log("After a blocked move:", travel(afterBeacon, 8));
Show a message or value in the output. Use it to watch the game work.

Mission challenge

Write a separate function called recharge that adds a given amount to the current energy. Call it after the blocked move and print the returned number.

Change one piece at a time. Run the checkpoint again and compare what you expected with what actually happened.