How do you reverse a string in place in JavaScript?

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

    First, use Array.from() to turn a string into an array, then Array.prototype.reverse() to reverse the array, and then Array.prototype.join() to make it back a string.

    const reverse = str => Array.from(str).reverse().join('');
    

提交回复
热议问题