DEV Community

loading...

Discussion on: Reverse a String - Four JavaScript Solutions

Collapse
bugmagnet profile image
Bruce Axtens

So after doing .call(), let's do .apply(), viz

Array.prototype.flip = function () {
    for (var i = 0; i < this.length / 2; i++) {
        var tmp = this[this.length - i - 1];
        this[this.length - i - 1] = this[i];
        this[i] = tmp;
    }
    return this;
}

var original = "lewd i did live - evil did i dwel";

var reversed = [].flip.apply(original.split("")).join("");

Okay, maybe it's time to do some real work now.