Missing base case
Standard · Find the bug · about 6 min
sumTo(n) should return 0 + 1 + … + n for any n ≥ 0. It works for sumTo(3) but crashes with a stack overflow for one input. Click the line with the bug, then fix it.
Example 1
Input:
[3]
Output:
6
Example 2
Input:
[0]
Output:
0
The buggy code never stops here.
Constraints
- 0 ≤ n ≤ 500
Learn the idea first: Recursion: base case, recursive case, the call stack, a visual lesson.
More Recursion: base case, recursive case, the call stack problems
- Factorial trace Warm-up
- Sum of digits Warm-up
- Countdown Standard
- Follow the frames of fact(4) Standard
- Print 1..n without loops Challenge