Quizz

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

β–Ά Do the exercises

Exercise 1 β€” Quiz: Duplicates Dropped

What does this code print?

console.log(new Set(["a", "b", "a"]).size);
Options
β€’ 2
β€’ 3
β€’ 1
β€’ 0

Hint : Count the different values only.

Exercise 2 β€” Quiz: Counting a Set

Which property gives the number of items in a Set?

Options
β€’ .size
β€’ .length
β€’ .count
β€’ .total

Hint : 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);
Options
β€’ 2
β€’ 1
β€’ 0
β€’ An error

Hint : 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);
Options
β€’ 2
β€’ 3
β€’ 1
β€’ 0

Hint : 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"));
Options
β€’ false
β€’ true
β€’ undefined
β€’ -1

Hint : 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);
Options
β€’ 1
β€’ 2
β€’ 0
β€’ An error

Hint : 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"));
Options
β€’ 2
β€’ 1
β€’ "b"
β€’ undefined

Hint : 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"));
Options
β€’ undefined
β€’ null
β€’ 0
β€’ An error

Hint : 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);
Options
β€’ 1
β€’ 2
β€’ 9
β€’ 0

Hint : 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"));
Options
β€’ undefined
β€’ "Zap"
β€’ 5
β€’ An error

Hint : 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"]);
Options
β€’ "Zap"
β€’ undefined
β€’ 5
β€’ An error

Hint : An object turns every key into text.

Exercise 15 β€” Quiz: Counting an Object

Which expression gives the number of keys of an object `o`?

Options
β€’ Object.keys(o).length
β€’ o.size
β€’ o.length
β€’ o.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?

Options
β€’ Map
β€’ Object
β€’ Set
β€’ Array

Hint : 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); }
Options
β€’ 2
β€’ 3
β€’ 1
β€’ 0

Hint : 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); }
Options
β€’ a=1
β€’ 1=a
β€’ a=a
β€’ undefined

Hint : 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));
Options
β€’ 1 a
β€’ a 1
β€’ a a
β€’ 1 1

Hint : 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()]);
Options
β€’ a,b
β€’ 1,2
β€’ a,1,b,2
β€’ 2

Hint : 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?

Options
β€’ map
β€’ has
β€’ add
β€’ forEach

Hint : 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);
Options
β€’ 2
β€’ 1
β€’ 0
β€’ An error

Hint : 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);
Options
β€’ 1
β€’ 2
β€’ 0
β€’ An error

Hint : 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?

Options
β€’ Set
β€’ Map
β€’ Array
β€’ Object

Hint : One collection is built exactly around refusing duplicates.

Back to the course