Practice
JavaScriptData StructuresReactConcepts
Sign in
← Back to problems

Sum to N

A Closer Look at Functionseasy

Course · Section 10: A Closer Look at Functions · Lecture 137: First-Class and Higher-Order Functions

Implement `sumTo(n)` returning `1 + 2 + ... + n` using recursion, with `sumTo(0) = 0`.

Sample tests

Input: sumTo(5)
Output: 15
Input: sumTo(1)
Output: 1

+ 1 hidden test run on Submit.

Hints

Common pitfalls
  • Missing the base case causes infinite recursion.

Learning resources

  • MDN: Recursion
Approach & explanation (try first)

Each call adds n to the sum of everything below it, bottoming out at 0.

Loading...
⌘/Ctrl + Enter

Run your code to see results.