question: give a string example "hello" output "olleh";
resolve 1:
function reverseStr(str) {
let newStr = "";
for(let i = str.length-1;i >= 0; i--) {
newStr += str.charAt(i);
}
return newStr;
}
reverseStr("hello"); //print olleh
resolve 2:
function reverseStr(str) {
let strArray = str.split("");
strArray.reverse();
return strArray.join("");
}
reverseStr("hello"); //print olleh
网友评论