Hướng dẫn reverse words in javascript
Looked around and could not find an answer for this one. Show
Nội dung chính
Nội dung chính
I am trying to reverse each word of a string without changing the order of the words... this is the code I have:
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.
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/
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
And if you want to preserve the punctuation in place:
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.
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 stringPhoto by Lukas Robertson on UnsplashWe are going to write a function called 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.') The first thing we are going to do is create a variable called 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(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. |