I can't return the sum correctly! Please help

I can't return the sum correctly! Please help
0

#1

Hi guys,
I’m trying to create a function that takes an integer input, adds each digit together, and if there are more than 1 digit, then do it again and again until there is only one.

Example:
Input = 456
~ 4+5+6=15
~ 1+5 = 6
so the function would return 6.

My algorithm seems to work, as tested by alert() messages, but somehow at the final step of returning the value, everything falls apart.
The solution seems so simple that I literately ran out of ideas to even test why it doesn’t work. Please help.
I put the two questions I have as comments in the JSFiddle code, here’s the link:
https://jsfiddle.net/nvhctqa0/2/

Thanks in advance


#2

Recursion is kind of like a chain of events that has to move “down” essentially calling itself again until the base case (n<9) is reached, once the base case is resolved, the chain has to move back up and pass the values back up the chain.

The issue here is when/if the base case is resolved, the value is not being passed back up. You can fix it by putting the inner call to your add function inside a return statement.


#3

Thank you, that solved my problem.