Challenge - 5 Problems
String Traversal Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of character access in a string
What is the output of the following JavaScript code?
DSA Javascript
const str = "hello"; console.log(str[1]);
Attempts:
2 left
💡 Hint
Remember that string indexing starts at 0.
✗ Incorrect
In JavaScript, strings are zero-indexed. str[1] accesses the second character, which is 'e'.
❓ Predict Output
intermediateOutput of string traversal with for loop
What will be printed by this code?
DSA Javascript
const s = "abc"; let result = ""; for(let i = 0; i < s.length; i++) { result += s[i] + "-"; } console.log(result);
Attempts:
2 left
💡 Hint
Check how the loop appends characters and dashes.
✗ Incorrect
The loop adds each character followed by a dash, resulting in 'a-b-c-'.
🧠 Conceptual
advancedUnderstanding string immutability in JavaScript
Which statement about strings in JavaScript is true?
Attempts:
2 left
💡 Hint
Try to assign a new character to a string index and see what happens.
✗ Incorrect
In JavaScript, strings cannot be changed after creation. You cannot assign a new character to a string index.
❓ Predict Output
advancedOutput of accessing out-of-range index in string
What does the following code print?
DSA Javascript
const text = "data"; console.log(text[10]);
Attempts:
2 left
💡 Hint
What happens when you access an index beyond string length?
✗ Incorrect
Accessing an index outside the string length returns undefined in JavaScript.
❓ Predict Output
expertOutput of nested loops traversing a string
What is the output of this code?
DSA Javascript
const s = "ab"; let output = ""; for(let i = 0; i < s.length; i++) { for(let j = 0; j <= i; j++) { output += s[j]; } output += ","; } console.log(output);
Attempts:
2 left
💡 Hint
Trace the inner loop for each outer loop iteration.
✗ Incorrect
First iteration adds 'a', second adds 'a' then 'b', each followed by a comma.
