← Back to challenges

Recursion: Sum

JavaScriptHardrecursionmathnumberslanguage_fundamentals

Instructions

Write a function that finds the sum of the first n natural numbers. Make your function recursive.

Examples

sum(5) ➞ 15
// 1 + 2 + 3 + 4 + 5 = 15

sum(1) ➞ 1

sum(12) ➞ 78

Notes

  • Assume the input number is always positive.
javascript
Loading editor…
to run
Walks through the solution with reasoning and edge cases.
Next: RegEx: Boundary Assertions II