Quizz
The full text of this chapter, to read or revisit. To practise, do the exercises.
βΆ Do the exercisesExercise 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);MetachuZap15undefinedHint : 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);31undefinedHint : It's still an ordinary array.
Exercise 3 β Quiz: The Loop Variable
In `team.forEach((cdm) => ...)` where `team` holds objects, what is `cdm`?
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]);ThunderSpark2undefinedHint : 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);31undefinedHint : 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);21undefinedHint : 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]);ThunderSparkZapundefinedHint : 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)));3214Hint : 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]);undefinednullHint : 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);undefinednull"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]);undefined0"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");unknownundefined"name"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);0100undefinedHint : 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);1000undefinedHint : 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);[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);23165Hint : 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);["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);6025undefinedHint : 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);45undefinedHint : Did anything modify the original objects?
Exercise 25 β Quiz: Which Tool?
You want the one object whose name is `"Zap"`. Which method?
team.find(...)team.filter(...)team.map(...)team.forEach(...)Hint : One method returns a single element, the others return a list or nothing.