Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to access the first character of the string.
DSA Javascript
const str = "hello"; const firstChar = str[1]; console.log(firstChar);
Attempts:
3 left
💡 Hint
Common Mistakes
Using index 1 instead of 0.
Using .length property instead of accessing character.
✗ Incorrect
In JavaScript, you can access the first character of a string using bracket notation with index 0.
2fill in blank
mediumComplete the code to loop through each character of the string using a for loop.
DSA Javascript
const word = "code"; for (let i = 0; i [1] word.length; i++) { console.log(word[i]); }
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<=' causes index out of range error.
Using '>' or '>=' causes the loop to never run.
✗ Incorrect
The loop should run while i is less than the string length to avoid going out of bounds.
3fill in blank
hardFix the error in the code to correctly get the last character of the string.
DSA Javascript
const text = "example"; const lastChar = text.charAt(text.length [1] 1); console.log(lastChar);
Attempts:
3 left
💡 Hint
Common Mistakes
Using '+' causes index out of range.
Using '*' or '/' are invalid for index calculation.
✗ Incorrect
To get the last character, subtract 1 from the string length because indices start at 0.
4fill in blank
hardFill both blanks to create a loop that prints characters only if they are vowels.
DSA Javascript
const phrase = "hello world"; for (let i = 0; i [1] phrase.length; i++) { const ch = phrase[i]; if ("aeiou".includes(ch) [2] true) { console.log(ch); } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>=' or '>' in loop condition causes errors.
Using '==' instead of '===' can cause unexpected behavior.
✗ Incorrect
The loop runs while i is less than phrase length. The condition checks if ch is exactly equal to true from includes().
5fill in blank
hardFill all three blanks to create an object mapping each character to its uppercase version if it is a letter.
DSA Javascript
const input = "abc123"; const result = {}; for (let i = 0; i [1] input.length; i++) { const ch = input[i]; if (ch [2] "a" && ch [3] "z") { result[ch] = ch.toUpperCase(); } } console.log(result);
Attempts:
3 left
💡 Hint
Common Mistakes
Using '==' instead of comparison operators.
Using '>=' and '<=' in wrong order.
✗ Incorrect
Loop runs while i < input.length. The if condition checks if ch is between 'a' and 'z' inclusive using >= and <=.
