美文网首页
javaScript reverse a string

javaScript reverse a string

作者: 猴子王 | 来源:发表于2018-11-02 20:22 被阅读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

相关文章

网友评论

      本文标题:javaScript reverse a string

      本文链接:https://www.haomeiwen.com/subject/cizcxqtx.html