0

I created on recursion program to find of sum of all values till the number via recursion(with return values). Then I thought of making same program by passing the total as argument. this one didnt work. When I tried debugging i found that last else(when n==1) takes the porgram to loader.js. I am new and couldn't understand what is happening here. Looking for your expert feedback.

function sum(n, total = 0) {
  console.log(n, total);
  if (n !== 1) {
    console.log("insie IF");
    total += n;
    sum(n - 1, total);
  } else {
    console.log("inside ELSE");
    total += 1;
  }
  return total;
}
  • What do you mean by "*takes the porgram to loader.js.*"??? – Bergi Jan 13 '19 at 13:43
  • 2
    How are you calling this function? What's the output / return value? What did you expect instead? – melpomene Jan 13 '19 at 13:44
  • Could you please also post the working version? – Bergi Jan 13 '19 at 13:44
  • 1
    You are ignoring the return value of the recursive call `sum(n - 1, total);`. – Bergi Jan 13 '19 at 13:45
  • Your problem can be simplified to: `function foo(x) { x += 1; } function bar(x) { foo(x); return x; } console.log(bar(0));` This outputs `0`, not `1`. – melpomene Jan 13 '19 at 13:46
  • bergi, the debugger opens loader.js, melpomene I am calling as sum(3) and expect 6 as answer. I am working on node 10.13 – Sachin Singh Jan 13 '19 at 13:49
  • @Bergi Yes I am ignoring the return value as I dont need it. I am using total for getting the total value. – Sachin Singh Jan 13 '19 at 13:51
  • 1
    @SachinSingh Well that's where you are wrong. `total` is a variable that is local to each call, you are not passing a reference that can be mutated. You *do need* the return value. – Bergi Jan 13 '19 at 13:55
  • @Bergi I think the problem comes from not understanding how tail-recursion works, not necessarily from pass-by-reference or pass-by-value problems, but maybe it's just me. I was preparing an answer explaining that. – Adrian Pop Jan 13 '19 at 14:00
  • @Bergi I think total is not local, Its defined at the top of scope in function call so for rest all function calls it will be accessible. Also I checked in debugger that it totals the value till expected (total -1) but gets stuck for last else case – Sachin Singh Jan 13 '19 at 14:03
  • 1
    @SachinSingh Every call to `sum` has its own (local) `total` variable. Updating one of them doesn't change the others. – melpomene Jan 13 '19 at 14:03
  • @SachinSingh If you don't believe me, put `console.log(n, total);` before `return total;` and observe the output. – melpomene Jan 13 '19 at 14:04
  • Yes, exactly as @SachinSingh said. The answers are correct, but the problem is that the final return will actually come from the first call, not the last. How I said, I think the problem comes from not understanding how tail-recursion works. – Adrian Pop Jan 13 '19 at 14:09
  • @AdrianPop This code doesn't contain tail recursion. – melpomene Jan 13 '19 at 14:09
  • It doesn't mean he did not think of doing that. – Adrian Pop Jan 13 '19 at 14:19
  • 1
    @Bergi and all, thanks for help, I got the issue the other program I saw with recursion has array passed. Since Array is reference type so it was working fine and here we have total is primitive type so the issue with final result. got it – Sachin Singh Jan 13 '19 at 14:53
  • `const sum = (n, total = 0) => n === 0 ? total : sum (n - 1, total + n)` then `sum(10)` returns `55`. – Mulan Jan 14 '19 at 18:21

1 Answers1

-2

You need to capture the return value from the recursive call to sum. total = sum(n - 1, total);

Andy Lamb
  • 2,151
  • 20
  • 22