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,32816 silver badges32 bronze badges
asked Mar 30, 2015 at 17:25
2
  • 1
    Note that += is not how you add an entry to an array; the first time you do it, your array is replaced with a string. Commented Mar 30, 2015 at 17:28
  • 1
    ok so i changed += to push and its an array again thank you! @T.J.Crowder Commented Mar 30, 2015 at 17:32

4 Answers 4

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

answered Mar 30, 2015 at 17:27
Sign up to request clarification or add additional context in comments.

Comments

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
answered Mar 30, 2015 at 17:31

Comments

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;
}
answered Mar 30, 2015 at 17:38

Comments

-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));
answered Mar 30, 2015 at 17:36

2 Comments

Your loop does reverse the array, but adds undefined to the beginning because you're not using array.length - 1.
array.length - 1 is not needed if we replace <= with <

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.