Quizz

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

β–Ά Do the exercises

Exercise 1 β€” Quiz: First Two

What does this code print?

const [a, b] = [10, 20, 30]; console.log(b);
Options
β€’ 20
β€’ 10
β€’ 30
β€’ undefined

Hint : Each name takes the position it occupies.

Exercise 2 β€” Quiz: Too Many Names

What does this code print?

const [a, b, c] = [1, 2]; console.log(c);
Options
β€’ undefined
β€’ 2
β€’ 0
β€’ An error

Hint : There is no element at that position.

Exercise 3 β€” Quiz: The Skipped Slot

What does this code print?

const [, second] = ["Zap", "Metachu"]; console.log(second);
Options
β€’ Metachu
β€’ Zap
β€’ undefined
β€’ An error

Hint : The empty slot still counts as a position.

Exercise 4 β€” Quiz: Unpack Two

Put the first two levels into `first` and `second`.

Hint : An array is unpacked with the brackets it is written with.

Exercise 5 β€” Quiz: By Name

What does this code print?

const o = { a: 1, b: 2 }; const { b, a } = o; console.log(a);
Options
β€’ 1
β€’ 2
β€’ undefined
β€’ An error

Hint : With objects, the order you write the names changes nothing.

Exercise 6 β€” Quiz: Wrong Key

What does this code print?

const zap = { name: "Zap", level: 4 }; const { lvl } = zap; console.log(lvl);
Options
β€’ undefined
β€’ 4
β€’ "Zap"
β€’ An error

Hint : The link is made by the exact name of the key.

Exercise 7 β€” Quiz: Renamed Variable

What does this code print?

const item = { price: 30 }; const { price: cost } = item; console.log(cost);
Options
β€’ 30
β€’ "price"
β€’ undefined
β€’ An error

Hint : One side reads the property, the other names your variable.

Exercise 8 β€” Quiz: Rename It

Extract `hp` into a variable named `health`.

Hint : Renaming uses one symbol, with the key first.

Exercise 9 β€” Quiz: Destructured Parameter

What does this code print?

function f({ name }) { return name; } console.log(f({ name: "Zap", level: 4 }));
Options
β€’ Zap
β€’ 4
β€’ undefined
β€’ An error

Hint : The pattern picks one property out of the received object.

Exercise 10 β€” Quiz: Default Applied

What does this code print?

const cdm = { name: "Zap" }; const { level = 1 } = cdm; console.log(level);
Options
β€’ 1
β€’ undefined
β€’ 0
β€’ An error

Hint : The key is missing from the object.

Exercise 11 β€” Quiz: Default Ignored

What does this code print?

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

Hint : Only a missing value triggers the fallback.

Exercise 12 β€” Quiz: Add a Fallback

Make `type` fall back to `"normal"` when it is missing.

Hint : A fallback is written like an assignment, and text needs quotes.

Exercise 13 β€” Quiz: Spread Length

What does this code print?

const a = [1, 2]; console.log([...a, 3].length);
Options
β€’ 3
β€’ 2
β€’ 4
β€’ 1

Hint : The spread pours the elements out one by one.

Exercise 14 β€” Quiz: Without the Dots

What does this code print?

const a = [1, 2]; console.log([a, 3].length);
Options
β€’ 2
β€’ 3
β€’ 4
β€’ 1

Hint : Without the dots, the whole array counts as a single element.

Exercise 15 β€” Quiz: Merged Arrays

What does this code print?

const a = [1]; const b = [2, 3]; console.log([...a, ...b]);
Options
β€’ 1,2,3
β€’ 1,2
β€’ 2,3,1
β€’ 3

Hint : Each spread keeps the order of its own elements.

Exercise 16 β€” Quiz: Pour It Out

Build a new array with every member of `team`, then Metachu.

Hint : One symbol pours the array out, and text needs quotes.

Exercise 17 β€” Quiz: Object Override

What does this code print?

const o = { hp: 40 }; const n = { ...o, hp: 10 }; console.log(n.hp);
Options
β€’ 10
β€’ 40
β€’ 50
β€’ undefined

Hint : Read the braces from left to right.

Exercise 18 β€” Quiz: Override Too Early

What does this code print?

const o = { hp: 40 }; const n = { hp: 10, ...o }; console.log(n.hp);
Options
β€’ 40
β€’ 10
β€’ 50
β€’ undefined

Hint : The value written last is the one that survives.

Exercise 19 β€” Quiz: Rest Count

What does this code print?

function f(...args) { return args.length; } console.log(f(1, 2, 3));
Options
β€’ 3
β€’ 1
β€’ 0
β€’ undefined

Hint : Every argument lands in the same array.

Exercise 20 β€” Quiz: Rest After a Parameter

What does this code print?

function f(first, ...rest) { return rest.length; } console.log(f("a", "b", "c", "d"));
Options
β€’ 3
β€’ 4
β€’ 1
β€’ 0

Hint : The named parameter is served first.

Exercise 21 β€” Quiz: Empty Rest

What does this code print?

function f(...args) { return args.length; } console.log(f());
Options
β€’ 0
β€’ undefined
β€’ 1
β€’ An error

Hint : With nothing to gather, you still get an array.

Exercise 22 β€” Quiz: Gather Them

Make the function accept any number of numbers and print how many.

Hint : One symbol gathers, and an array is measured as usual.

Exercise 23 β€” Quiz: Shared Array

What does this code print?

const a = [1, 2]; const b = a; b.push(3); console.log(a.length);
Options
β€’ 3
β€’ 2
β€’ 1
β€’ 0

Hint : How many arrays actually exist here?

Exercise 24 β€” Quiz: Copied Number

What does this code print?

let a = 5; let b = a; b = 99; console.log(a);
Options
β€’ 5
β€’ 99
β€’ undefined
β€’ An error

Hint : Simple values don't behave like arrays and objects.

Exercise 25 β€” Quiz: How Deep?

What does this code print?

const a = { stats: { hp: 50 } }; const b = { ...a }; b.stats.hp = 5; console.log(a.stats.hp);
Options
β€’ 5
β€’ 50
β€’ undefined
β€’ An error

Hint : A spread only reaches the first level.

Back to the course