# Reverse a String challenge

**URL:** <https://forum.freecodecamp.org/t/reverse-a-string-challenge/66942>\
**Category:** General\
**Created:** [December 19, 2016, 2:46am UTC](https://forum.freecodecamp.org/t/reverse-a-string-challenge/66942 "2016-12-19T02:46:34Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![nameoferic](https://avatars.discourse-cdn.com/v4/letter/n/7ea924/32.png) [@nameoferic](https://forum.freecodecamp.org/u/nameoferic)\
**Post date:** [December 19, 2016, 2:46am UTC](https://forum.freecodecamp.org/t/reverse-a-string-challenge/66942/1 "2016-12-19T02:46:34Z")

</div>

Not even sure where to start off here, having severe coder’s block!

Do you first str.split()? Then str.reverse()?

---

<div class="post-metadata">

**Author:** ![nameoferic](https://avatars.discourse-cdn.com/v4/letter/n/7ea924/32.png) [@nameoferic](https://forum.freecodecamp.org/u/nameoferic)\
**Post date:** [December 19, 2016, 2:59am UTC](https://forum.freecodecamp.org/t/reverse-a-string-challenge/66942/2 "2016-12-19T02:59:45Z")

</div>

```javascript
function reverseString(string) {
   string = "hello";
  var array = [" "];
  var newArray = [" "];
  newArray.reverse().join();
  return string;
}

reverseString("hello");

```

This is what I’ve hashed out so far, though it’s not working. I figured it was better to try and code myself first.

---

<div class="post-metadata">

**Author:** ![kevcomedia](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/kevcomedia/32/125737_2.png) [@kevcomedia](https://forum.freecodecamp.org/u/kevcomedia)\
**Post date:** [December 19, 2016, 3:04am UTC](https://forum.freecodecamp.org/t/reverse-a-string-challenge/66942/3 "2016-12-19T03:04:43Z")

</div>

I cleaned up your code.  
You need to use triple backticks to post code to the forum.  
See [this post for details](http://forum.freecodecamp.com/t/forum-code-formatting/25574).

You’ll want to convert your string to an array first. A way to do so is

```javascript
var stringArr = string.split("");

```

Now you have an array of the characters that make up the string.

Also, if you used `.join()` without any string between the parentheses, it’ll assume you want to join using commas (so you’ll probably get something like `"a,b,c"` instead of `"abc"`).

---

<div class="post-metadata">

**Author:** ![IsaacAbrahamson](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/isaacabrahamson/32/30882_2.png) [@IsaacAbrahamson](https://forum.freecodecamp.org/u/IsaacAbrahamson)\
**Post date:** [December 19, 2016, 3:06am UTC](https://forum.freecodecamp.org/t/reverse-a-string-challenge/66942/4 "2016-12-19T03:06:14Z")

</div>

Instead of doing:

```javascript
var array = [" "];
var newArray = [" "];

```

try using the split method:

```javascript
string.split('')

```

Then try calling the reverse etc on that!

Bonus trick: `.split()` returns itself, so you can chain the next methods on it.  
Bonus trick two. Instead of calling the methods on string and then returning string, try returning string with the methods ex:

```javascript
function blahblah(string) { return string.method1().method2() }

```

---

<div class="post-metadata">

**Author:** ![joshuaaron](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/joshuaaron/32/164085_2.png) [@joshuaaron](https://forum.freecodecamp.org/u/joshuaaron)\
**Post date:** [December 19, 2016, 3:41am UTC](https://forum.freecodecamp.org/t/reverse-a-string-challenge/66942/5 "2016-12-19T03:41:00Z")

</div>

There are a lot of ways to go about this.  
Everything mentioned above is pretty much correct.

```
function stringReverse(str) {
     return str.split("").reverse().join("");
}

stringReverse("Reverse me");
```

---

<div class="post-metadata">

**Author:** ![Omegga](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/omegga/32/97940_2.png) [@Omegga](https://forum.freecodecamp.org/u/Omegga)\
**Post date:** [December 19, 2016, 7:32am UTC](https://forum.freecodecamp.org/t/reverse-a-string-challenge/66942/6 "2016-12-19T07:32:04Z")

</div>

Hi.

In order to reverse a string, you have to change the order of the characters in string.

The issue is, strings in JavaScript cannot be changed.They’re _immutable_.You can’t reverse them.

But on the other hand, you can reverse an array.And an array of characters is able to represent a string.

One way to solve this is creating an array of characters like `["h", "o", "u", "s", "e"]` (upon which you will be able to do your mutations, like reversing, and finally convert this array back to a new string).

the method `split()`, when called on a string, just does that.

`var array = "house".split(""); // returns a newly created array ['h', 'o', 'u', 's', 'e']`

The double quotes in `split("")` is a separator.If instead you call `"house".split("u")` it would return `['ho', 'se']` .  
Since we used an empty string `""`, each character is separated.

now that we have an array, we can call `reverse()` on it.

`array.reverse() // ['e', 's', 'u', 'o', 'h']`

and finally we have to return a string, not an array.Using `join()`, we can create a new string, based on an existing array.

`array.join("") // 'esuoh'`

If you need additional reading on the topic : [https://medium.freecodecamp.com/how-to-reverse-a-string-in-javascript-in-3-different-ways-75e4763c68cb](https://medium.freecodecamp.com/how-to-reverse-a-string-in-javascript-in-3-different-ways-75e4763c68cb)

---

<div class="post-metadata">

**Author:** ![kevcomedia](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/kevcomedia/32/125737_2.png) [@kevcomedia](https://forum.freecodecamp.org/u/kevcomedia)\
**Post date:** [December 19, 2016, 7:43am UTC](https://forum.freecodecamp.org/t/reverse-a-string-challenge/66942/7 "2016-12-19T07:43:23Z")

</div>

You have some typo around this part:

> [@Omegga](#):
>
> `var array = “house”.split(“”); // returns a newly created array [‘e’, ‘s’, ‘u’, ‘o’, ‘h’]`

---

<div class="post-metadata">

**Author:** ![Omegga](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/omegga/32/97940_2.png) [@Omegga](https://forum.freecodecamp.org/u/Omegga)\
**Post date:** [December 19, 2016, 7:46am UTC](https://forum.freecodecamp.org/t/reverse-a-string-challenge/66942/8 "2016-12-19T07:46:38Z")

</div>

Thank you, I’ve edited my post.

---

<div class="post-metadata">

**Author:** ![\_kouqhar](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/_kouqhar/32/135441_2.png) [@\_kouqhar](https://forum.freecodecamp.org/u/_kouqhar)\
**Post date:** [March 11, 2020, 9:04am UTC](https://forum.freecodecamp.org/t/reverse-a-string-challenge/66942/9 "2020-03-11T09:04:37Z")

</div>

```auto
function reverseString(str) {
  let strArr = []
  for(let k = str.length - 1; k >= 0; k--){
    strArr.push(str[k])
  }
    return strArr.join('')
}
reverseString("Greetings from Earth");

```

Reply if you need clearification !!!

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [January 16, 2021, 11:28am UTC](https://forum.freecodecamp.org/t/reverse-a-string-challenge/66942/10 "2021-01-16T11:28:00Z")

</div>

Closed on January 16, 2021, 11:28am UTC


