Nodejs Array Reverse reverse()

Here you can find the source of reverse()

Method Source Code

Array.prototype.reverse = function() {
   for (var i = 0, j = this.length - 1; i < j; i++, j--) {
      var tmp = this[i];
      this[i] = this[j];/*from   www.j ava  2 s.c o  m*/
      this[j] = tmp;
   }
   
   return this;
};

Related

  1. reverse()
    Array.prototype.reverse = function() {
      var array = this.concat();    
      for(var i = 0; i<this.length; i++){
         this[i] = array.pop();      
      }; 
      return this;
    };
    console.log([1,2,3].reverse());
    
  2. reverse()
    Array.prototype.reverse = function() {
        for (var i=0; i<this.length/2; i++) {
            var temp = this[i];
            this[i] = this[this.length-1-i];
            this[this.length-1-i] = temp;
        return this;
    };
    console.log([1,2,3,4].reverse());
    ...
    
  3. reverse()
    Array.prototype.reverse = function() {
        var array = [];
        var length = this.length;
        for (var i = length - 1; i >= 0; i--){
            array.push(this[i])
        };
        for ( var i = 0; i < length; i ++){
            this[i] = array[i];
        return this;
    };
    
  4. reverse()
    Array.prototype.reverse = function() {
        for (i=0, j= this.length-1; i<j; i++, j--){
            var tmp = this[i];
            this[i]=this[j];
            this[j]=tmp;
        return this;
    };