Reversing a string in JavaScript

后端 未结 15 1971
野的像风
野的像风 2020-11-28 03:58

I\'m trying to reverse an input string

var oneway = document.getElementById(\'input_field\').value();
var backway = oneway.reverse();

but f

15条回答
  •  执笔经年
    2020-11-28 04:39

    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 

提交回复
热议问题