2

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

StephenTG
  • 2,579
  • 6
  • 26
  • 36
Lucky500
  • 677
  • 5
  • 17
  • 31
  • You seem to be asking two different questions. The jsbin doesn't reflect the the code you posted here and the code wouldn't produce the output you get. The title seems to indicated that you want to change the value of a string in place. That's not possible because strings are immutable. The rest of the post seems to ask about reversing individual words rather than the whole string. – Felix Kling Jan 21 '16 at 18:52
  • I changed the code that I had in the question, but the question and the bin are clear enough I suppose... I want to reverse the string in its place, so the first word on my string New, should be in position one in my result... as of now, I am getting the string reversed, but the first word is results, instead of new. – Lucky500 Jan 21 '16 at 18:56
  • "In place" has a different meaning when talking about values. E.g. `Array#sort` sorts the array "in place": it mutates the array instead of creating a new one. – Felix Kling Jan 21 '16 at 18:57
  • That's why I added an example of the result I was looking for, I understand that it can also mean "in place" as in memory space. – Lucky500 Jan 21 '16 at 19:00

4 Answers4

13

Try something like this.

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

console.log(wordsReverser('New string, same results.'));
btav
  • 790
  • 1
  • 5
  • 8
2

Here's a JSFiddle with the solution you're looking for https://jsfiddle.net/e109c4pc/

function wordsReverser(string){
    var arr = string.split("");
    var output = [];
    for(var i = arr.length - 1;  i >= 0; i--){
        output.push(arr[i]);
    }
  return output.join("");
}
Robert
  • 981
  • 1
  • 15
  • 24
1

Update: edited the answer to reflect the updated question

function wordsReverser(string){
    return string.split(' ').map(function(word) { 
        return word.split('').reverse().join(''); 
    }).join(' ');
}
alert(wordsReverser('New string, same results.'));
// weN ,gnirts emas .stluser

And if you want to preserve the punctuation in place:

function wordsReverser(string){
    return string.split(' ').map(function(word) { 
        var rev = word.split('').reverse().join('');
        if (['.',','].indexOf(rev[0]) > -1) {
          rev = rev.slice(1) + rev[0];
        }
        return rev;
    }).join(' ');
}
alert(wordsReverser('New string, same results.'));
// weN gnirts, emas stluser.
Borys Serebrov
  • 15,636
  • 2
  • 38
  • 54
0

You need to split the text into separate words, and then reverse each word individually.

function wordsReverser(string){
   var words = string.split(' ');
   var result = [];
   for(var i = 0; i < words.length; i ++){
     result.push(words[i].split('').reverse().join(''));
   }  
   return result.join(' ');
}

console.log(wordsReverser('New string, same results.'));
Marcos Casagrande
  • 37,983
  • 8
  • 84
  • 98