javaScript reverse a string

2018-11-02  本文已影响5人  猴子王

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
上一篇下一篇

猜你喜欢

热点阅读