Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript reverse the order of letters for each word in a string

Tags:

javascript

I am trying to get around the following but no success:

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 each word inside the array. I now need to reverse not the order of the words but the order of each letter for each word.

like image 864
Aessandro Avatar asked Aug 22 '13 14:08

Aessandro


1 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(" ")

Output: "There are a vast number of resources for learning more Javascript"

like image 108
Praveen Lobo Avatar answered Oct 04 '22 02:10

Praveen Lobo