# Summing It Up explainer

Aim of the puzzle: Use a function that calls itself recursively.
Walk through of solution: The function `sumUpTo()` takes a number as an argument, and returns the sum of every number up to that number. For example, `sumUpTo(5)` will return `15`, as `5 + 4 + 3 + 2 + 1 = 15`.

To complete the puzzle, change the code block inside the else statement from `return number + sumUpTo(number)` to `return number + sumUpTo(number - 1)`.

Let’s take a look inside the function to see how it works.

Inside the function is an if…else statement. The if statement tests if the number passed into the function is less than or equal to 1. If this is true, the function will return 1. However, if the number is greater than 1, the code in the else block will run.

Inside the else block is where things get a bit tricky. The function will take the number, and add it to the result of calling the function again, this time with `number - 1` as an argument.

This will make more sense if we follow a number through the function. Let’s try calling the function with `3` as an argument: `sumUpTo(3)`:

The if statement will check if 3 is less than or equal to 1. Because 3 is greater than 1, the code in the else block will run instead. This will return `3 + sumUpTo(3-1)`, calling the function again with `2` as the number argument.

Our equation is now `3 + sumUpTo(2)`

The if statement will check the number again, looking to see if 2 is less than or equal to 1. This is still false, so the else block will run again, this time returning `2 + sumUpTo(2-1)`, calling the function again with `1` as the number argument.

Our equation is now `3 + 2 + sumUp(1)`

When the function runs again with `1` as the number, the if statement code block will run, because 1 is less than or equal to 1. This code block simply returns the number, and the function can stop calling itself. This is important, as calling a function recursively without a condition that stops the function will make the recursion endless, and will cause a program to crash. `sumUpTo(1)` returns `1`, and stops the function.

Our equation is now `3 + 2 + 1`, which equals `6`.

Sample code solution:

``````function sumUpTo(number) {
if (number <= 1) {
return number;
} else {
return number + sumUpTo(number - 1);
}
}

console.log(sumUpTo(4));
``````

Javascript Concepts: Recursion, Functions, Control Flow, If…Else, `console.log()`

9 Likes

I really enjoy this application, please create more task to practice more of the old things we learned and new subjects to learn more advanced fundamentals of JS.

2 Likes

Sorry, I don’t understand the logic…
I understand the question but in the programming section you said, return number + sumUpto(number - 1)
So, according to the logic, if the number is 5, then why not return 5 + (5 - 1) = 9 instead of keep looping and adding 5 with + 4 + 3 + 2 + 1, where is the loop coming from??? By the way there is no for loop option in this function that checks again and again like if 5 + (5 - 1) =5 + 4 now the number is 4 then 4 + ( 4 - 1) = 4 + 3 continues …
Plz can you give the details explanation…

I am a learner too, so I may be wrong. It looks like sumUpTo itself is a function created somewhere with a hidden code, so when you type in sumUpTo(5-1), you are not merely asking for 5-1 but you are calling the function sumUpTo.

It might have been created with a for…loop like (i=1, i<=1, i++) return total =+1, or simply like sumUpTo (n) = (n(n+1))/2. This backend code runs when you call sumUpTo

Example=

sumUpTo(5-1) —>
5-1 = 4
(4(4+1))/2 = (4(5))/2 = 20/2 = 10
It’s the same as 4+3+2+1 = 10