DEV Community

loading...

Discussion on: Reverse a String - Four JavaScript Solutions

Collapse
bugmagnet profile image
Bruce Axtens • Edited

Actually, you could take the recursion along a bit further and make it a prototype of String, viz:

String.prototype.flip = function () {
  return (this.length === 1) 
    ? this 
    : this.substr(1).flip() + this.substr(0, 1);
};

var original = "lewd i did live - evil did i dwel";
var reversed = original.flip();

Someone'll beat me to a blog post, likely.

Collapse
abraham profile image
Abraham Williams

It's generally recommended that you should not extend native prototypes.

developers.google.com/web/updates/...

Collapse
markwaterous profile image
Mark

shakes fist at MooTools