Tutorialspoint
Problem
Solution
Submissions

Reverse a String

Certification: Basic Level Accuracy: 100% Submissions: 2 Points: 5

Write a JavaScript function to reverse a string. Implement built-in methods where the function should return a reversed string.

Example 1
  • Input: s = "hello"
  • Output: "olleh"
  • Explanation:
    • The original string is "hello" with characters h-e-l-l-o.
    • Reversing the order gives us o-l-l-e-h.
    • The reversed string is "olleh".
Example 2
  • Input: s = "JavaScript"
  • Output: "tpircSavaJ"
  • Explanation:
    • The original string is "JavaScript" with 10 characters.
    • Reversing character by character from end to beginning.
    • The reversed string is "tpircSavaJ".
Constraints
  • 0 ≤ s.length ≤ 10^4
  • s consists of printable ASCII characters
  • Time Complexity: O(n)
  • Space Complexity: O(n)
StringsCapgeminiApple
Editorial

Login to view the detailed solution and explanation for this problem.

My Submissions
All Solutions
Lang Status Date Code
You do not have any submissions for this problem.
User Lang Status Date Code
No submissions found.

Please Login to continue
Solve Problems

 
 
 
Output Window

Don't have an account? Register

Solution Hints

  • For built-in approach: convert string to array, use reverse() method, then join back
  • Alternatively, build the reversed string by iterating from the last character
  • Consider edge cases like empty strings

Steps to solve by this approach:

 Step 1: Built-in method - Convert string to array using split('')
 Step 2: Use the reverse() method to reverse the array elements
 Step 3: Join the reversed array back to string using join('')

Submitted Code :