Javascript · Strings
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);
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
let level = 25;
let message = ____Level ________level________;
console.log(message);
Options: ` ` $ { } level "
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:
let codemon = "Florachu";
let lastThree = codemon.____(________);
console.log(lastThree);
Options: slice substring - 3 codemon.length 0
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:
let name = "Pyrochu";
let first = name____;
let last = name.____(name.____ - 1);
console.log(first + last);
Options: [0] [1] charAt length slice trim
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()
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
:
let codemon = "Pyrochu";
let lastChar = codemon.____(codemon.____ ____ ____);
console.log(lastChar);
Options: charAt length - 1 + 0 slice
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:
let level = 20;
let message = ____Level: ________level ____ 2________;
console.log(message);
Options: ` ` $ { } * + - "
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"
:
let name = "Florachu";
let middle = name.____(____, ____);
console.log(middle);
Options: slice 2 5 3 4 substring charAt
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:
let userInput = " AQUACHU ";
let clean = userInput.____().____();
console.log(clean);
Options: trim toLowerCase toUpperCase slice includes charAt
Hint : What needs to happen to clean up user input? Remove extra spaces? Change the case? In what order?
Exercise 23 — Mixing Quote Types
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:
let input = "aQuAcHu";
let first = input.____(0, 1).____();
let rest = input.____(1).____();
let display = first ____ rest;
console.log(display);
Options: slice toUpperCase toLowerCase + charAt trim slice
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