JavaScript 05 · Decide whether Pip can launch

JavaScript lesson 5 of 50 · Beginner · About 25 minutes

Turn three checks into a decision about whether to launch towards the Beacon.

A distress signal lights up Pip’s antenna at Sky Dock while two cloud creatures wait on a distant platform.
Mission Write readable comparisons and choose an if, else if or else branch.

Get ready

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

Check energy. Compare the battery with the launch cost. → Check the gate and map. Combine the yes-or-no answers. → Choose a branch. Print the first matching launch message.
Check energy → Check the gate and map → Choose a branch

Understand the JavaScript in this lesson

Language to look for booleans, comparison operators, strict equality, and, not, if, else if, else.

1. Set up a test mission

Write the starting data first. true is a boolean, one of the two values that describe a yes-or-no result. false is the other. Leave both keywords unquoted because quoted words become strings. Each name sounds like something you can check about the mission.

const energy = 8;
const launchCost = 6;
const gate = "open";
const hasMap = true;

2. Make each comparison tell you something

The operator >= means greater than or equal to. It allows launch when the energy is exactly the cost. The operator === checks that both sides have the same type and value. It compares the gate text with "open". Between checks, && means and. All three conditions must be true for ready to be true.

const enoughEnergy = energy >= launchCost;
const gateIsOpen = gate === "open";
const ready = enoughEnergy && gateIsOpen && hasMap;

3. Choose one message

Write the if block below the comparisons. JavaScript checks the conditions in parentheses from top to bottom. When one is true, it runs only that branch and skips the remaining branches of this decision. Braces show which lines belong to a branch, and indentation makes them easier to read. The ! symbol means not, so !enoughEnergy is true when the rover needs a charge. The final else has no condition. The only remaining reason here is the missing map.

if (ready) {
  console.log("Launch approved.");
} else if (!enoughEnergy) {
  console.log("Charge the battery first.");
} else if (!gateIsOpen) {
  console.log("Open the launch gate first.");
} else {
  console.log("Collect the flight map first.");
}
Follow what happens step by step
  1. Compare 8 with 6
    enoughEnergy is true.Why 8 is at least the required energy.
  2. Check the gate and the map
    gateIsOpen and hasMap are both true.Why The gate text matches exactly and hasMap began as true.
  3. Read the branching block
    Launch approved.Why The first branch wins because ready is true.

Predict before you run Set energy to 5 and gate to closed. Which message is printed?

Reveal the answer

Charge the battery first. Both checks fail, but the low-energy check comes before the closed-gate check.

Complete checkpoint

JavaScript · the instructions

const energy = 8;
const launchCost = 6;
const gate = "open";
const hasMap = true;

const enoughEnergy = energy >= launchCost;
const gateIsOpen = gate === "open";
const ready = enoughEnergy && gateIsOpen && hasMap;

if (ready) {
  console.log("Launch approved.");
} else if (!enoughEnergy) {
  console.log("Charge the battery first.");
} else if (!gateIsOpen) {
  console.log("Open the launch gate first.");
} else {
  console.log("Collect the flight map first.");
}

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–13
Line 1 const energy = 8;
Set up energy. Keep this name attached to the same value.
Line 2 const launchCost = 6;
Set up launchCost. Keep this name attached to the same value.
Line 3 const gate = "open";
Set up gate. Keep this name attached to the same value.
Line 4 const hasMap = true;
Set up hasMap. Keep this name attached to the same value.
Line 6 const enoughEnergy = energy >= launchCost;
Set up enoughEnergy. Keep this name attached to the same value. Check whether the first is greater than or equal to the second.
Line 7 const gateIsOpen = gate === "open";
Set up gateIsOpen. Keep this name attached to the same value. The triple equals compares without converting the value’s type.
Line 8 const ready = enoughEnergy && gateIsOpen && hasMap;
Set up ready. Keep this name attached to the same value. JavaScript evaluates the right side only when the left side is truthy.
Line 10 if (ready) {
Run this part when the test is truthy.
Line 11 console.log("Launch approved.");
Show a message or value in the output. Use it to watch the game work.
Line 12 else
Use this alternative when the earlier test did not pass.
Line 12 if (!enoughEnergy) {
Run this part when the test is truthy. The exclamation mark reverses a true-or-false test.
Line 13 console.log("Charge the battery first.");
Show a message or value in the output. Use it to watch the game work.
Lines 14–17
Line 14 else
Use this alternative when the earlier test did not pass.
Line 14 if (!gateIsOpen) {
Run this part when the test is truthy. The exclamation mark reverses a true-or-false test.
Line 15 console.log("Open the launch gate first.");
Show a message or value in the output. Use it to watch the game work.
Line 16 else {
Use this alternative when the earlier test did not pass.
Line 17 console.log("Collect the flight map first.");
Show a message or value in the output. Use it to watch the game work.

Mission challenge

Set hasMap to false while keeping the other checks true. Run the code and record the new console message.

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