Reverse a string issue - join function does not seem to work

Reverse a string issue - join function does not seem to work
0

#1

I was wondering if I could tag onto this artical and ask someone to explain to me why this doesn’t work. I can make it work in 5 or 6 different ways but I don’t understand why it doesn’t work this way.

function reverseString(str) {
var string = str.split("");
string.reverse();
string.join(’’);
return string;
}

reverseString(“hello”);


It's not working
#2

I moved this question to a new topic, because it had nothing to do with the original topic you posted it under.

Also, I’ve edited your post for readability. When you enter a code block into the forum, remember to precede it with a line of three backticks and follow it with a line of three backticks to make easier to read. See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

Your code above does not work, because even though string.reverse() reverses the string array in-place, string.join(’’) does not join the array in-place. Instead, it returns a string, but since you did not assign the returned value to anything, that returned value is “lost”. If you were to have done either of the following, it would work:

var reversedString = string.join('');
return reverseString;

OR

return string.join('');