当一个字符串被传递给一个带有返回语句的函数时,你如何在JavaScript中反转它,而不使用内置函数(.reverse(), . charat()等)?


当前回答

在ES6中,你还有一个选择

function reverseString (str) {
  return [...str].reverse().join('')
}

reverseString('Hello');

其他回答

下面是一个基本的ES6不可变的例子,没有使用Array.prototype.reverse:

//:: reverse = String ->字符串 const reverse = s => []. reduceright .使用实例调用(s, (a, b) => a + b) Console.log (reverse('foo')) // => 'oof' Console.log (reverse('bar')) // => 'rab' Console.log (reverse('foo-bar')) // => ' rabb -oof'

详细分析和十种不同的方法来反转字符串和他们的性能细节。

http://eddmann.com/posts/ten-ways-to-reverse-a-string-in-javascript/

这些实现的性能:

每个浏览器的最佳执行实现

Chrome 15 -实现1和6 Firefox 7 -实现6 IE 9 -实现4 Opera 12 -实现9

以下是这些实现:

实现1:

function reverse(s) {
  var o = '';
  for (var i = s.length - 1; i >= 0; i--)
    o += s[i];
  return o;
}

实现2:

function reverse(s) {
  var o = [];
  for (var i = s.length - 1, j = 0; i >= 0; i--, j++)
    o[j] = s[i];
  return o.join('');
}

实现3:

function reverse(s) {
  var o = [];
  for (var i = 0, len = s.length; i <= len; i++)
    o.push(s.charAt(len - i));
  return o.join('');
}

实现4:

function reverse(s) {
  return s.split('').reverse().join('');
}

实现5:

function reverse(s) {
  var i = s.length,
      o = '';
  while (i > 0) {
    o += s.substring(i - 1, i);
    i--;
  }
  return o;
}

实现6:

function reverse(s) {
  for (var i = s.length - 1, o = ''; i >= 0; o += s[i--]) { }
  return o;
}

实现7:

function reverse(s) {
  return (s === '') ? '' : reverse(s.substr(1)) + s.charAt(0);
}

实现8:

function reverse(s) {
  function rev(s, len, o) {
    return (len === 0) ? o : rev(s, --len, (o += s[len]));
  };
  return rev(s, s.length, '');
}

实现9:

function reverse(s) {
  s = s.split('');
  var len = s.length,
      halfIndex = Math.floor(len / 2) - 1,
      tmp;
 

     for (var i = 0; i <= halfIndex; i++) {
        tmp = s[len - i - 1];
        s[len - i - 1] = s[i];
        s[i] = tmp;
      }
      return s.join('');
    }

实现10

function reverse(s) {
  if (s.length < 2)
    return s;
  var halfIndex = Math.ceil(s.length / 2);
  return reverse(s.substr(halfIndex)) +
         reverse(s.substr(0, halfIndex));
}

实现11

var reverser  = function(str){
let string = str.split('');

    for(i=0;i<string.length;i++){
        debugger;
        string.splice(i,0,string.pop());
    
    } 
    console.log(string.join())
}
reverser('abcdef')

我自己最初的尝试…

var str = "The Car";

function reverseStr(str) {
  var reversed = "";
  var len = str.length;
  for (var i = 1; i < (len + 1); i++) {  
    reversed += str[len - i];      
  }

  return reversed;
}

var strReverse = reverseStr(str);    
console.log(strReverse);
// "raC ehT"

http://jsbin.com/bujiwo/19/edit?js,console,output

这样的事情应该遵循最佳实践:

(function(){ 'use strict'; var str = "testing"; //using array methods var arr = new Array(); arr = str.split(""); arr.reverse(); console.log(arr); //using custom methods var reverseString = function(str){ if(str == null || str == undefined || str.length == 0 ){ return ""; } if(str.length == 1){ return str; } var rev = []; for(var i = 0; i < str.length; i++){ rev[i] = str[str.length - 1 - i]; } return rev; } console.log(reverseString(str)); })();

String.prototype.reverse_string=function() {return this.split("").reverse().join("");}

or

String.prototype.reverse_string = function() {
    var s = "";
    var i = this.length;
    while (i>0) {
        s += this.substring(i-1,i);
        i--;
    }
    return s;
}