Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Write a recursive function that computes the sum of all numbers from 1 to n, where n is given as parameter.
- function recSumN(n){
- if (n == 1) return 1;
- return n + recSumN(n-1);
- }
- console.log(recSumN(4));
- //should return 10
- //progression:
- // r(4)
- // returns 4 + r(3)
- // returns 3 + r(2)
- // returns 2 + 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement