Quizz

The full text of this chapter, to read or revisit. To practise, do the exercises.

β–Ά Do the exercises

Exercise 1 β€” Quiz: Element Then Property

What does this code print?

const team = [ { name: "Zap", level: 4 }, { name: "Metachu", level: 15 } ]; console.log(team[1].name);
Options
β€’ Metachu
β€’ Zap
β€’ 15
β€’ undefined

Hint : Pick the position first, then the key.

Exercise 2 β€” Quiz: Length of the List

What does this code print?

const shop = [ { name: "Potion" }, { name: "Map" }, { name: "Rope" } ]; console.log(shop.length);
Options
β€’ 3
β€’ 1
β€’ undefined
β€’ An error

Hint : It's still an ordinary array.

Exercise 3 β€” Quiz: The Loop Variable

In `team.forEach((cdm) => ...)` where `team` holds objects, what is `cdm`?

Options
β€’ One object of the list
β€’ Its index
β€’ Its name property
β€’ The whole list

Hint : The callback receives the elements themselves.

Exercise 4 β€” Quiz: Reach a Property

Print the level of the first member.

Hint : Position first, then key.

Exercise 5 β€” Quiz: Into the Inner Array

What does this code print?

const zap = { name: "Zap", moves: ["Spark", "Thunder"] }; console.log(zap.moves[1]);
Options
β€’ Thunder
β€’ Spark
β€’ 2
β€’ undefined

Hint : Reach the array, then a position in it.

Exercise 6 β€” Quiz: Count Inside

What does this code print?

const bag = { items: ["Potion", "Map", "Rope"] }; console.log(bag.items.length);
Options
β€’ 3
β€’ 1
β€’ undefined
β€’ An error

Hint : The property holds a real array.

Exercise 7 β€” Quiz: Method on Its Own Array

What does this code print?

const bag = { items: ["Potion"], add(x) { this.items.push(x); } }; bag.add("Map"); console.log(bag.items.length);
Options
β€’ 2
β€’ 1
β€’ undefined
β€’ An error

Hint : The method adds to the object's own list.

Exercise 8 β€” Quiz: Push Into It

Complete the method so it adds to the object's own list.

Hint : Refer to the object without naming it, then add at the end.

Exercise 9 β€” Quiz: Two Levels Deep

What does this code print?

const trainer = { team: [ { name: "Zap", moves: ["Spark", "Thunder"] } ] }; console.log(trainer.team[0].moves[1]);
Options
β€’ Thunder
β€’ Spark
β€’ Zap
β€’ undefined

Hint : Follow the path from left to right.

Exercise 10 β€” Quiz: Nested Loops

How many lines does the inner loop print?

const team = [ { moves: ["a", "b"] }, { moves: ["c"] } ]; team.forEach((c) => c.moves.forEach((m) => console.log(m)));
Options
β€’ 3
β€’ 2
β€’ 1
β€’ 4

Hint : Add up the sizes of the inner arrays.

Exercise 11 β€” Quiz: Broken Chain

What happens on the last line?

const trainer = { name: "Sam" }; console.log(trainer.team[0]);
Options
β€’ An error
β€’ undefined
β€’ Nothing
β€’ null

Hint : Reading a position of something that doesn't exist is not allowed.

Exercise 12 β€” Quiz: Dive Safely

Print the first team member only if the team exists.

Hint : Test the link you are about to follow.

Exercise 13 β€” Quiz: Optional Chaining

What does this code print?

const trainer = { name: "Sam" }; console.log(trainer.starter?.name);
Options
β€’ undefined
β€’ An error
β€’ null
β€’ "Sam"

Hint : The question mark stops the chain quietly.

Exercise 14 β€” Quiz: Optional Index

What does this code print?

const trainer = { name: "Sam" }; console.log(trainer.team?.[0]);
Options
β€’ undefined
β€’ An error
β€’ 0
β€’ "Sam"

Hint : The same protection works before a position too.

Exercise 15 β€” Quiz: Nullish Fallback

What does this code print?

const trainer = {}; console.log(trainer.name ?? "unknown");
Options
β€’ unknown
β€’ undefined
β€’ "name"
β€’ An error

Hint : The left side is missing, so the fallback takes over.

Exercise 16 β€” Quiz: Zero and ??

What does this code print?

const cdm = { hp: 0 }; console.log(cdm.hp ?? 100);
Options
β€’ 0
β€’ 100
β€’ undefined
β€’ An error

Hint : Only two values in the language trigger this operator.

Exercise 17 β€” Quiz: Zero and ||

What does this code print?

const cdm = { hp: 0 }; console.log(cdm.hp || 100);
Options
β€’ 100
β€’ 0
β€’ undefined
β€’ An error

Hint : This operator reacts to every falsy value, not just the empty ones.

Exercise 18 β€” Quiz: Safe and Sound

Print the number of team members, or 0 when there is no team.

Hint : One symbol survives the missing link, the other replaces the empty answer.

Exercise 19 β€” Quiz: map on Objects

What does `r` contain?

const team = [ { name: "Zap", level: 4 }, { name: "Metachu", level: 15 } ]; const r = team.map((c) => c.level);
Options
β€’ [4, 15]
β€’ ["Zap", "Metachu"]
β€’ 19
β€’ []

Hint : One value comes out per object.

Exercise 20 β€” Quiz: filter on Objects

What does this code print?

const team = [ { hp: 40 }, { hp: 0 }, { hp: 25 } ]; console.log(team.filter((c) => c.hp > 0).length);
Options
β€’ 2
β€’ 3
β€’ 1
β€’ 65

Hint : Count the objects that pass the test.

Exercise 21 β€” Quiz: Filter Then Map

What does `r` contain?

const team = [ { name: "Zap", level: 4 }, { name: "Metachu", level: 15 } ]; const r = team .filter((c) => c.level >= 10) .map((c) => c.name);
Options
β€’ ["Metachu"]
β€’ ["Zap", "Metachu"]
β€’ [15]
β€’ []

Hint : Select first, then extract from what remains.

Exercise 22 β€” Quiz: Names of the Ready

Keep the members with HP above 0, then take their names.

Hint : First select the objects, then transform them.

Exercise 23 β€” Quiz: find an Object

What does this code print?

const team = [ { name: "Zap", hp: 25 }, { name: "Metachu", hp: 60 } ]; console.log(team.find((c) => c.name === "Metachu").hp);
Options
β€’ 60
β€’ 25
β€’ undefined
β€’ An error

Hint : The whole object comes back, so its properties are available.

Exercise 24 β€” Quiz: New List, Old List

What does this code print?

const team = [{ level: 4 }]; const up = team.map((c) => ({ ...c, level: c.level + 1 })); console.log(team[0].level);
Options
β€’ 4
β€’ 5
β€’ undefined
β€’ An error

Hint : Did anything modify the original objects?

Exercise 25 β€” Quiz: Which Tool?

You want the one object whose name is `"Zap"`. Which method?

Options
β€’ team.find(...)
β€’ team.filter(...)
β€’ team.map(...)
β€’ team.forEach(...)

Hint : One method returns a single element, the others return a list or nothing.

Back to the course