3

This is my code:

function reverseArray(array){
  var newArray = [];
  for (i = 0; i <= array.length + 1; i++)
    newArray.unshift(array.shift());
  return newArray;
};

I don't understand why in the for loop the condition isn't i < array.length. For example, when the array has 3 elements, it seems to me that you would need to loop over the array 3 times, shifting each element into the new array, but for some reason on the consoles when I try it (for example console.log(reverseArray(["a", "b", "c"]))), I had to change it to the current i <= array.length + 1; to get the code to give the correct output ["c", "b", "a"]. I do not understand why, if someone could help explain why i < array.length doesn't work I would really appreciate it. Thanks!

3
  • 3
    array.length is different after each iteration, since you're shifting your array. Put its value in a variable before the loop, and use this value in the condition. By the way, there is a reverse method. Commented Aug 2, 2014 at 23:31
  • Thanks! I understand now. I do know about the reverse method, this was an exercise for a tutorial. Commented Aug 2, 2014 at 23:40
  • can also do var v; while(v = a.shift()) b.unshift(v); Commented Aug 2, 2014 at 23:48

2 Answers 2

2
  1. your code is error in the if condition check, because every time the condition is be checked in the for statement, so the array.lenght is changed every time, and the condition should not be array.length + 1, you can try the code below

    function reverseArray(array){
      var newArray = [];
      for (var i = 0,len=array.length; i < len; i++)
        newArray.unshift(array.shift());
      return newArray;
    };
    
  2. I suggest to use the reverse method of the Array, but if you want to make a new copy of the array, you can use Array.slice(), try this:

    function reverseArray(array){
        var newArray=array.slice()
        newArray.reverse()
        return newArray
    }
    
Sign up to request clarification or add additional context in comments.

2 Comments

Because you know the length of the array, you can make this function a lot more performant. Please see these benchmarks to see how the solutions compare. Just to give you a basic idea, my solution is only slightly different than yours but it's over 10x faster. I highly encourage that all people learn performance costs of dynamically expanding arrays.
I know the performance problem of using the unshift and splice method, I recommend to use reverse method, but I test the benchmarks, it seems use your method have the best performance.
1

For whatever reason, if you can't use Array.prototype.reverse, I would write the function like this

function reverseArray(arr) {
  var len = arr.length;
  var rev = new Array(len);
  for (var i=0, j=len-1; i<len; i++, j--) {
    rev[i] = arr[j];
  }
  return rev;
}

reverseArray([1,2,3]);
// [3,2,1]

You can see this solution dominate the benchmarks. It's even faster than the native Array.prototype.reverse using a 10-element array (tested in Chrome).

Comments

Your Answer

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.