0

Wondering why i needed to add 4 to the array length in order for it to print out the entire array in reverse?

before i added 4 it was just using the .length property and it was only printing out 6543.

thanks in advance!

function reverseArray(array) {
    var newArray =[];

     for(var i = 0; i <= array.length+4; i++) {
         newArray += array.pop(i);
      }
     return newArray;
 }
var numbers = [1,2,3,4,5,6];
console.log(reverseArray(numbers));
xsami
  • 1,312
  • 16
  • 31
jordan
  • 305
  • 1
  • 7
  • 17

4 Answers4

5

array.pop removes (and returns) the last element. This affects the length of the array. The length is checked on every iteration, so since the array is getting shorter every time, the loop is ended early.

You can create a loop and pop items until it is empty, but another thing to take into account, is that it is the original array you are altering. I think a function like reverseArray shouldn't alter the array numbers that was passed to it if it returns another one. So a better solution would be a simple loop that iterates over all items without modifying the array.

function reverseArray(array)
{
  var newArray =[];

  for (var i = array.length-1; i >= 0; i--) {
    newArray.push(array[i]);
  }
  return newArray;
}

var numbers = [1,2,3,4,5,6];
console.log(reverseArray(numbers));
console.log(numbers); // Should be unaltered.

If you don't mind modifying the array, you can use the reverse() method of the array:

var numbers = [1,2,3,4,5,6];
numbers.reverse();
console.log(numbers);
GolezTrol
  • 114,394
  • 18
  • 182
  • 210
0

In Javascript, pop always removes the last element of the array. This shortens length, meaning that i and array.length were converging.

You can do a few things to avoid this behavior:

  1. Store the original length when you start the loop: for (var i = 0 , l = array.length; i < l; i++)
  2. Copy over values without modifying the original array
Andrew Larson
  • 483
  • 2
  • 7
0

When you pop the items from the array, the item is removed from the array. As you increase the counter and decrease the length, they will meet halfway, so you get only half of the items.

Use push to put the items in the result. If you use += it will produce a string instead of an array.

If you use pop, then you can just loop while there are any items left in the array:

function reverseArray(array) {
    var newArray = [];
    while (array.length > 0) {
        newArray.push(array.pop());
    }
    return newArray;
}

You can leave the original array unchanged by looping through it backwards and add items to the new array:

function reverseArray(array) {
    var newArray = [];
    for (var i = array.length - 1; i >= 0; i--) {
        newArray.push(array[i]);
    }
    return newArray;
}
Guffa
  • 687,336
  • 108
  • 737
  • 1,005
-1

use following method for same output

function reverseArray(array) {

    var newArray =[];
    var j = array.length-1;

    for(var i = 0; i < array.length; i++)

    {

        newArray[j]= array[i]; j--;

    }

    return newArray;

}

var numbers = [1,2,3,4,5,6];

console.log(reverseArray(numbers));