JavaScript lesson 10 of 50 · Beginner · About 35 minutes
Bring together state, loops and functions to collect rescue parts and guide a cloud creature home.

Get ready
Open Complete checkpoint below. Paste the JavaScript into a blank JavaScript editor and run it. Language to look for objects, arrays, for...of, functions, return, guard checks, combining earlier skills. Write Pip’s state as one object and the route as an array of different stop objects. The same three keys describe each stop, so the function can read every stop in the same way. The empty string for part at the Beacon means there is nothing new to collect there. Pip starts at Central Hub with an empty toolkit. The The loop passes one stop object to Below the loop, require the wind cell and wind lens as well as the Beacon location. Try the program as written and then set the opening energy to 7. The lower battery still lets Pip collect both parts, but it prevents the final leg. The rescue message is safe because the code checks the location as well as the toolkit.
Understand the JavaScript in this lesson
1. Describe the mission before running it
const pip = { location: "Central Hub", energy: 12, toolkit: [] };
const route = [
{ name: "Workshop", cost: 2, part: "wind cell" },
{ name: "Garden", cost: 3, part: "wind lens" },
{ name: "Beacon", cost: 4, part: "" }
];2. Give every stop the same visiting rule
visit() function receives a robot object and one stop object. It first checks the battery and returns false if the visit cannot happen. Otherwise it changes the location and subtracts the cost on the supplied robot. In the collection check, !== means “is not the same type and value as”, so an empty part name never goes into the toolkit. A second check prevents duplicates. The success return gives the caller a yes-or-no result.function visit(robot, stop) {
if (robot.energy < stop.cost) {
return false;
}
robot.energy -= stop.cost;
robot.location = stop.name;
if (stop.part !== "" && !robot.toolkit.includes(stop.part)) {
robot.toolkit.push(stop.part);
}
return true;
}3. Let the route drive the game
visit() at a time. If the visit returns false, the program reports the blocked stop. break leaves the nearest loop and continues below it, preventing later stops from being tried after a failure. Otherwise the current message reports the values that visit() just changed.console.log("Pip sets off from Central Hub.");
for (const stop of route) {
if (!visit(pip, stop)) {
console.log(`Blocked before ${stop.name}.`);
break;
}
console.log(`Reached ${pip.location}. Energy: ${pip.energy}.`);
}4. Test the win condition
const hasParts = pip.toolkit.includes("wind cell") && pip.toolkit.includes("wind lens");
if (pip.location === "Beacon" && hasParts) {
console.log("Beacon restored! The cloud creature can fly home.");
} else {
console.log("Rescue paused. Pip needs both parts and a route to the Beacon.");
}
Pip collects wind cell and wind lens, with 7 energy remaining.Why The visit rule changes one robot object and adds each missing part.
Pip reaches Beacon, with 3 energy remaining.Why The last stop costs 4 and has no part to collect.
Beacon restored! The cloud creature can fly home.Why Both toolkit checks and the location check are true.
Predict before you run What happens if you set Pip’s starting energy to 7?
Reveal the answer
Pip visits Workshop and Garden and has 2 energy at the end of those stops. The visit to Beacon needs 4, so the route stops and the rescue is paused.
Complete checkpoint
JavaScript · the instructions
const pip = { location: "Central Hub", energy: 12, toolkit: [] };
const route = [
{ name: "Workshop", cost: 2, part: "wind cell" },
{ name: "Garden", cost: 3, part: "wind lens" },
{ name: "Beacon", cost: 4, part: "" }
];
function visit(robot, stop) {
if (robot.energy < stop.cost) {
return false;
}
robot.energy -= stop.cost;
robot.location = stop.name;
if (stop.part !== "" && !robot.toolkit.includes(stop.part)) {
robot.toolkit.push(stop.part);
}
return true;
}
console.log("Pip sets off from Central Hub.");
for (const stop of route) {
if (!visit(pip, stop)) {
console.log(`Blocked before ${stop.name}.`);
break;
}
console.log(`Reached ${pip.location}. Energy: ${pip.energy}.`);
}
const hasParts = pip.toolkit.includes("wind cell") && pip.toolkit.includes("wind lens");
if (pip.location === "Beacon" && hasParts) {
console.log("Beacon restored! The cloud creature can fly home.");
} else {
console.log("Rescue paused. Pip needs both parts and a route to the Beacon.");
}
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–21
-
Line 1
const pip = { location: "Central Hub", energy: 12, toolkit: [] }; - Set up pip. Keep this name attached to the same value. Braces create an object. Properties give names to the stored values. Square brackets create an ordered array. Its first position is 0.
-
Lines 2–6
const route = [ { name: "Workshop", cost: 2, part: "wind cell" }, { name: "Garden", cost: 3, part: "wind lens" }, { name: "Beacon", cost: 4, part: "" } ]; - Set up route. Keep this name attached to the same value. Square brackets create an ordered array. Its first position is 0. Braces create an object. Properties give names to the stored values.
-
Line 8
function visit(robot, stop) { - Define visit. The body runs when the function is called, with inputs named robot, stop.
-
Line 9
if (robot.energy < stop.cost) { - Run this part when the test is truthy. Check whether the first value is less than the second.
-
Line 10
return false; - Leave this function and send the value back to the caller.
-
Line 12
robot.energy -= stop.cost; - Update the existing value with the operation shown.
-
Line 13
robot.location = stop.name; - Store the result in the name or property to the left.
-
Line 14
if (stop.part !== "" && !robot.toolkit.includes(stop.part)) { - Run this part when the test is truthy. JavaScript evaluates the right side only when the left side is truthy. The exclamation and two equals signs check whether values differ without type conversion.
-
Line 15
robot.toolkit.push(stop.part); - Add entries to the end of the array and return its new length.
-
Line 17
return true; - Leave this function and send the value back to the caller.
-
Line 20
console.log("Pip sets off from Central Hub."); - Show a message or value in the output. Use it to watch the game work.
-
Line 21
for (const stop of route) { - Take one value at a time from this sequence and run the body with it.
Lines 22–33
-
Line 22
if (!visit(pip, stop)) { - Run this part when the test is truthy. The exclamation mark reverses a true-or-false test. Call visit with the values inside the parentheses.
-
Line 23
console.log(`Blocked before ${stop.name}.`); - Show a message or value in the output. Use it to watch the game work.
-
Line 24
break; - Leave this loop or switch immediately.
-
Line 26
console.log(`Reached ${pip.location}. Energy: ${pip.energy}.`); - Show a message or value in the output. Use it to watch the game work.
-
Line 29
const hasParts = pip.toolkit.includes("wind cell") && pip.toolkit.includes("wind lens"); - Set up hasParts. Keep this name attached to the same value. JavaScript evaluates the right side only when the left side is truthy. Check whether the collection contains the supplied item or text.
-
Line 30
if (pip.location === "Beacon" && hasParts) { - Run this part when the test is truthy. JavaScript evaluates the right side only when the left side is truthy. The triple equals compares without converting the value’s type.
-
Line 31
console.log("Beacon restored! The cloud creature can fly home."); - Show a message or value in the output. Use it to watch the game work.
-
Line 32
else { - Use this alternative when the earlier test did not pass.
-
Line 33
console.log("Rescue paused. Pip needs both parts and a route to the Beacon."); - Show a message or value in the output. Use it to watch the game work.
Mission challenge
Add a Supply Dock stop before the Beacon that costs one unit. Decide what to put in its part field if nobody left a part there, then check the new final energy.
Change one piece at a time. Run the checkpoint again and compare what you expected with what actually happened.