Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to print each character of the string word using a for loop.
DSA Typescript
const word = "hello"; for (let i = 0; i < word.length; i++) { console.log(word[1]); }
Attempts:
3 left
💡 Hint
Common Mistakes
Using parentheses instead of square brackets to access characters.
Using curly braces or angle brackets which are invalid here.
✗ Incorrect
In TypeScript, strings can be accessed like arrays using square brackets with the index inside, e.g., word[i].
2fill in blank
mediumComplete the code to print the last character of the string text.
DSA Typescript
const text = "typescript"; console.log(text[1]);
Attempts:
3 left
💡 Hint
Common Mistakes
Using text.length as index which is out of bounds.
Using 0 which gives the first character, not the last.
✗ Incorrect
The last character is at index length - 1 because string indices start at 0.
3fill in blank
hardFix the error in the code to correctly print each character in uppercase.
DSA Typescript
const name = "code"; for (let i = 0; i < name.length; i++) { console.log(name[1].toUpperCase()); }
Attempts:
3 left
💡 Hint
Common Mistakes
Calling toUpperCase without parentheses.
Using parentheses to access characters instead of square brackets.
✗ Incorrect
You must access the character with square brackets and call toUpperCase() as a function on that character.
4fill in blank
hardFill both blanks to create a loop that prints characters at even indices only.
DSA Typescript
const phrase = "algorithm"; for (let [1] = 0; [2] < phrase.length; [1] += 2) { console.log(phrase[[1]]); }
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names in the loop condition and increment.
Incrementing by 1 instead of 2.
✗ Incorrect
The loop variable must be consistent; using 'i' for initialization and condition ensures correct iteration.
5fill in blank
hardFill all three blanks to create a function that returns the first character of a string in uppercase.
DSA Typescript
function firstCharUppercase(str: string): string {
return str[1].toUpperCase() + str[2];
}Attempts:
3 left
💡 Hint
Common Mistakes
Using substring(1) instead of slice(1) (both work but only one is correct here).
Using [1] which gets only the second character.
✗ Incorrect
Use str[0] to get the first character, then slice(1) to get the rest of the string.
