More Solutions to Javascript Reverse String Problem

I just started the Udemy course “The Coding Interview Bootcamp” by Stephen Grider.  I have five solutions below for the reverse string problem in Javascript.  The first three solutions I was already comfortable with.  I never actually used the for…of loop, however.  Grider recommends using it when possible in interviews as there is less room for typos and simple errors than with a traditional for loop.  I tried using the reduce() method before, but never completely understood it.  Grider explains it as reducing an array into a single item or value.  It makes a lot more sense now.

// SOLUTION 1 - using array.prototype.reverse() method
function reverse(str) {
   return str.split("").reverse().join("");

// SOLUTION 2 - using traditional for loop
function reverse(str) {
   const arr = str.split("");
   const reverse = [];

   for (let i=arr.length; i>=0; i--) {
   return reverse.join("");

// SOLUTION 3 - using array.prototype.forEach() method
function reverse(str) {
   const arr=str.split("");
   const reverse=arr.reverse();
   const reverseReturn = []

   arr.forEach(function(letter) {
   return reverseReturn.join("");

// SOLUTION 4 - using for...of loop
function reverse(str) {
   let reversed = '';

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

// SOLUTION 5  - using array.prototype.reduce() method
function reverse(str) {
   return str.split('').reduce((reversed, character) => 
       character + reversed



Leave a Reply

Your email address will not be published. Required fields are marked *