2

Can someone please explain to me why we need (n-1) in the following code.

  function multiply(arr, n) {
    if (n <= 0) {    
      return 1;
    } else {
      return multiply(arr, n - 1) * arr[n - 1];
    }
  }

I understand that we have a base case of if (n <= 0){return 1} inorder for the code to not loop for ever but I dont understand the (n-1) and [n-1] in the recursive case of return multiply(arr, n - 1) * arr[n - 1]; .

Any help is much appreciated.

2
  • What else would you write there? Are you suggesting that passing n instead of n-1 would also work? Commented Oct 3, 2021 at 17:08
  • "I understand that we have a base case inorder for the code to not loop for ever" - well, could explain in your own words how this "looping" works, how you would reach that base case? Commented Oct 3, 2021 at 17:09

3 Answers 3

1

It looks like this function is meant to start with the last element of the array and recursively operate on each earlier element. This is why when calling the function recursively, you must pass in the next earlier element, i.e. n-1. This moves the function closer to the base case with each iteration.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your help, i understand it now
This can be much easier written with Array.prototype.reduce()
0

This function just multiplies all elements of the given array. Initially you must pass the array and it's length as n.

The last element for any array is n-1. Thus on the 1st iteration it will take the last element and multiply further until it reaches 0. Then it will stop.

Just try to run this function mentally in your head on some simple examples and you'll get it.

1 Comment

Thanks for you help and advice i will do as you said
0

The function could be improved, but you can understand it as-is by adding some debug logging...

function multiply(arr, n) {
  if (n <= 0) {
    return 1;
  } else {
    console.log(`recurse to multiply ${arr[n-1]} by elements in [${arr.slice(0,n-1)}]`);
    return multiply(arr, n - 1) * arr[n - 1];
  }
}

multiply([1, 2, 3], 3);

A clearer implementation wouldn't require the length param, and be clearer about decomposition...

// if the array has a first element, multiply it by the remainder of the array
function multiply(arr) {
  return arr.length ? arr[0] * multiply(arr.slice(1)) : 1;
}

console.log(multiply([1,2,3]))

2 Comments

Your "better implementation" has a time complexity of O(n²) however, in contrast to the linear complexity of the original function.
Thanks, @Bergi. I considered that but figured it was more valuable for explaining recursion with the general form: "handle the first and recurse with the rest". Edited to say what I meant by "better".

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.