Reverse a string - why isn't this correct

Reverse a string - why isn't this correct
0

#1
function reverseString(str) {
  var array = ['hello','Howdy','Greetings from Earth'];
  array = str.split('');
  array = array.reverse();
  array = array.join('');
  str = array.join('');
  return str;
}

reverseString("hello");
reverseString("Howdy");
reverseString("Greetings from Earth");

#2

The above line assigns the result of array.join('') to array. So after this line, array is already a string. When you try to execute the next line:

str = array.join('');

it errors out, because array is no longer an actual array (it is a string). You could simply return array at this point.

FYI - There is no reason to use the following assignment:

array = array.reverse();

Even though the reverse function returns the reversed array, it actually reverses the array in-place which means it replaces the array after the reverse is complete. Writing the following would accomplish the same thing.

array.reverse();