Reverse the string

is this the simplest answer?
it passed the test but are there any simpler answer than mine?

  **Your code so far**

function reverseString(str) {
var array = [];
array = str.split('');
var arr = array.reverse('');
return arr.join('');
}

reverseString("hello");
  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Safari/537.36 OPR/82.0.4227.23

Challenge: Reverse a String

Link to the challenge:

This is about as simple as it gets. But it can be shortened, just a little.

can you do this one without making those variables? :grin:

I like without array elements also, as strings are iterable:

function reverseString(str) {
let res="";
for(let i=str.length-1;i>=0;i--){
 res+=str[i]
}
return res;
}

This concatenatesstr[last]+str[last-1]...
You can probably do it recursively also, but still needs a conditional.

But in your case it could be re written without defining a single variable.

I added spoiler tags around your code since it is a working solution.

Hi @hyperionhype !

I added spoiler tags around your code since it is a working solution.

You can turn your solution into a one liner using method chaining.

2 Likes