0

Somebody please explain me this code in detail

function reverse(s){
   return s.split("").reverse().join("");
}
3
  • 4
    What part of it don't you understand? Commented Feb 25, 2012 at 22:53
  • It assumes the input to be a string. Given the input, the string is split using an empty string as delimiter. The result is an array, consisting of individual characters. Then, the reverse method of Array is used to reverse the order of the array's elements. Then, the string is concatenated again, using an empty string. Commented Feb 25, 2012 at 22:55
  • Sorry all, I misunderstood the function. I thought reverse() is being called from the same function.. Thanks for answers. Commented Feb 25, 2012 at 23:09

1 Answer 1

1

The function takes a string, it splits it in its constituent characters to obtain an array using the split function, it reverses this array using the reverse method and joins the elements with an empty string.

Basically it reverses a string which given the name of the function I guess doesn't come as a surprise to anyone.

So:

reverse('abc')

will return:

cba
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.