Challenge - 5 Problems
String Reversal Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of reversing string using split, reverse, join
What is the output of the following JavaScript code that reverses a string?
DSA Javascript
const str = "hello"; const reversed = str.split("").reverse().join(""); console.log(reversed);
Attempts:
2 left
💡 Hint
Think about what split, reverse, and join do to the string.
✗ Incorrect
The string is split into an array of characters, reversed, then joined back into a string, resulting in the reversed string.
❓ Predict Output
intermediateOutput of manual string reversal with for loop
What is the output of this code that reverses a string manually using a for loop?
DSA Javascript
const str = "world"; let reversed = ""; for(let i = str.length - 1; i >= 0; i--) { reversed += str[i]; } console.log(reversed);
Attempts:
2 left
💡 Hint
The loop starts from the end of the string and builds the reversed string.
✗ Incorrect
The loop appends characters from the end to the start, creating the reversed string.
❓ Predict Output
advancedOutput of recursive string reversal function
What is the output of this recursive function that reverses a string?
DSA Javascript
function reverseString(str) { if (str === "") return ""; return reverseString(str.substr(1)) + str[0]; } console.log(reverseString("abcde"));
Attempts:
2 left
💡 Hint
The function calls itself with the string minus the first character, then adds the first character at the end.
✗ Incorrect
The recursion builds the reversed string by moving the first character to the end after reversing the rest.
❓ Predict Output
advancedOutput of reversing string using reduce method
What is the output of this code that reverses a string using the reduce method?
DSA Javascript
const str = "reduce"; const reversed = str.split("").reduce((acc, char) => char + acc, ""); console.log(reversed);
Attempts:
2 left
💡 Hint
Reduce accumulates characters by adding the current character before the accumulator.
✗ Incorrect
The reduce method builds the reversed string by prepending each character to the accumulator.
❓ Predict Output
expertOutput of reversing string with spread operator and map
What is the output of this code that reverses a string using the spread operator and map?
DSA Javascript
const str = "maptest"; const reversed = [...str].map((_, i, arr) => arr[arr.length - 1 - i]).join(""); console.log(reversed);
Attempts:
2 left
💡 Hint
The map function accesses characters from the end to the start using the index.
✗ Incorrect
The map creates a new array by picking characters from the end to the start, then joins them to form the reversed string.
