Quizz

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

β–Ά Do the exercises

Exercise 1 β€” Quiz: The First Letters

What does this code print?

console.log("Aquachu".slice(0, 4));
Options
β€’ Aqua
β€’ Aquac
β€’ quac
β€’ chu

Hint : The end index marks where to stop, without joining.

Exercise 2 β€” Quiz: To the End

What does this code print?

console.log("Aquachu".slice(4));
Options
β€’ chu
β€’ Aqua
β€’ achu
β€’ Aquachu

Hint : With one argument, the cut runs to the last character.

Exercise 3 β€” Quiz: From the End

What does this code print?

console.log("Metachu".slice(-3));
Options
β€’ chu
β€’ Met
β€’ Meta
β€’ achu

Hint : A negative number counts backwards.

Exercise 4 β€” Quiz: Original Untouched

What does this code print?

const name = "Zapchu"; name.slice(0, 3); console.log(name);
Options
β€’ Zapchu
β€’ Zap
β€’ ""
β€’ An error

Hint : A string can never be modified in place.

Exercise 5 β€” Quiz: Cut Three Letters

Keep the first three characters.

Hint : One method cuts a piece, and the end index is excluded.

Exercise 6 β€” Quiz: substring and Negatives

What does this code print?

console.log("Zapchu".substring(-3));
Options
β€’ Zapchu
β€’ chu
β€’ Zap
β€’ ""

Hint : One of the two cutting methods refuses negative numbers.

Exercise 7 β€” Quiz: Only the First

What does this code print?

console.log("a-a-a".replace("a", "b"));
Options
β€’ b-a-a
β€’ b-b-b
β€’ a-a-a
β€’ b

Hint : This method stops after one match.

Exercise 8 β€” Quiz: All of Them

What does this code print?

console.log("a-a-a".replaceAll("a", "b"));
Options
β€’ b-b-b
β€’ b-a-a
β€’ a-a-a
β€’ bbb

Hint : This one keeps going to the end of the text.

Exercise 9 β€” Quiz: Replaced by Nothing

What does this code print?

console.log("Z-a-p".replaceAll("-", ""));
Options
β€’ Zap
β€’ Z-a-p
β€’ Za-p
β€’ ""

Hint : Replacing with an empty string removes what was found.

Exercise 10 β€” Quiz: Fill the Template

Put `"Zap"` in place of `NAME`.

Hint : One method swaps a piece of text, and text needs quotes.

Exercise 11 β€” Quiz: Padded Left

What does this code print?

console.log("7".padStart(3, "0"));
Options
β€’ 007
β€’ 700
β€’ 7
β€’ 000

Hint : The method name says which side receives the filler.

Exercise 12 β€” Quiz: Padded Right

What does this code print?

console.log("7".padEnd(3, "0"));
Options
β€’ 700
β€’ 007
β€’ 7
β€’ 000

Hint : The other side this time.

Exercise 13 β€” Quiz: Too Long Already

What does this code print?

console.log("Aquachu".padStart(4, "*"));
Options
β€’ Aquachu
β€’ Aqua
β€’ ***Aquachu
β€’ *Aquachu

Hint : Padding can only make text longer.

Exercise 14 β€” Quiz: Padding a Number

`level` is the number `7`. Which line prints `007`?

Options
β€’ String(level).padStart(3, "0")
β€’ level.padStart(3, "0")
β€’ level.padEnd(3, "0")
β€’ String(level).padEnd(3, "0")

Hint : Padding belongs to strings, and the filler goes on the side numbers are read from.

Exercise 15 β€” Quiz: Three Digits

Display the score on three digits, filled with zeros.

Hint : Numbers are read from the right, and the filler is written as text.

Exercise 16 β€” Quiz: Repeated

What does this code print?

console.log("ab".repeat(3));
Options
β€’ ababab
β€’ abababab
β€’ ab
β€’ aaabbb

Hint : The whole piece is repeated, in order.

Exercise 17 β€” Quiz: Repeated Zero Times

What does this code print?

console.log("x".repeat(0).length);
Options
β€’ 0
β€’ 1
β€’ undefined
β€’ An error

Hint : Repeating nothing gives an empty result, not an error.

Exercise 18 β€” Quiz: Length After repeat

What does this code print?

console.log("abc".repeat(2).length);
Options
β€’ 6
β€’ 3
β€’ 2
β€’ 5

Hint : Multiply the length of the piece by the number of times.

Exercise 19 β€” Quiz: Draw a Line

Print a line of 10 dashes.

Hint : One method multiplies a piece of text the number of times you give it.

Exercise 20 β€” Quiz: Starts With?

What does this code print?

console.log("LOG: done".startsWith("LOG:"));
Options
β€’ true
β€’ false
β€’ LOG:
β€’ 0

Hint : Look at the first characters of the text.

Exercise 21 β€” Quiz: Ends With?

What does this code print?

console.log("Aquachu".endsWith("chu"));
Options
β€’ true
β€’ false
β€’ chu
β€’ 3

Hint : Look at the last characters of the text.

Exercise 22 β€” Quiz: Wrong Case

What does this code print?

console.log("Zapchu".startsWith("zap"));
Options
β€’ false
β€’ true
β€’ Zap
β€’ An error

Hint : The comparison is exact, letter by letter.

Exercise 23 β€” Quiz: Family Filter

Keep only the names ending with `"chu"`.

Hint : One method looks at the end, and the piece searched for is text.

Exercise 24 β€” Quiz: Chained Methods

What does this code print?

console.log("Aquachu".slice(0, 3).toUpperCase());
Options
β€’ AQU
β€’ Aqu
β€’ AQUACHU
β€’ aqu

Hint : Each step works on the result of the one before it.

Exercise 25 β€” Quiz: Which Method?

You want every level number shown on 3 digits, like `042`. Which method?

Options
β€’ padStart
β€’ padEnd
β€’ repeat
β€’ slice

Hint : Digits are read from the right, so the filler goes on the left.

Back to the course