Challenge - 5 Problems
String Mastery in TypeScript
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the output of this TypeScript string code?
Consider this TypeScript code that manipulates strings. What will it print?
DSA Typescript
const s = 'hello'; const result = s[0] + s[1] + s.length; console.log(result);
Attempts:
2 left
💡 Hint
Remember that s.length gives the number of characters in the string.
✗ Incorrect
The code takes the first two characters 'h' and 'e' from the string 'hello' and adds the length of the string, which is 5. So the output is 'he5'.
🧠 Conceptual
intermediateHow does TypeScript handle string immutability?
Which statement best describes string immutability in TypeScript?
Attempts:
2 left
💡 Hint
Think about whether you can assign a new character to a string index.
✗ Incorrect
Strings in TypeScript (like in JavaScript) are immutable, meaning you cannot change individual characters by index. You must create a new string to change content.
❓ Predict Output
advancedWhat is the output of this TypeScript string concatenation?
Analyze the following TypeScript code and determine its output.
DSA Typescript
const a = 'abc'; const b = 'def'; const c = a + b.length + b[0]; console.log(c);
Attempts:
2 left
💡 Hint
Remember that b.length is a number and adding it to a string converts it to string.
✗ Incorrect
The code concatenates 'abc' + '3' + 'd' resulting in 'abc3d'.
🔧 Debug
advancedWhich option causes a runtime error in this TypeScript string code?
Given the code snippet below, which option will cause a runtime error when executed?
DSA Typescript
const str: string = 'test'; const char = str.charAt(10); console.log(char.length);
Attempts:
2 left
💡 Hint
Check what charAt returns when index is out of range.
✗ Incorrect
charAt returns an empty string '' if index is out of range, so char.length is 0 and no error occurs.
🚀 Application
expertHow to correctly reverse a string in TypeScript?
Which option correctly reverses the string 'hello' in TypeScript?
Attempts:
2 left
💡 Hint
Strings do not have a reverse method directly.
✗ Incorrect
To reverse a string, split it into an array of characters, reverse the array, then join back into a string.
