So we are practicing functional javascript in my programming class with this assignment and I just can't get it to work right. Any advice in making this code work would be appreciated. Everything but the body was given for me to use. Here is what I have come up with: (It is always sending me just the first array index content rather than all of them reversed. I tried changing it to
if(arr.length <= 1) return arr;
but that never hits the base case.)
function ReverseArray(arr) {
//base case
if(arr.length == 1)
{
return arr[0];
}
if(arr.length == 0)
{
return 0;
}
var head = arr.pop;
var newArr = [head, ReverseArray(arr)];
return newArr;
}
[end0, [....]]. After two iterations, you'll have this[end0, [end1, [...]]]and it will keep nesting arrays further and further. Your approach will not work. You need a new design. Perhaps you want to use.concat()to add two arrays together rather than embedding one in another. If you want help with a new design, you will have to better describe the rules of the assignment sincearr.reverse()already exists to do this.if (arr.length < 2) return arr;