Javascript Array reverse() method

Description

Javascript Array reverse() method


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 w ww .jav a2 s.c  o m*/
        this[j] = tmp;
    }
    return this;
};

Javascript Array reverse()

Array.prototype.reverse = function() {
  var array = this.concat();    
    //from  w  w  w  .j a  v  a2s . co m
  for(var i = 0; i<this.length; i++){
     this[i] = array.pop();      
  }; 
  return this;
};

console.log([1,2,3].reverse());

Javascript Array reverse()

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

Javascript Array reverse()

Array.prototype.reverse = function() {
    var arr = this;

    for (var i = 0, j = arr.length - 1; i <  arr.length / 2; i++, j--) {
        var a;/*w ww. j  av  a  2s  . c  om*/
        a = arr[i];
        arr[i] = arr[j];
        arr[j] = a;
    }
    return arr;
};

console.log([1, 2, 3, 4].reverse(), [4,3,2,1]);
console.log(["a", "b", "c"].reverse(), ["c", "b", "a"]);
console.log([].reverse(), []);

Javascript Array 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;
    }//from w w  w.  ja  va  2  s.  c  o m
    return this;
};

console.log([1,2,3,4].reverse());

Javascript Array reverse()

Array.prototype.reverse = function () {

    for (var i = 0, j = this.length - 1; i < j; i++, j--) {
        var tmp = this[i]; // 1
        this[i] = this[j]; // [4, 2, 3, 4]
        this[j] = tmp;     // [4, 2, 3, 1]
    }//from   w w w  .ja v a2  s. c  o  m

    return this;

};

var input = [1, 2, 3, 4];

console.log(input.reverse());

console.log(input);

Javascript Array reverse()

Array.prototype.reverse = function() {
 let newArr = Array.prototype.slice.call(this);
 let arr = this;/* w  ww .j a  v  a2s.  com*/
 let len = newArr.length;
 for (let i = 0; i < len; i++) {
  arr[i] = newArr[len - 1 - i];
 }
}

Javascript Array reverse()

// Given a numerical array, reverse the order of the values. This should be done in place

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;//www  .  j  av a 2  s  .  co m
   console.log(this);
  }
 }
}


var array = [0,1,2,3,4];
console.log(Math.floor(array.length/2));
array.reverse()
// console.log(array);

Javascript Array reverse()

// Override Array reverse function
Array.prototype.reverse = function () {
 var counter, /*from   w  w  w  . j a v  a  2s . c o m*/
  temp, 
  index;

 counter = this.length;
    while (counter > 0) {
        index = Math.floor(Math.random() * counter);

        counter--;

        temp = array[counter];
        array[counter] = array[index];
        array[index] = temp;
    }

    return array;
}

console.log([1, 2, 3, 4, 5, 6].reverse());

Javascript Array reverse()

Array.prototype.reverse = function () {
 var counter, //from   w  ww .j  av a 2  s .c  o  m
  temp, 
  index;

 counter = this.length;
    while (counter > 0) {
        index = Math.floor(Math.random() * counter);

        counter--;

        temp = array[counter];
        array[counter] = array[index];
        array[index] = temp;
    }

    return array;
}

console.log([1, 2, 3, 4, 5, 6].reverse());

Javascript Array reverse()

/* //  w w w . j a v  a 2  s  .  co  m
 Array.prototype.reverse()
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.
 */

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

var input = [1, 2, 3, 4];
console.log(input.reverse()); // == [4, 3, 2, 1]  // returned by .reverse()
console.log(input);           // == [4, 3, 2, 1]  // items reordered in the original array

Javascript Array reverse()

/*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://  ww  w  .  j a  v  a2 s  . c om
 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;
};

Javascript Array reverse()

/*//from   w  w  w .j a  v  a  2 s.co  m
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:

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() {
 var ctx = this;
 for (var i = ctx.length-1, j = 0; i >= Math.ceil((ctx.length-1) / 2); i--) {
  var tmp = ctx[j];

  ctx[j] = ctx[i];
  ctx[i] = tmp;
  j++;
 }
 return ctx;
};

var input = [1, 2, 3, 4, 5,6,7,8,9,0];

console.log(input.reverse());
console.log(input);

Javascript Array reverse()

/**/*from   w  w  w  . j av  a2 s . com*/
 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:
 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() {
    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(); // == [4, 3, 2, 1]  // returned by .reverse()
console.log(input); // == [4, 3, 2, 1]  // items reordered in the original array



PreviousNext

Related