Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reverse each word in string without changing word order [duplicate]

Looked around and could not find an answer for this one.

I am trying to reverse each word of a string without changing the order of the words...

this is the code I have:

  function wordsReverser(string){
    return string.split('').reverse().join('');
  }
  console.log(wordsReverser('New string, same results.'));

what I am getting for results is this: ".stluser emas ,gnirts weN"

I am looking for this: "weN gnirts... "

Here is a jsbin

like image 507
Lucky500 Avatar asked Apr 26 '26 01:04

Lucky500


1 Answers

Try something like this.

function wordsReverser(string){
return string.split("").reverse().join("").split(" ").reverse().join(" ")  
}

console.log(wordsReverser('New string, same results.'));
like image 164
btav Avatar answered Apr 27 '26 13:04

btav