-1

just trying to find the sum in the array and can't figure out what is wrong with my code, if you can help that would be appreciated :)

function arraySum(arr, index) {
    if (arr.length <= index)
        return 0;
    return (arraySum(arr, arr.length - 1) + arr[arr.length - 1]);

}

I'm trying to find the sum of the array using recursion. what is exactly wrong with my code :) if you can help that would be appreciated

ps index=0 arr.length =8

  • 3
    Is this a homework problem? Recursion doesn't seem like the best way to do this. – mykaf Mar 30 '22 at 15:10
  • `arr.length - 1` will always be the same right? – 0stone0 Mar 30 '22 at 15:12
  • Use a `console.log()` to print the `arr.length` and `index` to debug the logic. How are you calling this function? If you're calling it with `[1,2,3], 3`, then you can see the base case is hit right away. Why not increase the index from 0 to `arr.length - 1`? The initial call is `(arr, 0)` and you do `index + 1` per recursive call with a base case of `index >= arr.length`. Also, this is a poor usage of recursion -- you'll blow the stack if your array is a few thousand elements. – ggorlen Mar 30 '22 at 15:13
  • 3
    are you trying to find the max value or the sum? – yih613 Mar 30 '22 at 15:13
  • arr.length = 8, i'm tryinig to find the sum sorry, typo – EverProgrammer Mar 30 '22 at 15:16
  • Issue in return call , i submitted the answer below – Sameh Mar 31 '22 at 03:20

5 Answers5

0
function arraySum(arr, index) {
    if (index >= arr.length)
        return 0;
    return (arraySum(arr, index + 1) + arr[index]);
}

arr.length - 1 is the same, what you need to do is to iterate through the array, to make it work recursively. recursion needs a terminating point. in your code you are passing arr.length - 1 as index at every call, which remains constant.

  • why don't you use reduce to get the sum of array , why are you doing recursion in the first place: function sum(arr) { const reducer = (sum, val) => sum + val; const initialValue = 0; return arr.reduce(reducer, initialValue); } sum([1, 3, 5, 7]); // 16 – Sameh Mar 30 '22 at 15:58
0

First of all, in the recursion patterns you need a break condition.

Second, in each step of the recursion you need to take a little piece of work, and pass the rest to the others.

You have the break condition but it seems like your little piece of work is missing or not done properly.

Here is how to do that:

var sum = function(arr, currentIndex) {
   if(currentIndex < 0 || currentIndex >= arr.length) return 0;
   return arr[currentIndex] + sum(arr, ++currentIndex);
};

//Test it
var arr = [1, 4, 5];
sum(arr, 0);
Marco Merola
  • 131
  • 2
  • 4
  • `function arraySum(arr, index) { if(index < 0 || index >= arr.length) return 0; return arr[index] + arraysum(arr, ++index); }` This didn't work properly but i feel like getting closer – EverProgrammer Mar 30 '22 at 15:40
0

Note: There are much better ways of achieving the desired goal:
How to find the sum of an array of numbers


To answer OP's question, here's an other approach using pop():

  • If there are more then items in the array (arr.length > 1)

    • Return recursive result:
      (arr.pop() + arraySum(arr))
  • If this was the last item, just return the last value
    arr.pop()

Combining this in a short-if-statement gives us:

function arraySum(arr) {
    return (arr.length > 1) ? (arr.pop() + arraySum(arr)) : arr.pop();
}

function arraySum(arr) {
    return (arr.length > 1) ? (arr.pop() + arraySum(arr)) : arr.pop();
}

const arr = [ 1, 2, 3 ];
const res = arraySum(arr);
console.log(res); // 6
0stone0
  • 34,288
  • 4
  • 39
  • 64
0

I tried the following snippet of code and it is running

function arraySum(arr, index) {
  if (index >= arr.length) {
    return 0;
  }
  return arr[index] + arraySum(arr, index + 1)  ;
}
const x = arraySum([1, 2, 3], 0);
console.log(x);

enter image description here

Since this is a simple array if you just need sum you can use reduce method

function sum(arr) {
  const reducer = (sum, val) => sum + val;
  const initialValue = 0;
  return arr.reduce(reducer, initialValue);
}

sum([1, 3, 5, 7]); // 16
Sameh
  • 1,318
  • 11
  • 11
-2

function findSum(A, N) {
    if (N <= 0)
        return 0;
    return (findSum(A, N - 1) + A[N - 1]);
}

 
let A = [1, 2, 3, 4, 5];
let N = A.length;
document.write(findSum(A,N));
0stone0
  • 34,288
  • 4
  • 39
  • 64