reverse Array - Node.js Array

Node.js examples for Array:Reverse Array

Description

reverse Array

Demo Code


function reverseArray(array) {
  var finalArray = []
  for (var i=array.length-1;i>=0;i--) {
    finalArray.push(array[i])/*ww  w  .  j  a  v a 2s  . co m*/
  }
  return finalArray;
}

function reverseArrayInPlace(array) {
  for (var i=0;i<Math.floor(array.length/2);i++) {
    var temp = array[array.length-1-i];
    array[array.length-1-i] = array[i];
    console.log(array[i],temp)
    array[i] = temp;
  }
  return array;
}

console.log(reverseArray(["A", "B", "C"]));
var arrayValue = [1, 2, 3, 4, 5];
reverseArrayInPlace(arrayValue);
console.log(arrayValue);

Related Tutorials