How to begin Reverse a String algorithm?

How to begin Reverse a String algorithm?


So I’m trying to talk out loud this algorithm and break the problem down piece by piece.

Here’s what I understand so far and hopefully the forum can help fill in the blanks.

So I understand I probably will need to store this string into an array.

Once the string is into the array I would probably need to split() the string and store it into a new array. This new array will divide each character. Something like this.

var oldArray = [Hello World]
var newArray = [H e l l o W o r l d] // this a new string with each character in its own individual index.

After the array is split() I probably need to reverse the entire newArray and then join() the characters back into the old array.

Once this is done I need to turn the array back into a string an again. Does my logic sound right?


You have an extra array step both before and after reversing. You can go straight from "Hello World" to ['H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd'] and you can go straight from your reversed array back to a string.


Hello ArielLeslie,

Thank you for your quick response. Here is my question.

How do I store strings into an array? I understand how to use a for loop to iterate through integers, but I don’t know how to take each character and make its own index. Like this below.

[‘H’, ‘e’, ‘l’, ‘l’, ‘o’, ’ ', ‘W’, ‘o’, ‘r’, ‘l’, ‘d’]

Is there something I can review to do this?


I believe that the challenge description recommends that you research the split() function.


look at this, you might like this little cheat hidden in the JS docs:

var str = 'asdfghjkl';
var strReverse = str.split('').reverse().join(''); // 'lkjhgfdsa'
// split() returns an array on which reverse() and join() can be applied


@Feight - The OP did not want to know the solution, he just wanted to make sure his algorithm was correct first. I have blurred out your solution to avoid spoiling it for the OP. Please do not just give the solution in the future when someone is trying to understand their algorithm first. At least blur the solution out by surrounding the solution with [spoiler] and [/spoiler] tags.

Thank you.


Okay this is what I’ve so far. I think its coming along very nice.

function reverseString(str) {

var newArray = str.split(’’);

var newArray2 = newArray.reverse();

var newArray3 = newArray2.join();

str = newArray3.toString();

return str;


reverseString(“Greeting from earth”);

However I don’t know to convert an array back to a string. My output is this below.

h,y,r,a,e, ,m,o,r,f, ,g,n,i,t,e,e,r,G

It should be back back to its original string. Any ideas on what I should do?


The join function actually converts your newArray2 to a string. The problem is, you did not specify a separator. Remember on the split function, you used ‘’ as the separator, which split the string into individual characters. You need to specify ‘’ again for it to join the array back as a string of characters. You can then just return newArray3 once you use the correct separator with the join function. I would recommend naming newArray3 to something which better describes what is holds. Maybe something like joinedString? There is no need to assign it back to str and return str. That would just be an extra step.


I figured it out and I passed the test…but I really don’t understand this ‘’ and this ’ ’ separators

Like why does ‘’ put commas and ’ ’ put spaces? That’s weird to me. lol


If you do as I suggested with the following, there would be no commas.

function reverseString(str) {
  var newArray = str.split(’’);
  var newArray2 = newArray.reverse();
  var joinedString = newArray2.join('');
  return joinedStrr;
reverseString(“Greeting from earth”);

Your original solution had the following line:

var newArray3 = newArray2.join();

Because you did not specify a separator, JavaScript attempted to literally convert your array (remember arrays are separated by commas when you console.log them) to a string. That is why the commas appeared. If you put join(’’), then it creates the string and separates each element with a blank string (hence no commas).


I get it. Makes sense now. Thank you for your help. On to the next.


@RandellDawson its right there in the docs… I couldn’t believe my luck when i found it.
I get your point though.


I had to take a few days off. I’m back working on this algo again… I’m going to check the doc like @Feight. lol