Hướng dẫn reverse words in javascript

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

Nội dung chính

  • Not the answer you're looking for? Browse other questions tagged javascript arrays string reverse or ask your own question.
  • Reverse every word in a string
  • How would you reverse words in a sentence using JavaScript?
  • How do you reverse text in JavaScript?
  • How do you reverse each word in a given string?
  • Does JavaScript have a reverse function?

Nội dung chính

  • Not the answer you're looking for? Browse other questions tagged javascript arrays string reverse or ask your own question.
  • Reverse every word in a string
  • How would you reverse words in a sentence using JavaScript?
  • How do you reverse text in JavaScript?
  • How do you reverse each word in a given string?
  • Does JavaScript have a reverse function?

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,5094 gold badges25 silver badges36 bronze badges

asked Jan 21, 2016 at 18:50

4

Try something like this.

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

console.log(wordsReverser('New string, same results.'));

answered Jan 21, 2016 at 18:55

btavbtav

7601 gold badge5 silver badges8 bronze badges

4

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("");
}

answered Jan 21, 2016 at 18:55

RobertRobert

9631 gold badge15 silver badges22 bronze badges

2

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.

answered Jan 21, 2016 at 18:55

Boris SerebrovBoris Serebrov

15k1 gold badge39 silver badges54 bronze badges

4

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.'));

answered Jan 21, 2016 at 18:56

Not the answer you're looking for? Browse other questions tagged javascript arrays string reverse or ask your own question.

Reverse every word in a string

Photo by Lukas Robertson on Unsplash

We are going to write a function called reverseWords that accepts a string, str, as an argument.

You are given a string of varying word counts. The goal of the function is to reverse every word in the string and return it.

Example:

reverseWords('The quick brown fox jumps over the lazy dog.')
// output: 'ehT kciuq nworb xof spmuj revo eht yzal .god'

The first thing we are going to do is create a variable called reverseWordArr. This variable will contain our string in array form.

let reverseWordArr = str.split(" ")

Since we want to create another array but containing all the words in the string reversed, we will use the map method.

.map(word => word.split("").reverse().join(""));

Here is the full variable:

let reverseWordArr = str.split(" ").map(word => word.split("").reverse().join(""));

Now that we created our array and it contains all the words in the string but in reverse, we need to convert the array back to a string and return it.

return reverseWordArr.join(" ");

Here is the full function:

How would you reverse words in a sentence using JavaScript?

Reverse every word in a string.

let reverseWordArr = str.split(" ").

. map(word => word. split(""). reverse(). join(""));.

let reverseWordArr = str. split(" "). map(word => word. split(""). reverse(). join(""));.

return reverseWordArr.join(" ");.

How do you reverse text in JavaScript?

Example 2: Reverse a String Using built-in Methods First, the string is split into individual array elements using the split() method. str. split("") gives ["h", "e", "l", "l", "o"] . The string elements are reversed using the reverse() method.

How do you reverse each word in a given string?

Split the given inputString into words using split() method. Then take each individual word, reverse it and append to reverseString. Finally print reverseString.

Does JavaScript have a reverse function?

JavaScript Array reverse() The reverse() method reverses the order of the elements in an array. The reverse() method overwrites the original array.