Bird
Raised Fist0
DSA Javascriptprogramming~20 mins

String Reversal Approaches in DSA Javascript - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
String Reversal Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output 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);
Ahello
Bolleh
Cundefined
DError
Attempts:
2 left
💡 Hint
Think about what split, reverse, and join do to the string.
❓ Predict Output
intermediate
2:00remaining
Output 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);
AError
Bworld
Cundefined
Ddlrow
Attempts:
2 left
💡 Hint
The loop starts from the end of the string and builds the reversed string.
❓ Predict Output
advanced
2:00remaining
Output 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"));
Aabcde
BError
Cedcba
Dedcbe
Attempts:
2 left
💡 Hint
The function calls itself with the string minus the first character, then adds the first character at the end.
❓ Predict Output
advanced
2:00remaining
Output 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);
Aecuder
Breduce
CError
Dedcuer
Attempts:
2 left
💡 Hint
Reduce accumulates characters by adding the current character before the accumulator.
❓ Predict Output
expert
2:00remaining
Output 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);
Atsetpam
Bmaptest
CError
Dtsetmap
Attempts:
2 left
💡 Hint
The map function accesses characters from the end to the start using the index.

Practice

(1/5)
1. What does the JavaScript method split('') do when applied to a string?
easy
A. It reverses the string characters.
B. It removes all spaces from the string.
C. It joins array elements into a string.
D. It converts the string into an array of individual characters.

Solution

  1. Step 1: Understand the split method

    The split('') method splits a string into an array by each character because the separator is an empty string.
  2. Step 2: Effect on string

    For example, 'hello'.split('') becomes ['h', 'e', 'l', 'l', 'o'].
  3. Final Answer:

    It converts the string into an array of individual characters. -> Option D
  4. Quick Check:

    split('') = array of chars [OK]
Hint: split('') breaks string into chars array [OK]
Common Mistakes:
  • Thinking split('') reverses the string
  • Confusing split('') with join()
  • Assuming split('') removes spaces
2. Which of the following is the correct syntax to reverse a string str in JavaScript using built-in methods?
easy
A. str.reverse().join('')
B. str.split('').reverse().join('')
C. reverse(str).split('').join('')
D. str.join('').reverse()

Solution

  1. Step 1: Understand string and array methods

    Strings do not have a reverse() method, but arrays do. So first convert string to array using split('').
  2. Step 2: Reverse array and join back

    Use reverse() on the array, then join('') to convert back to string.
  3. Final Answer:

    str.split('').reverse().join('') -> Option B
  4. Quick Check:

    split + reverse + join = reversed string [OK]
Hint: Split string, reverse array, join back [OK]
Common Mistakes:
  • Trying to call reverse() directly on string
  • Using join() before reverse()
  • Using incorrect method order
3. What is the output of the following code?
const str = 'abc';
const reversed = str.split('').reverse().join('');
console.log(reversed);
medium
A. Error: reverse is not a function
B. 'abc'
C. 'cba'
D. ['c', 'b', 'a']

Solution

  1. Step 1: Split string into array

    'abc'.split('') becomes ['a', 'b', 'c'].
  2. Step 2: Reverse array and join

    ['a', 'b', 'c'].reverse() becomes ['c', 'b', 'a'], then .join('') makes it 'cba'.
  3. Final Answer:

    'cba' -> Option C
  4. Quick Check:

    split + reverse + join = 'cba' [OK]
Hint: Split, reverse, join outputs reversed string [OK]
Common Mistakes:
  • Expecting original string output
  • Expecting array output instead of string
  • Confusing method order causing errors
4. Identify the error in this string reversal code:
function reverseString(s) {
  return s.reverse().join('');
}
medium
A. Strings do not have reverse() method.
B. join() cannot be used on strings.
C. Function missing return statement.
D. split('') is missing before reverse().

Solution

  1. Step 1: Check string methods

    Strings do not have reverse(), so calling s.reverse() causes error.
  2. Step 2: Fix by splitting string

    We must convert string to array first using s.split('') before reversing.
  3. Final Answer:

    Strings do not have reverse() method. -> Option A
  4. Quick Check:

    No reverse() method on strings [OK]
Hint: Strings do not have reverse() method [OK]
Common Mistakes:
  • Trying to reverse string directly
  • Ignoring need to split string
  • Misunderstanding join() usage
5. Given the function below, what will reverseString('hello world') return?
function reverseString(s) {
  let reversed = '';
  for (let i = s.length - 1; i >= 0; i--) {
    reversed += s[i];
  }
  return reversed;
}
hard
A. 'dlrow olleh'
B. 'hello world'
C. 'helloworld'
D. Error: Cannot read property 'length' of undefined

Solution

  1. Step 1: Loop from end to start

    The for loop starts at last character index and moves backward to 0, appending each character to reversed.
  2. Step 2: Build reversed string

    Characters are added in reverse order, so 'hello world' becomes 'dlrow olleh'.
  3. Final Answer:

    'dlrow olleh' -> Option A
  4. Quick Check:

    Loop backward appends chars reversed [OK]
Hint: Loop backward to build reversed string [OK]
Common Mistakes:
  • Looping forward instead of backward
  • Not appending characters correctly
  • Confusing string concatenation
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard