Friday, May 17, 2024
 Popular · Latest · Hot · Upcoming
136
rated 0 times [  143] [ 7]  / answers: 1 / hits: 15708  / 12 Years ago, sun, july 29, 2012, 12:00:00

I want the text to be printed without commas.



<html>
<head>
<title>Reverse</title>
</head>
<body>
<form name=rev>
Enter the string : <input type=text name=str/>
<input type=button value=click onclick=rev1() /><br>
reverse of given string : <input type=text name=res/>
</form>
<script type=text/JavaScript>
function rev1(){
var a=rev.str.value;
var b=[];
var i,j=0;
for(i=a.length-1;i>=0;i--){
b[j]=a[i];
j++
}
//rev.res.value=b;
alert(b);
}
</script>
</body>
</html>


If I give the input as abc I am getting an output as c,b,a, but I want it as cba.


More From » javascript

 Answers
27

  1. You may reverse your string using javascript built-in functions:



            <html>
    <head>
    <title>Reverse</title>
    </head>
    <body>
    <form name=rev>
    Enter the string : <input type=text name=str/>
    <input type=button value=click onclick=rev1() /><br>
    reverse of given string : <input type=text name=res/>
    </form>
    <script type=text/JavaScript>
    function rev1(){
    var a=rev.str.value;
    var b=a.split().reverse().join();
    //rev.res.value=b;
    alert(b);
    }
    </script>
    </body>
    </html>

  2. You may also transfer join your array elements to become a string



            <html>
    <head>
    <title>Reverse</title>
    </head>
    <body>
    <form name=rev>
    Enter the string : <input type=text name=str/>
    <input type=button value=click onclick=rev1() /><br>
    reverse of given string : <input type=text name=res/>
    </form>
    <script type=text/JavaScript>
    function rev1(){
    var a=rev.str.value;
    var b=[];
    var i,j=0;
    for(i=a.length-1;i>=0;i--){
    b[j]=a[i];
    j++
    }
    //rev.res.value=b;
    alert(b.join());
    }
    </script>
    </body>
    </html>


[#83996] Friday, July 27, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
blaynetheoi

Total Points: 146
Total Questions: 116
Total Answers: 103

Location: India
Member since Thu, Apr 8, 2021
3 Years ago
;