JavaScript reverses the letter order for each word in a string

I try to get around the following, but do not have time:

var string = "erehT era a tsav rebmun fo secruoser rof gninrael erom tpircsavaJ"; var x = string.split(" "); for(i=0; i<=x.length; i++){ var element = x[i]; } 
Element

now represents every word inside the array. Now I need to cancel not the word order, but the order of each letter for each word.

+4
source share
4 answers
 var string = "erehT era a tsav rebmun fo secruoser rof gninrael erom tpircsavaJ"; // you can split, reverse, join " " first and then "" too string.split("").reverse().join("").split(" ").reverse().join(" ") 

Conclusion: "There is a huge amount of resources for learning more Javascript."

+11
source

You can do this using Array.prototype.map and Array.prototype.reverse .

 var result = string.split(' ').map(function (item) { return item.split('').reverse().join(''); }).join(' '); 

What does the map function do?

It moves the array created by splitting the initial string and calls function (item) , which we provided as an argument for each element. Then it takes the return value of this function and inserts it into a new array. Finally, it returns this new array, which in our example contains the inverse words in order.

+7
source

You can do the following:

 let stringToReverse = "tpircsavaJ"; stringToReverse.split("").reverse().join("").split(" ").reverse().join(" ") 

// let keyword allows you to declare variables in new ECMAScript (JavaScript)

0
source

You can do the following.

 var string = "erehT era a tsav rebmun fo secruoser rof gninrael erom tpircsavaJ"; arrayX=string.split(" "); arrayX.sort().reverse(); var arrayXX=''; arrayX.forEach(function(item){ items=item.split('').sort().reverse(); arrayXX=arrayXX+items.join(''); }); document.getElementById('demo').innerHTML=arrayXX; 
0
source

Source: https://habr.com/ru/post/1498396/


All Articles