Problem Solving | Reverse a string

Problem Solving | Reverse a string

Subscribe to my newsletter and never miss my upcoming articles

Given a string, return a new string with the reversed order of characters.

Examples:

reverse('apple') === 'leppa'

reverse('hello') === 'olleh'

reverse('Greetings!') === '!sgniteerG'

Pseudo Solution:

  1. Convert the string to array
  2. Reverse the array using javascript reverse function
  3. Join the array back to string using .join function
  4. Return the string
function reverse(str) {

let arrayString = [... str]
arrayString.reverse();
return arrayString.join('');

}

Simplified solution:

function reverse(str) {
  return [...str].reverse().join('');
}

Without using the .reverse function

function reverse(str) {
  let reversed = "";

  for (let character of str) {
    reversed = character + reversed;
  }

  return reversed;

}
 
Share this