Quizz
The full text of this chapter, to read or revisit. To practise, do the exercises.
βΆ Do the exercisesExercise 1 β Quiz: Duplicates Dropped
What does this code print?
console.log(new Set(["a", "b", "a"]).size);2310Hint : Count the different values only.
Exercise 2 β Quiz: Counting a Set
Which property gives the number of items in a Set?
.size.length.count.totalHint : A Set is not an array, so it doesn't count like one.
Exercise 3 β Quiz: Number or Text
What does this code print?
console.log(new Set([1, "1"]).size);210Hint : A Set compares values exactly, type included.
Exercise 4 β Quiz: Remove Duplicates
Build a duplicate-free array from `names`.
Hint : One symbol pours a collection out, and one collection refuses repeats.
Exercise 5 β Quiz: After add
What does this code print?
const s = new Set(["a"]);
s.add("a");
s.add("b");
console.log(s.size);2310Hint : Adding a value already present changes nothing.
Exercise 6 β Quiz: Set has
What does this code print?
const s = new Set(["Zap"]);
console.log(s.has("Metachu"));falsetrueundefined-1Hint : This method answers with a boolean.
Exercise 7 β Quiz: After delete
What does this code print?
const s = new Set(["a", "b"]);
s.delete("a");
console.log(s.size);120Hint : One value was taken out of the collection.
Exercise 8 β Quiz: Record and Check
Add Metachu, then check it is there.
Hint : A Set uses one method to insert and another to ask a yes-or-no question.
Exercise 9 β Quiz: Reading a Map
What does this code print?
const m = new Map([["a", 1], ["b", 2]]);
console.log(m.get("b"));21"b"undefinedHint : The key selects which value comes back.
Exercise 10 β Quiz: Missing Key
What does this code print?
const m = new Map([["a", 1]]);
console.log(m.get("z"));undefinednull0Hint : Asking for a key that was never set is not an error.
Exercise 11 β Quiz: Same Key Twice
What does this code print?
const m = new Map();
m.set("a", 1);
m.set("a", 9);
console.log(m.size);1290Hint : A key is unique inside a Map.
Exercise 12 β Quiz: Store and Read
Store Lea's score, then read it back.
Hint : One method stores a pair, another brings a value back.
Exercise 13 β Quiz: Map Key Types
What does this code print?
const m = new Map();
m.set(5, "Zap");
console.log(m.get("5"));undefined"Zap"5Hint : A Map compares keys exactly, type included.
Exercise 14 β Quiz: Object Key Types
What does this code print?
const o = {};
o[5] = "Zap";
console.log(o["5"]);"Zap"undefined5Hint : An object turns every key into text.
Exercise 15 β Quiz: Counting an Object
Which expression gives the number of keys of an object `o`?
Object.keys(o).lengtho.sizeo.lengtho.count()Hint : An object has no built-in counter of its own.
Exercise 16 β Quiz: Which Collection?
You need keys that are numbers and must stay numbers. Which do you pick?
MapObjectSetArrayHint : Only one of them keeps a key's type intact.
Exercise 17 β Quiz: Looping a Set
How many lines does this print?
const s = new Set(["a", "b", "a"]);
for (let v of s) {
console.log(v);
}2310Hint : A Set never holds the same value twice.
Exercise 18 β Quiz: Looping a Map
What does this code print?
const m = new Map([["a", 1]]);
for (let [k, v] of m) {
console.log(k + "=" + v);
}a=11=aa=aundefinedHint : With for...of, the pair arrives with the key in front.
Exercise 19 β Quiz: Map forEach Order
What does this code print?
const m = new Map([["a", 1]]);
m.forEach((x, y) => console.log(x + " " + y));1 aa 1a a1 1Hint : On a Map, the callback does not receive the key first.
Exercise 20 β Quiz: Just the Keys
What does this code print?
const m = new Map([["a", 1], ["b", 2]]);
console.log([...m.keys()]);a,b1,2a,1,b,22Hint : One method returns one side of the pairs.
Exercise 21 β Quiz: Total the Values
Add up every value of the Map.
Hint : You need one side of the pairs, then the method that boils a list into one number.
Exercise 22 β Quiz: Set Methods Missing
Which of these does a Set NOT have?
maphasaddforEachHint : Three of them belong to a Set; one belongs to arrays.
Exercise 23 β Quiz: Copying a Set
What does this code print?
const a = new Set(["x"]);
const b = a;
b.add("y");
console.log(a.size);210Hint : Assigning does not build a second collection.
Exercise 24 β Quiz: Set From a Set
What does this code print?
const a = new Set(["x"]);
const b = new Set(a);
b.add("y");
console.log(a.size);120Hint : Building a new collection from the old one separates them.
Exercise 25 β Quiz: The Right Tool
You want to know whether a name was already met, with no duplicates kept. Which collection?
SetMapArrayObjectHint : One collection is built exactly around refusing duplicates.