d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Javascript Help
Add Reply New Topic New Poll
Member
Posts: 11,254
Joined: Nov 6 2004
Gold: 0.00
Mar 4 2017 11:07am
How can i take a string of words and replace / switch the first and last words with each other?

I.E.
original - this is a sentence
new - sentence is a the
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Mar 4 2017 01:35pm
split the string into an array. swap the first and last entries.
Member
Posts: 31,235
Joined: Nov 17 2007
Gold: 27,290.00
Mar 19 2017 12:40am
Code
function swapfirstandlast2(str){
var arr = str.split(" ");
var temp = arr[0]
arr[0] = arr[arr.length - 1];
arr[arr.length -1] = temp;
var newstr = arr.join(" ");
return newstr
}


This post was edited by lilpsychokilla on Mar 19 2017 01:00am
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll