How do you reverse a string in place in JavaScript?

前端 未结 30 2428
猫巷女王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条回答
  •  忘掉有多难
    2020-11-22 00:39

    function reverseString(string) {
        var reversedString = "";
        var stringLength = string.length - 1;
        for (var i = stringLength; i >= 0; i--) {
            reversedString += string[i];
        }
        return reversedString;
    }
    

提交回复
热议问题