String traversal means going through each character in a string one by one, usually from the first character to the last.
Click to reveal answer
beginner
How do you access the character at index 3 in a string named text in TypeScript?
You use text[3] to get the character at index 3. Remember, counting starts at 0, so index 3 is the fourth character.
Click to reveal answer
beginner
What is the index of the first character in a string?
The first character of a string is at index 0.
Click to reveal answer
intermediate
Why is it important to check string length before accessing characters by index?
Because accessing an index outside the string length can cause errors or return undefined. Checking length helps avoid mistakes.
Click to reveal answer
beginner
What will console.log('hello'.length) print?
It will print 5 because the string 'hello' has 5 characters.
Click to reveal answer
What is the index of the last character in a string of length 7?
A1
B7
C0
D6
✗ Incorrect
Indexing starts at 0, so the last character is at length - 1, which is 6.
Which of these is the correct way to get the first character of a string s in TypeScript?
As[0]
Bs[1]
Cs.charAt(1)
Ds.length
✗ Incorrect
The first character is at index 0, so s[0] gets it.
What happens if you try to access s[10] when s has length 5?
AThrows an error
BReturns the last character
CReturns undefined
DReturns the first character
✗ Incorrect
Accessing an index outside the string length returns undefined in TypeScript.
How can you find the length of a string str?
Astr.size
Bstr.length
Clength(str)
Dstr.count
✗ Incorrect
The property str.length gives the number of characters in the string.
Which loop is commonly used to traverse each character in a string?
AAll of the above
Bwhile loop
Cdo-while loop
Dfor loop
✗ Incorrect
All these loops can be used to traverse strings, but for loop is most common.
Explain how to traverse a string and access each character in TypeScript.
Think about counting from the start to the end of the string.
You got /3 concepts.
Why should you be careful when accessing characters by index in a string?
Consider what happens if you ask for a character beyond the string's end.
You got /4 concepts.
Practice
(1/5)
1. What does the expression str[2] return when str = "hello"?
easy
A. The character 'e'
B. The character 'l'
C. The substring 'll'
D. An error because strings are not indexable
Solution
Step 1: Understand string indexing
Strings in TypeScript can be accessed by index starting at 0, so index 2 refers to the third character.
Step 2: Identify the character at index 2 in "hello"
The characters are: h(0), e(1), l(2), l(3), o(4). So index 2 is 'l'.
Final Answer:
The character 'l' -> Option B
Quick Check:
str[2] = 'l' [OK]
Hint: Remember string index starts at 0 [OK]
Common Mistakes:
Thinking index starts at 1
Confusing character with substring
Assuming strings are not indexable
2. Which of the following is the correct way to loop through each character of a string text in TypeScript?
easy
A. for (let char of text) { console.log(char); }
B. for (let char in text) { console.log(char); }
C. for (let i = 0; i <= text.length; i++) { console.log(text[i]); }
D. for (let i = 1; i <= text.length; i++) { console.log(text[i]); }
Solution
Step 1: Understand looping over strings
Using for...of loops over characters directly, which is the cleanest way.
Step 2: Evaluate the options
The index-based loop using <= text.length causes an out-of-bounds access. for...in iterates over string indices ('0', '1', etc.) rather than characters. for...of correctly iterates over characters. Starting the index at 1 skips the first character and risks out-of-bounds.
Final Answer:
for (let char of text) { console.log(char); } -> Option A
Quick Check:
Use for...of to access characters directly [OK]
Hint: Use for...of to loop characters directly [OK]
Common Mistakes:
Using for...in which loops keys, not values
Starting index at 1 instead of 0
Confusing index with character in loops
3. What is the output of the following code?
const s = "abcde";
let result = "";
for (let i = 0; i < s.length; i += 2) {
result += s[i];
}
console.log(result);
medium
A. "bdf"
B. ""
C. "abcde"
D. "ace"
Solution
Step 1: Analyze the loop increments and indexing
The loop starts at 0 and increments by 2 each time, so indexes are 0, 2, 4.
Step 2: Collect characters at indexes 0, 2, 4
Characters are s[0] = 'a', s[2] = 'c', s[4] = 'e'. Concatenated result is "ace".
Final Answer:
"ace" -> Option D
Quick Check:
Indexes 0,2,4 characters combined = "ace" [OK]
Hint: Check loop step size and indexes accessed [OK]
Common Mistakes:
Assuming loop increments by 1
Confusing indexes with characters
Expecting all characters concatenated
4. Identify the error in the following code snippet that tries to print each character of word:
const word = "test";
for (let i = 1; i <= word.length; i++) {
console.log(word[i]);
}
medium
A. Missing declaration of variable i
B. Using <= instead of < causes infinite loop
C. Indexing starts at 1, skipping first character and causing undefined at end
D. Strings cannot be accessed by index in TypeScript
Solution
Step 1: Check loop index range
The loop starts at 1 and goes up to word.length (4), so indexes accessed are 1,2,3,4.
Step 2: Identify indexing issues
Index 0 is skipped (first character 't'), and index 4 is out of bounds (undefined). So first character is missed and last access is invalid.
Final Answer:
Indexing starts at 1, skipping first character and causing undefined at end -> Option C
Quick Check:
String indexes start at 0, not 1 [OK]
Hint: Loop indexes for strings start at 0 and go to length-1 [OK]
Common Mistakes:
Starting loop at 1 instead of 0
Using <= instead of < in loop condition
Assuming strings are not indexable
5. Given a string input, which code snippet correctly reverses the string by traversing it from end to start?
hard
A. let rev = '';
for (let i = input.length - 1; i >= 0; i--) {
rev += input[i];
}
console.log(rev);
B. let rev = '';
for (let i = 0; i < input.length; i++) {
rev += input[input.length - i];
}
console.log(rev);
C. let rev = '';
for (let i = input.length; i > 0; i--) {
rev += input[i];
}
console.log(rev);
D. let rev = '';
for (let i = input.length; i >= 0; i--) {
rev += input[i];
}
console.log(rev);
Solution
Step 1: Understand correct index range for reverse traversal
Indexes go from input.length - 1 down to 0 inclusive to cover all characters in reverse.
Step 2: Verify the index ranges
Looping from input.length - 1 down to 0 inclusive correctly captures all characters in reverse. A forward loop using input.length - i first accesses an out-of-bounds index. Starting at input.length immediately accesses invalid index. Decrementing from input.length to 0 also starts with out-of-bounds.
Final Answer:
let rev = '';
for (let i = input.length - 1; i >= 0; i--) {
rev += input[i];
}
console.log(rev); -> Option A
Quick Check:
Loop from length-1 down to 0 for reverse traversal [OK]
Hint: Start loop at length-1 and go down to 0 [OK]
Common Mistakes:
Starting loop at length instead of length-1
Using wrong index inside loop causing undefined
Loop condition off by one causing missing characters
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.