How do you reverse a string in place in JavaScript?

前端 未结 30 2343
猫巷女王i
猫巷女王i 2020-11-22 00:29

How do you reverse a string in place (or in-place) in JavaScript when it is passed to a function with a return statement, without using built-in functions (.reverse()<

30条回答
  •  -上瘾入骨i
    2020-11-22 00:56

    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;
    }
    

提交回复
热议问题