Quizz

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

β–Ά Do the exercises

Exercise 1 β€” String Immutability

What happens when you try to change a character in a string?

let name = "Aquachu"; name[0] = "E"; console.log(name);
Options
β€’ "Aquachu" - Strings cannot be modified
β€’ "Equachu"
β€’ An error is thrown
β€’ undefined

Hint : Think about what happens when you try to modify a string directly. Does JavaScript allow you to change individual characters?

Exercise 2 β€” Concatenation with Numbers

What will this code output?

let level = 10; let message = "Level " + level + 5; console.log(message);
Options
β€’ "Level 105"
β€’ "Level 15"
β€’ 15
β€’ An error occurs

Hint : When you use + with a string, what type does everything become? The operations are done from left to right.

Exercise 3 β€” Build a Template Literal

Complete this template literal to display: Level 25

Hint : What special character wraps template literals? And what do you put around a variable name to insert it?

Exercise 4 β€” Method Chaining Result

What will this code output?

let name = " PYROCHU "; let result = name.trim().toLowerCase(); console.log(result);
Options
β€’ "pyrochu"
β€’ " pyrochu "
β€’ "PYROCHU"
β€’ " PYROCHU "

Hint : Method chaining applies operations one after another. Think about what each method does and in what order.

Exercise 5 β€” Match Use Case to Method

Match each use case with the best string method:

Items to match
β€’ Check if an email contains "@"
β€’ Get the first 3 letters of a name
β€’ Remove user input spaces
β€’ Convert "25" to a number
β€’ Make username all lowercase
Definitions
β€’ .includes()
β€’ .slice()
β€’ .trim()
β€’ parseInt()
β€’ .toLowerCase()

Exercise 6 β€” Empty String Behavior

What is the .length of an empty string?

let empty = ""; console.log(empty.length);
Options
β€’ 0
β€’ 1
β€’ undefined
β€’ null

Hint : How many characters are between the two quote marks?

Exercise 7 β€” Dynamic String Extraction

Extract the last 3 characters of any string without knowing its length:

Hint : Which method lets you count backwards from the end? What number represents the third-to-last position?

Exercise 8 β€” Spaces Count Too!

What is the length of this string?

let message = "Hi "; console.log(message.length);
Options
β€’ 3
β€’ 2
β€’ 4
β€’ undefined

Hint : Remember that spaces are characters too. Count everything between the quotes!

Exercise 9 β€” Parsing Non-Numeric Strings

What happens when you try to parse a non-numeric string?

let name = "Aquachu"; let number = parseInt(name); console.log(number);
Options
β€’ NaN
β€’ 0
β€’ null
β€’ An error is thrown

Hint : What happens when you try to parse something that isn't a number? JavaScript has a special value for this.

Exercise 10 β€” Extract First and Last

Get the first and last characters of a string:

Hint : Strings start at index 0. How can you calculate the position of the last character using .length?

Exercise 11 β€” Edge Cases Results

Match each edge case with its result:

Items to match
β€’ "Hello"[10]
β€’ "Hello".charAt(10)
β€’ "Hello".slice(10)
β€’ parseInt("abc")
Definitions
β€’ undefined
β€’ "" (empty string)
β€’ "" (empty string)
β€’ NaN

Exercise 12 β€” Case Sensitivity in .includes()

What will this return?

let type = "Fire"; let result = type.includes("fire"); console.log(result);
Options
β€’ false
β€’ true
β€’ undefined
β€’ An error occurs

Hint : Does .includes() treat uppercase and lowercase letters as the same or different?

Exercise 13 β€” Get the Last Character

Get the last character of a string using .charAt() and .length:

Hint : If a string has 7 characters and indexing starts at 0, what's the last index? How do you calculate it?

Exercise 14 β€” String + Number

What happens when you add a string and a number?

let level = 25; let message = "Level " + level; console.log(message);
Options
β€’ "Level 25" - The number is converted to a string
β€’ An error occurs
β€’ NaN
β€’ 25

Hint : What happens when you use + between different types? Does the number stay a number?

Exercise 15 β€” Negative Indices in .slice()

What does .slice(-1) return?

let name = "Florachu"; let result = name.slice(-1); console.log(result);
Options
β€’ "u" - The last character
β€’ "F" - The first character
β€’ "Florachu"
β€’ undefined

Hint : With .slice(), negative numbers count backwards. Where does counting start from?

Exercise 16 β€” Calculate Inside Template Literals

Use a template literal to display double the level:

Hint : Inside ${} you can write math! What operation doubles a number?

Exercise 17 β€” Best Practices

Match each situation with the best practice:

Items to match
β€’ Building strings with variables
β€’ Getting last characters
β€’ Comparing user input
β€’ Processing user input
Definitions
β€’ Use template literals
β€’ Use .slice() with negative index
β€’ Convert to same case first
β€’ Use .trim() first

Exercise 18 β€” Trim and Length

What is the length after trimming?

let name = " Aquachu "; let trimmed = name.trim(); console.log(trimmed.length);
Options
β€’ 7
β€’ 11
β€’ 9
β€’ 8

Hint : After .trim() removes the spaces at the start and end, how many letters remain in the name?

Exercise 19 β€” Extract Middle Characters

Extract "ora" from "Florachu":

Hint : Find where "ora" starts and ends in the string. Remember that .slice() doesn't include the end index.

Exercise 20 β€” Parsing with Decimals

Which function should you use to convert "3.14" to a number?

Options
β€’ parseFloat() - to keep the decimal part
β€’ parseInt() - it works for all numbers
β€’ Both work the same
β€’ Neither can parse decimals

Hint : One function keeps decimal numbers, the other cuts them off. Which one do you need for a decimal value?

Exercise 21 β€” Finding Character Position

You want to find at which index the letter "c" appears in "Aquachu". Which method would you use?

Options
β€’ .indexOf() - returns the index of a substring
β€’ .includes() - checks if it contains the character
β€’ .charAt() - gets character at index
β€’ .slice() - extracts parts of strings

Hint : Think about what each method returns: a boolean, a character, a piece of the string, or a position?

Exercise 22 β€” Chain Multiple Methods

Clean and format user input in one line:

Hint : What needs to happen to clean up user input? Remove extra spaces? Change the case? In what order?

Exercise 23 β€” Mixing Quote Types

Which string is valid?

Options
β€’ let message = "It's Aquachu!";
β€’ let message = 'It's Aquachu!';
β€’ Both are valid
β€’ Neither is valid

Hint : Look carefully at the apostrophe in "It's". What type of quotes are used inside and outside the string?

Exercise 24 β€” Method Return Types

Match each method with what type it returns:

Items to match
β€’ .length
β€’ .includes()
β€’ .slice()
β€’ parseInt()
Definitions
β€’ Number
β€’ Boolean
β€’ String
β€’ Number (or NaN)

Exercise 25 β€” Build a Display Name

Create a formatted display name: first letter uppercase, rest lowercase:

Hint : You need to split the string into two parts, change their case differently, then join them. Which methods help with this?

Back to the course