# Reverse a String - Why doesn't my code work?

**URL:** <https://forum.freecodecamp.org/t/reverse-a-string-why-doesnt-my-code-work/107415>\
**Category:** General\
**Created:** [April 19, 2017, 9:42am UTC](https://forum.freecodecamp.org/t/reverse-a-string-why-doesnt-my-code-work/107415 "2017-04-19T09:42:33Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![a-mitch-a](https://avatars.discourse-cdn.com/v4/letter/a/898d66/32.png) [@a-mitch-a](https://forum.freecodecamp.org/u/a-mitch-a)\
**Post date:** [April 19, 2017, 9:42am UTC](https://forum.freecodecamp.org/t/reverse-a-string-why-doesnt-my-code-work/107415/1 "2017-04-19T09:42:33Z")

</div>

Hi All

After fiddling with my code for the Reverse a String challenge, I solved the problem eventually. But I’m not sure why my first attempt didn’t work (I’m very new to programming).

Here is my first attempt:

```
function reverseString(str) {
  str.split('').reverse().join('');
  return str;
}

reverseString("hello");

```

And here is what worked:

```
function reverseString(str) {
  return str.split('').reverse().join('');
}

reverseString("hello");

```

I understand why my final solution worked, but I would also like to understand why my first attempt didn’t work. Can anyone explain?

Many thanks,  
Mitch.

---

<div class="post-metadata">

**Author:** ![astv99](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/astv99/32/66820_2.png) [@astv99](https://forum.freecodecamp.org/u/astv99)\
**Post date:** [April 19, 2017, 10:06am UTC](https://forum.freecodecamp.org/t/reverse-a-string-why-doesnt-my-code-work/107415/2 "2017-04-19T10:06:21Z")

</div>

In JavaScript, functions are functional, meaning that they return a value and don’t “mutate” a data type such as a string. So in the first attempt, the returned​ string from the result of the split, reverse, and join is actually lost because you didn’t save it.

The “return str” actually returns the original​ argument string unmodified, because it was never changed to begin with. The split, reverse, and join are all functional, meaning that a new string is returned by each method call.

---

<div class="post-metadata">

**Author:** ![JohnL3](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/johnl3/32/16459_2.png) [@JohnL3](https://forum.freecodecamp.org/u/JohnL3)\
**Post date:** [April 19, 2017, 10:12am UTC](https://forum.freecodecamp.org/t/reverse-a-string-why-doesnt-my-code-work/107415/3 "2017-04-19T10:12:43Z")

</div>

step through it here [https://goo.gl/pnHUf0](https://goo.gl/pnHUf0) … look at the reverseString block on right hand side of window that appears on step 2 look at console.output on right hand side at top on step 3 and look again at reverseString block  
uncomment the commented line of code and step through again

---

<div class="post-metadata">

**Author:** ![mathisme](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/mathisme/32/65930_2.png) [@mathisme](https://forum.freecodecamp.org/u/mathisme)\
**Post date:** [April 19, 2017, 11:42am UTC](https://forum.freecodecamp.org/t/reverse-a-string-why-doesnt-my-code-work/107415/4 "2017-04-19T11:42:33Z")

</div>

Basically what astv said;

Strings are immutable so the functions you called will return a string but have no effect on the original. I believe if you had var a = in front of your function call and then return a; it would have worked.

---

<div class="post-metadata">

**Author:** ![michal9909](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/michal9909/32/65875_2.png) [@michal9909](https://forum.freecodecamp.org/u/michal9909)\
**Post date:** [April 19, 2017, 12:30pm UTC](https://forum.freecodecamp.org/t/reverse-a-string-why-doesnt-my-code-work/107415/5 "2017-04-19T12:30:13Z")

</div>

`str = str.split('').reverse().join('');`  
`return str;`

As people have said above, you need to save changes to your variable.

---

<div class="post-metadata">

**Author:** ![MarcMcIntosh](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/marcmcintosh/32/7112_2.png) [@MarcMcIntosh](https://forum.freecodecamp.org/u/MarcMcIntosh)\
**Post date:** [April 19, 2017, 12:33pm UTC](https://forum.freecodecamp.org/t/reverse-a-string-why-doesnt-my-code-work/107415/6 "2017-04-19T12:33:42Z")

</div>

you’re returning the argument.  
function reverseString(str) {  
…  
return str;  
}

---

<div class="post-metadata">

**Author:** ![a-mitch-a](https://avatars.discourse-cdn.com/v4/letter/a/898d66/32.png) [@a-mitch-a](https://forum.freecodecamp.org/u/a-mitch-a)\
**Post date:** [April 19, 2017, 2:54pm UTC](https://forum.freecodecamp.org/t/reverse-a-string-why-doesnt-my-code-work/107415/7 "2017-04-19T14:54:40Z")

</div>

Thanks all, appreciate the feedback.

---

<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 17, 2021, 8:52pm UTC](https://forum.freecodecamp.org/t/reverse-a-string-why-doesnt-my-code-work/107415/8 "2021-01-17T20:52:32Z")

</div>

Closed on January 17, 2021, 8:52pm UTC


