Practice: Sum of Natural Numbers

We'll now practice the techniques learned in this section using the "Sum of Natural Numbers" problem. Try to solve the problem recursively on your own first. If it helps to start by writing an iterative solution, feel free to do so. Remember to write a complete 'Recursive Definition' before attempting a solution.

Problem Description

// Create a function that calculates the sum of the first `n`
// natural numbers. A natural number is a positive integer
// starting from 1. Therefore, the sum of the first `n` natural
// numbers is the sum of all integers from 1 to `n`.

// For example, if `n` is 5, the sum would be 1 + 2 + 3 + 4 + 5 == 15.

console.log(sumOfNaturalNumbers(1) === 1);
console.log(sumOfNaturalNumbers(5) === 15);
console.log(sumOfNaturalNumbers(10) === 55);
console.log(sumOfNaturalNumbers(20) === 210);
console.log(sumOfNaturalNumbers(0) === 0);

// All test cases should log true.

Exercises

To ask LSBot exercise-specific questions, click the "Work in Code Editor" button next to each exercise solution.
Exercise 1

Write the recursive definition for the "Sum of Natural Numbers" problem. Make sure to include both a base case and a recursive definition.

View Our Solution Work in Code Editor

In recursive problem-solving, we typically define two key components: a recursive definition and a base case. The recursive definition breaks down the problem into smaller instances of the same problem, while the base case provides a simple, non-recursive termination condition.

The Base Case

The base case in this scenario occurs when the number n is less than or equal to 1. In such cases, the sum of all natural numbers up to n is n itself. Although 0 is not traditionally a natural number, we include it here to handle edge cases effectively. This consideration is reflected in the last of our example test cases, where 0 is provided as input.

The Recursive Definition

In the case of summing the first n natural numbers, our recursive definition is as follows: The sum of the first n natural numbers can be expressed as n plus the sum of the first n-1 natural numbers.

The solution becomes more attainable with a well-defined base case and a recursive definition.

Exercise 2

Write the solution code following your recursive definition from above.

View Our Solution Work in Code Editor

function sumOfNaturalNumbers(n) {
  if (n <= 1) {
    return n;
  }
  return n + sumOfNaturalNumbers(n - 1);
}

Hi! I'm LSBot. I'm here to help you understand this chapter content with fast , focused answers. Any code from the editor will be automatically included with your question.

Ask me about concepts, examples, or anything you'd like clarified from this chapter. I can explain complex topics, provide examples, or help connect ideas.

Switch to Deep Dive mode if you want comprehensive answers across the entire curriculum. Responses may take longer, but I'll draw on the entire Launch School curriculum to give you a fuller picture.

Want to know more? Refer to the LSBot User Guide .

GitHub Repository Submission

When using GitHub mode, paste your repository URL below and click Save URL to store it. The saved URL will be automatically included with every message you send until you choose to clear it. Learn more

Your GitHub repository URL is saved. LSBot will automatically fetch your latest code from this repository for each message. To change the URL, clear it first and save a new one.
Output
No output yet. Click "Run Code" to execute your code.