1.字符串的完全翻转:
var str = "smile at life";
document.write(str.split("").reverse().join("")); //结果为efil ta elims
2.翻转字符串中单词顺序,但单词字母顺序不变:
function reverseStr(param){
var arr = param.split(" ");
var newArr = [];
for(i=0;i<arr.length;i++){
newArr[arr.length-i] = arr[i];
}
return newArr.join(" ");
}
document.write(reverseStr("smile at life")); //life at smile
var str = "smile at life";
document.write(str.split("").reverse().join("")); //结果为efil ta elims
2.翻转字符串中单词顺序,但单词字母顺序不变:
function reverseStr(param){
var arr = param.split(" ");
var newArr = [];
for(i=0;i<arr.length;i++){
newArr[arr.length-i] = arr[i];
}
return newArr.join(" ");
}
document.write(reverseStr("smile at life")); //life at smile