JavaScript 06 · Pack the rescue toolkit

JavaScript lesson 6 of 50 · Beginner · About 25 minutes

Keep Pip’s gear in an ordered list and check whether a new part is already packed.

A distress signal lights up Pip’s antenna at Sky Dock while two cloud creatures wait on a distant platform.
Mission Use an array, read an item by position and add a missing item.

Get ready

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

Build the bag. Store the tools in a list. → Check for duplicates. Use includes before adding a new part. → Report the bag. Read the last item and the updated count.
Build the bag → Check for duplicates → Report the bag

Understand the JavaScript in this lesson

Language to look for arrays, indexing from zero, length, includes(), push(), const with arrays.

1. Give the bag an order

Square brackets around a comma-separated group of values make an array, an ordered list of items. Start with a torch and a spanner. When square brackets go after a name, they can read a numbered position instead. JavaScript starts at position zero, so toolkit[0] reads the torch. Use .length without parentheses because it is a count property that you read.

const toolkit = ["torch", "spanner"];
const firstTool = toolkit[0];
console.log("First tool:", firstTool);
console.log("Tools before pickup:", toolkit.length);

2. Pick up only a missing object

Find the discovery in the Garden. The includes() method answers with true or false. Here ! turns the question into “is the part absent?”. The lines inside the braces run when the answer is true. The push() method adds the discovery to the end of the same array. The name toolkit still refers to the same array after push(), which is why the declaration can use const.

const discovery = "wind lens";
if (!toolkit.includes(discovery)) {
  toolkit.push(discovery);
}

3. Read the last slot

Add the final reports and run them. The list now has three items numbered zero, one and two. Subtract one from the count to find the last index. The expression toolkit[toolkit.length - 1] gets the count, works out the right index, then reads that position. The final membership check confirms the part was packed.

console.log("Tools after pickup:", toolkit.length);
console.log(`Newest tool: ${toolkit[toolkit.length - 1]}.`);
console.log("Lens packed:", toolkit.includes("wind lens"));
Follow what happens step by step
  1. Create the toolkit array
    torch, then spannerWhy Array values keep their order.
  2. Look for wind lens
    The membership check gives false and Pip adds it.Why The not operator makes the condition true.
  3. Read the last index
    There are three items and index 2 gives wind lens.Why The last index is one less than the count.

Predict before you run If the toolkit already starts with wind lens, will the program pack a second lens?

Reveal the answer

No. The membership check returns true, so the negated condition is false and JavaScript skips push().

Complete checkpoint

JavaScript · the instructions

const toolkit = ["torch", "spanner"];
const firstTool = toolkit[0];
console.log("First tool:", firstTool);
console.log("Tools before pickup:", toolkit.length);

const discovery = "wind lens";
if (!toolkit.includes(discovery)) {
  toolkit.push(discovery);
}

console.log("Tools after pickup:", toolkit.length);
console.log(`Newest tool: ${toolkit[toolkit.length - 1]}.`);
console.log("Lens packed:", toolkit.includes("wind lens"));

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 toolkit = ["torch", "spanner"];
Set up toolkit. Keep this name attached to the same value. Square brackets create an ordered array. Its first position is 0.
Line 2 const firstTool = toolkit[0];
Set up firstTool. Keep this name attached to the same value. Work out what is inside the brackets first, then use it as the key or index.
Line 3 console.log("First tool:", firstTool);
Show a message or value in the output. Use it to watch the game work.
Line 4 console.log("Tools before pickup:", toolkit.length);
Show a message or value in the output. Use it to watch the game work.
Line 6 const discovery = "wind lens";
Set up discovery. Keep this name attached to the same value.
Line 7 if (!toolkit.includes(discovery)) {
Run this part when the test is truthy. The exclamation mark reverses a true-or-false test. Check whether the collection contains the supplied item or text.
Line 8 toolkit.push(discovery);
Add entries to the end of the array and return its new length.
Line 11 console.log("Tools after pickup:", toolkit.length);
Show a message or value in the output. Use it to watch the game work.
Line 12 console.log(`Newest tool: ${toolkit[toolkit.length - 1]}.`);
Show a message or value in the output. Use it to watch the game work.
Line 13 console.log("Lens packed:", toolkit.includes("wind lens"));
Show a message or value in the output. Use it to watch the game work.

Mission challenge

Change discovery to compass. Predict the newest item and whether the final check for wind lens is true or false.

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