How do you reverse a string in place in JavaScript?

前端 未结 30 2958
猫巷女王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:49

    The following technique (or similar) is commonly used to reverse a string in JavaScript:

    // Don’t use this!
    var naiveReverse = function(string) {
        return string.split('').reverse().join('');
    }
    

    In fact, all the answers posted so far are a variation of this pattern. However, there are some problems with this solution. For example:

    naiveReverse('foo 

提交回复
热议问题