Nodejs Array Reverse reverse(arr)

Here you can find the source of reverse(arr)

Method Source Code

(function() {//  www.ja v a  2 s  .  c o  m
'use strict'
//reverse an array of integers in place
let arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Array.prototype.reverse = Array.prototype.reverse || function(arr) {
    for(let i = 0; i < arr.length / 2; i++) {
        let opp =  arr.length - i - 1   
        arr[i] += arr[opp]
        arr[opp] = arr[i] - arr[opp]
        arr[i] -= arr[opp]
    }
}


arr.reverse()
arr.map((n) => console.log(n))
})()

Related

  1. reverse()
    Array.prototype.reverse = function () {
      var counter, 
        temp, 
        index;
      counter = this.length;
        while (counter > 0) {
            index = Math.floor(Math.random() * counter);
            counter--;
            temp = this[counter];
    ...
    
  2. reverse()
    Array.prototype.reverse = function () {
      var counter, 
        temp, 
        index;
      counter = this.length;
        while (counter > 0) {
            index = Math.floor(Math.random() * counter);
            counter--;
            temp = array[counter];
    ...
    
  3. reverse()
    Array.prototype.reverse = function () {
        for (var i = 0, j = this.length - 1; i < j; i++, j--) {
            var tmp = this[i];
            this[i] = this[j];
            this[j] = tmp;
        return this;
    };
    
  4. 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;
    };
    var input = [1, 2, 3, 4];
    input.reverse(); 
    console.log(input); 
    
  5. reverse()
    Array.prototype.reverse = function(){
      var a = this;
      for(var i =0;i<(a.length>>1);i++)
        this.swap(i,a.length-1-i);
    
  6. reverse(fn)
    Array.prototype.reverse = function(fn) {
      for (var i = this.length - 1; i >= 0; i--) fn(this[i]);
    
  7. reverse1()
    Array.prototype.reverse1 = function() {
        var arrLength = this.length;
        for(var i = arrLength-1; i>=0; i--){
            this.push(this[i]);
        this.splice(0,arrLength);
        return this;
    };
    console.log([3,2,1].reverse1());
    ...
    
  8. reverseElements()
    "use strict";
    Array.prototype.reverseElements = function () {
        var temp = this.concat();
        var i = this.length - 1;
        var target = this.length - 1;
        while (i >= 0) {
            this[target - i] = temp[i];
            i--;
        return this;
    
  9. reverseFromLToR(left,right)
    Array.prototype.reverseFromLToR = function(left,right){
        if(right >= this.length){
            return;
        while(left < right){
            var temp = this[left];
            this[left] = this[right];
            this[right] = temp;
            left++;
    ...