Nodejs Array Reverse reverse()

Here you can find the source of reverse()

Method Source Code

/*The Array's reverse() method has gone missing! Re-write it, quick-sharp!
 When this method is called, it reverses the order of the items in the
 original array. Then then it returns that same, original array. No new arrays
 should need to be created to pass this kata.
 Here's an example://from ww w . j a  v a2 s  . co m
 var input = [1, 2, 3, 4];
 input.reverse(); // == [4, 3, 2, 1]  // returned by .reverse()
 input;           // == [4, 3, 2, 1]  // items reordered in the original array */

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;
};

Related

  1. 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;
    };
    
  2. reverse()
    Array.prototype.reverse = function() {
      if (this.length > 1) {
        for (var i = 0; i < Math.floor(this.length/2); i++){
          var temp = this[i];
          this[i] = this[this.length-1-i];
          this[this.length-1-i] = temp;
          console.log(this);
    var array = [0,1,2,3,4];
    console.log(Math.floor(array.length/2));
    array.reverse()
    
  3. 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];
    ...
    
  4. 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];
    ...
    
  5. 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];
    ...
    
  6. 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); 
    
  7. reverse()
    Array.prototype.reverse = function(){
      var a = this;
      for(var i =0;i<(a.length>>1);i++)
        this.swap(i,a.length-1-i);
    
  8. reverse(arr)
    (function() {
    'use strict'
    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]
    ...
    
  9. reverse(fn)
    Array.prototype.reverse = function(fn) {
      for (var i = this.length - 1; i >= 0; i--) fn(this[i]);