-2

Define a function reverse() that computes the reversal of a string. For example, reverse("jag testar")should return the string "ratset gaj".As I am getting errors and I am new to javascript .I got the code .but it is not executing

//my code 
function (string)
{
    var length = string.length;
    var reversed = [];
    var joined = ("");
    for (var i = length; i > 0; i--){
        reversed.push(string.charAt(i-1));
    }

    for (i = 0; i < (length) ; i++){
        joined += (reversed[i]);

    }

    return joined ;
Barmar
  • 741,623
  • 53
  • 500
  • 612

3 Answers3

0

Point 1: your function is not well declared:

function (string)

The correct way to declare functions in javascript is:

function functionName(varName)

Point 2 (i guess typo): your function must end with } if not will throw error.

So in your concrete case, if you replace your function for this:

function reverse(s){
    return s.split("").reverse().join("");
}

And call it in this way:

var yourString = "ABCDE";
var reversedString = reverse(yourString);  

// reversedString = "EDCBA"

To understand how it works:

  • It splits the string into single char array with split(""), so "ABCDE".split("") = ["A","B","C","D","E"]
  • The arrays:reverse() function of invert the order, so ["A","B","C","D","E"].reverse() = ["E","D","C","B","A"].
  • join("") reconstructs the string again, so ["E","D","C","B","A"].join("") = "EDCBA"
Jordi Castilla
  • 26,609
  • 8
  • 70
  • 109
0

in the OP:

function (string)

In a function declaration, a name is mandatory, so:

function reverse(string)

{
    var length = string.length;

There is no need for length, just initialise i in the for loop to string.length.

    var reversed = [];
    var joined = ("");

There is no need for the grouping operator:

    var joined = "";

But there's no need for joined at all.

    for (var i = length; i > 0; i--){
        reversed.push(string.charAt(i-1));
    }

    for (i = 0; i < (length) ; i++){
        joined += (reversed[i]);
    }

why put the characters into an array, then iterate over the array to put them back into a string? Immediately after the first loop you could do:

  return reversed.join('');

    return joined ;

You omitted the closing bracket:

}

As noted in comments, you can use split, reverse and join, or a recursive function. You could also use split and forEach, but probably faster to use a plain loop as you've done:

function reverse(s) {
  var result = '',
      i = s.length;

  while (i) {
    result += s[--i];
  }
  return result;
}

Though += can be slow in some older browsers. Maybe test performance - oh look, it's been done… and a plain loop is fastest by a long way. ;-)

RobG
  • 142,382
  • 31
  • 172
  • 209
-2
<!DOCTYPE html>
<html>
<body>

<p>Click the button to reverse the order of the elements in the array.</p>

<button onclick="myFunction()">Try it</button>

<p id="demo"></p>

<script>
var fruits = ["Banana", "Orange", "Apple", "Mango"];
document.getElementById("demo").innerHTML = fruits;

function myFunction() {
    fruits.reverse();
    document.getElementById("demo").innerHTML = fruits;
}
</script>

</body>

vikas
  • 7
  • 4
  • 2
    But...the question is about a string, not an array. – nnnnnn Mar 03 '15 at 11:16
  • 2
    Welcome to [so]. It appears that you have copied this from [W3Schools](http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_reverse) without attribution. When using content from other sources, please provide attribution (by providing a link to it) and note that any relevant content is from that source. For more information, please read the [referencing guidelines](http://stackoverflow.com/help/referencing). Thanks! – Qantas 94 Heavy Mar 03 '15 at 11:22
  • thanks to giving attention to me – vikas Mar 03 '15 at 11:37