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!
array.lengthis different after each iteration, since you'reshifting your array. Put its value in a variable before the loop, and use this value in the condition. By the way, there is areversemethod.var v; while(v = a.shift()) b.unshift(v);