0

I referred this link (What is tail recursion?) but i can't get the answer by applying those steps ,can anyone explain please?

let sum = 5;
function sumTo(n) {

    if(n==1) return n;
    else {
        return sum + sumTo(n-1);
    }
}
let ss = sumTo(5);
Sesha
  • 189
  • 1
  • 4
  • Its explained well in accepted answer – Satpal Nov 15 '17 at 12:18
  • Such title is really useless. – qxg Nov 16 '17 at 04:42
  • This is not a duplicate,look briefly the question first or run above question and analysis briefly @satpal – Sesha Nov 16 '17 at 04:42
  • look *closely* at this line in your code: `return sum + sumTo(n-1);`, and compare it to the example in the duplicate question: `return x + recsum(x-1);`. In your code, you are trying to return the sum of the outside value every time, whereas in the other example, the sum is on the passed in value, making it cumulative. – Claies Nov 16 '17 at 04:50
  • Thanks for all suggestion, finally i understood and came to answer my self – Sesha Dec 20 '17 at 10:01
  • thank you claies. Here the answer for above question : 5 + sum(4); 5 + 5 + sum(3); 5 + 5 + 5 + sum(2); 5 + 5 + 5 + 5 + sum(1); 5 + 5 + 5 + 5 + 1; – Sesha Dec 20 '17 at 10:03

0 Answers0