Reversing a string in JavaScript

后端 未结 15 2014
野的像风
野的像风 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:46
    <!DOCTYPE html>
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title></title>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
        <script>
    
    
            $(document).ready(function () {
    
                $("#stringInput").keyup(function () {
                    debugger;
                    var string = $("#stringInput").val();
                    var stringArray = [];
                    sInput = string.toString();
    
                    for (var i = 0, len = sInput.length; i < len; i++) {
    
                        stringArray.push(sInput.charAt(i));
                    }
                    stringArray.reverse();
                    str = stringArray.join('');
    
    
                    document.getElementById("stringOutput").value = str;
    
                });
    
                $("button").click(function () {
                    debugger;
                    $("#myForm").toggle();
                });
            });
    
        </script>
    </head>
    
    <body>
    
         <div>
            <form name="myForm" id="myForm">
    
        <table>
            <tr>
                <td>Insert Elements :</td>
                <td><input type="text" name="stringInput" id="stringInput"/></td>
    
                <td>Output :</td>
                <td><input type="text" id="stringOutput" name="stringOutput" readonly="true" /></td>
            </tr>
    
        </table>
          </form>
             <button>Show/Hide</button>
        </div>
    </body>
    </html>
    
    0 讨论(0)
  • 2020-11-28 04:46

    This reverse prototype function is implemented using "this". If you see log console of "this", it will generate the array, and it has length property. So that it!!! Just use reverse "for-loop" as shown in the code snippet.

    String.prototype.reverse = function () {
        console.log(this);
        var result = "";
        var len = this.length;
        
        for (i = (len-1); i >= 0 ; i--) {
            result += this[i];
        }
        return result;
    };
    
    alert("elahnu jaknap".reverse());

    0 讨论(0)
  • 2020-11-28 04:47

    I think you'll find that in fact reverse() isn't a function in jQuery. Incidentally, jQuery is really good at manipulating your DOM, but isn't really for string manipulation as such (although you can probably get plugins/write your own) to do this.

    The best way I've found to reverse a string in javascript is to do the following:

    String.prototype.reverse = function(){
    splitext = this.split("");
    revertext = splitext.reverse();
    reversed = revertext.join("");
    return reversed;
    }
    

    Found at: http://www.bytemycode.com/snippets/snippet/400/

    I think you'll find that if you pop the above into your code somewhere, your call to .reverse() should work :)

    0 讨论(0)
提交回复
热议问题