Bird
Raised Fist0
DSA Javascriptprogramming~10 mins

String Reversal Approaches in DSA Javascript - Interactive Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to reverse a string using built-in methods.

DSA Javascript
function reverseString(str) {
  return str.split('').[1]().join('');
}
Drag options to blanks, or click blank then click option'
Areverse
Bsort
Cmap
Dfilter
Attempts:
3 left
💡 Hint
Common Mistakes
Using sort() instead of reverse() changes order alphabetically, not reverses.
Using map() or filter() does not reverse the array.
2fill in blank
medium

Complete the code to reverse a string using a for loop.

DSA Javascript
function reverseString(str) {
  let reversed = '';
  for (let i = str.length - 1; i [1] 0; i--) {
    reversed += str[i];
  }
  return reversed;
}
Drag options to blanks, or click blank then click option'
A>=
B>
C<=
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' or '<=' causes the loop to never run or run incorrectly.
Using '>' excludes the first character at index 0.
3fill in blank
hard

Fix the error in the recursive string reversal function.

DSA Javascript
function reverseString(str) {
  if (str.length === 0) {
    return '';
  }
  return reverseString(str.slice(1)) + str[1](0);
}
Drag options to blanks, or click blank then click option'
Aslice
BcharAt
Csubstr
Dsubstring
Attempts:
3 left
💡 Hint
Common Mistakes
Using slice(0) returns the entire string, causing infinite recursion.
substr and substring can work but are less clear for single character extraction.
4fill in blank
hard

Fill both blanks to create a reversed string using reduce.

DSA Javascript
function reverseString(str) {
  return str.split('').[1]((acc, char) => char [2] acc, '');
}
Drag options to blanks, or click blank then click option'
Areduce
B+
Dconcat
Attempts:
3 left
💡 Hint
Common Mistakes
Using concat instead of '+' may cause errors since concat is for arrays.
Adding accumulator before char reverses the string incorrectly.
5fill in blank
hard

Fill the first two blanks to reverse a string using a while loop.

DSA Javascript
function reverseString(str) {
  let reversed = '';
  let i = str.length [1];
  while (i [2] 0) {
    reversed += str[i];
    i--;
  }
  return reversed;
}
Drag options to blanks, or click blank then click option'
A- 1
B>
D>=
Attempts:
3 left
💡 Hint
Common Mistakes
Starting i at str.length causes out-of-bounds access.
Using '>' excludes the character at index 0.
Incorrect indexing inside loop causes wrong characters.

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