1

In JavaScript, what would be the most simple way to reverse a string of characters? Because I am a student with brain dysfunctions that doesn't allow me to think straight and my code needs to be as simple as possible.

  • 6
    possible duplicate of [How do you reverse a string in place in JavaScript?](http://stackoverflow.com/questions/958908/how-do-you-reverse-a-string-in-place-in-javascript) – ssube Sep 14 '15 at 17:47
  • 4
    possible duplicate of [Reversing a string in JavaScript](http://stackoverflow.com/questions/1611427/reversing-a-string-in-javascript) – Carlangueitor Sep 14 '15 at 17:47
  • @ssube It is not "possible", but a duplicate indeed! Those two previous comments with links have plenty of implementations/options for that – x80486 Sep 14 '15 at 17:49
  • @ɐuıɥɔɐɯ That's the automatically-generated comment for a dupe. ;) SO chose to phrase it as diplomatically as possible. – ssube Sep 14 '15 at 17:51
  • Oh! Since I can't do that, I didn't know...silly! – x80486 Sep 14 '15 at 17:52
  • 2
    It may be even simpler next time to type the question into **Google**, instead of creating a Stack Overflow account and writing an entire post. (That duplicate was the 2nd result. Also, the first hit was on topic, answered the question, and was a good read.) – Jongware Sep 14 '15 at 18:02

4 Answers4

3
return s.split('').reverse().join('');
gen_Eric
  • 223,194
  • 41
  • 299
  • 337
  • 1
    Code-only answers are not very helpful. You should include an explanation of what is happening and how. – ssube Sep 14 '15 at 17:48
  • It splits the string into an array, each element containing one character, reverses that array's order, then joins the array into a single string with no separator. – Scott Sep 14 '15 at 17:59
0

This one works as well, it's not as dense as the previous mentioned tho.

Here's the jsfiddle

function join(array, con) {
    con = con ? con : "";
    var str = "";
    for (var i = 0; i < array.length; i++) {
        str += array[i] + con;
    }
    return str;
}

function reverse(str) {
    var newStr = [];
    for (var i = 0; i < str.length / 2; i++) {
           var temp = str[i];
       newStr[i] = str[str.length - i - 1];
       newStr[str.length - i - 1] = temp;
    }
    return join(newStr, "");
}

document.write(reverse("FooBar"));
Yeezus
  • 23
  • 8
0

Above answer will work just fine; it is probably the simplest way. Just to add a few words..

  • split() method splits a string into an array.

  • reverse() method reverses an array.

  • join() method joins elements into a string.

So we get

s.split('').reverse().join('');

It splits the string into an array, reverses and puts it back together from an array into a string.

Here is a solution using recursion:

function reverseString(s) {
  return (s === '') ? '' : reverseString(s.substr(1)) + s.charAt(0);
}

The function recursively calls itself, where given string is passed as an argument (except for the first character) It will continue iterate until until it runs out of input. As a result we have a reversed string.

olgash
  • 3
  • 3
0

Method - 1 Convert string to array using Array.from call reverse on array call join on array for converting into string

Method - 2 Using Array.from and reduceRight

const str = "stack overflow";

const rev_str = Array.from(str).reverse().join('');

console.log(rev_str)


// Alternate way
const rev_str2 = Array.from(str).reduceRight((acc, curr) => `${acc}${curr}`, '');

console.log(rev_str2)
Siva K V
  • 10,561
  • 2
  • 16
  • 29