Quizz
The full text of this chapter, to read or revisit. To practise, do the exercises.
βΆ Do the exercisesExercise 1 β String Immutability
What happens when you try to change a character in a string?
let name = "Aquachu";
name[0] = "E";
console.log(name);"Aquachu" - Strings cannot be modified"Equachu"undefinedHint : 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);"Level 105""Level 15"15Hint : 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);"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:
.includes().slice().trim()parseInt().toLowerCase()Exercise 6 β Empty String Behavior
What is the .length of an empty string?
let empty = "";
console.log(empty.length);01undefinednullHint : 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);324undefinedHint : 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);NaN0nullHint : 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:
"Hello"[10]"Hello".charAt(10)"Hello".slice(10)parseInt("abc")undefined"" (empty string)"" (empty string)NaNExercise 12 β Case Sensitivity in .includes()
What will this return?
let type = "Fire";
let result = type.includes("fire");
console.log(result);falsetrueundefinedHint : 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);"Level 25" - The number is converted to a stringNaN25Hint : 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);"u" - The last character"F" - The first character"Florachu"undefinedHint : 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:
Exercise 18 β Trim and Length
What is the length after trimming?
let name = " Aquachu ";
let trimmed = name.trim();
console.log(trimmed.length);71198Hint : 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?
parseFloat() - to keep the decimal partparseInt() - it works for all numbersHint : 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?
.indexOf() - returns the index of a substring.includes() - checks if it contains the character.charAt() - gets character at index.slice() - extracts parts of stringsHint : 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?
let message = "It's Aquachu!";let message = 'It's Aquachu!';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:
.length.includes().slice()parseInt()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?