Quizz
The full text of this chapter, to read or revisit. To practise, do the exercises.
βΆ Do the exercisesExercise 1 β Quiz: Valid Object
Which line creates a valid object?
const c = { name: "Zap", level: 3 };const c = [ name: "Zap", level: 3 ];const c = { name = "Zap", level = 3 };const c = { "Zap", 3 };Hint : Curly braces, and a colon between each key and value.
Exercise 2 β Quiz: Dot Read
What does this code print?
const zap = { name: "Zap", level: 3 };
console.log(zap.level);3"Zap""level"undefinedHint : The key after the dot selects the value.
Exercise 3 β Quiz: Bracket Read
What does this code print?
const zap = { name: "Zap", level: 3 };
console.log(zap["name"]);"Zap"undefined"name"Hint : Brackets with a string do the same as the dot.
Exercise 4 β Quiz: Variable Key
What does this code print?
const zap = { hp: 45, speed: 30 };
let stat = "speed";
console.log(zap[stat]);3045undefined"speed"Hint : The brackets look inside the variable to find the key.
Exercise 5 β Quiz: Dot and Variable
What does this code print?
const zap = { hp: 45, speed: 30 };
let stat = "speed";
console.log(zap.stat);undefined3045Hint : The dot takes the word literally.
Exercise 6 β Quiz: Read Both Ways
Print the name with the dot and the level with the brackets.
Hint : Two notations, same result.
Exercise 7 β Quiz: Missing Property
What does this code print?
const zap = { name: "Zap" };
console.log(zap.hp);undefined0nullHint : A key that isn't there gives the usual "nothing" value.
Exercise 8 β Quiz: Modified
What does this code print?
const zap = { level: 3 };
zap.level += 2;
console.log(zap.level);532undefinedHint : The property is updated in place.
Exercise 9 β Quiz: Created
What does this code print?
const zap = { name: "Zap" };
zap.hp = 45;
console.log(zap.hp);45undefined"hp"Hint : Assigning to a new key adds it.
Exercise 10 β Quiz: Add a Nickname
Add a `nickname` property worth `"Zappy"`.
Hint : Same syntax as changing an existing property.
Exercise 11 β Quiz: Deleted
What does this code print?
const zap = { name: "Zap", bonus: 10 };
delete zap.bonus;
console.log(zap.bonus);undefined100Hint : After deletion, the key is simply absent.
Exercise 12 β Quiz: Key Exists?
What does this code print?
const zap = { name: "Zap", bonus: 10 };
delete zap.bonus;
console.log("bonus" in zap);falsetrueundefined10Hint : This operator answers whether the key is still there.
Exercise 13 β Quiz: Method Call
Which line runs the `speak` method of `aquachu`?
aquachu.speak();aquachu.speak;speak(aquachu);aquachu[speak];Hint : Through the object, with parentheses.
Exercise 14 β Quiz: Method Result
What does this code print?
const calc = {
add(a, b) {
return a + b;
}
};
console.log(calc.add(4, 6));10undefined46a + bHint : A method returns like any function.
Exercise 15 β Quiz: Declare a Method
Give the object a `greet` method that prints `Hi!`, then call it.
Hint : Short syntax: just the name and parentheses in the object.
Exercise 16 β Quiz: this Reads
What does this code print?
const zap = {
name: "Zap",
intro() {
return "I am " + this.name;
}
};
console.log(zap.intro());I am ZapI am this.nameI am undefinedHint : Inside the method, this is the object before the dot.
Exercise 17 β Quiz: this Writes
What does the last line print?
const zap = {
level: 1,
up() {
this.level += 3;
}
};
zap.up();
console.log(zap.level);413undefinedHint : The method changes the object's own property.
Exercise 18 β Quiz: Use this
Complete the method so it works for any object it belongs to.
Hint : Don't name the variable inside the method.
Exercise 19 β Quiz: for...in Gives
What does this code print?
const stats = { hp: 45, speed: 30 };
for (let k in stats) {
console.log(k);
}hp then speed45 then 300 then 1undefinedHint : This loop visits names, not values.
Exercise 20 β Quiz: Value in the Loop
Inside `for (let k in stats)`, which expression gives the value of the current property?
stats[k]stats.kstats["k"]k.statsHint : The key is in a variable.
Exercise 21 β Quiz: Sum the Stats
Add every value of the object into `total`.
Hint : The object loop keyword, then the loop variable as the key.
Exercise 22 β Quiz: Nested Read
What does this code print?
const trainer = {
starter: { name: "Zap", level: 3 }
};
console.log(trainer.starter.name);"Zap"undefined"starter"Hint : One dot per level.
Exercise 23 β Quiz: Object in Array
What does this code print?
const team = [
{ name: "Aquachu" },
{ name: "Pyrochu" }
];
console.log(team[1].name);"Pyrochu""Aquachu"undefined1Hint : First the index, then the property.
Exercise 24 β Quiz: Broken Chain
What happens on the last line?
const trainer = { name: "Sam" };
console.log(trainer.rival.name);undefined"Sam"Hint : Reading a property of undefined is not allowed.
Exercise 25 β Quiz: typeof Object
What does this code print?
const zap = { name: "Zap" };
console.log(typeof zap);"object""zap""array""string"Hint : Same family as arrays.