1

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;
}
5
  • 1
    if(arr.length = 1) WRONG and if(arr.length = 0) wrong, You are assigning Commented Nov 12, 2014 at 17:22
  • Deleted my answer because I realised you were meant to use recursion. Commented Nov 12, 2014 at 17:31
  • 1
    Even if your function worked, your function returns an array so after one iteration, you'll have this [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 since arr.reverse() already exists to do this. Commented Nov 12, 2014 at 17:59
  • if (arr.length < 2) return arr; Commented Nov 12, 2014 at 20:11
  • 1
    possible duplicate of Reverse array function with one parameter. (JavaScript) Commented Nov 13, 2014 at 4:51

3 Answers 3

2
x = y <--assignment
z == y <-- comparison 

Looking at your code:

if(arr.length = 1) 

needs to be

if(arr.length == 1)

same with the zero check


AND you are not calling pop

var head = arr.pop;

you need to parenthesis

var head = arr.pop();
Sign up to request clarification or add additional context in comments.

Comments

2

This is the most precise and clean way to do it in a single line with the ternary operator.

function reverse(arr) {
  return arr.length < 2 ? arr : [arr.pop()].concat(reverse(arr));
}
console.log(reverse([4, 3, 3, 1]));

Comments

2

Here's a runnable example with a working function. FYI there is also a built-in function that will do this for you.

I think other than the confusion with assignment/comparison operators, when you were building the result array, you should be using Array.concat() to concatenate your array, instead you were building a new array where the second item was an array itself.

var a = [1,2,3,4,5];
console.log(ReverseArray(a));

function ReverseArray(arr) {
    if(arr.length < 2) {
        return arr;
    } else {
        return [arr.pop()].concat(ReverseArray(arr));
    }
}

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.