Exercise: Sum Of Numbers


Use a for loop to compute the sum of all numbers from 14 to 73.

Once computed, print this sum to the console.

Solution

Introduction

In this lesson, we will learn how to use a for loop to compute the sum of a range of numbers. This is a fundamental concept in programming that helps in understanding loops and iteration. Summing numbers is a common task in various scenarios such as calculating totals, averages, and other aggregate values.

Understanding the Basics

A for loop is a control flow statement that allows code to be executed repeatedly based on a condition. It consists of three parts: initialization, condition, and increment/decrement. Understanding how to use a for loop is crucial before moving on to more complex programming tasks.

Example of a basic for loop:

for (let i = 0; i < 5; i++) {
  console.log(i);
}

This loop will print numbers from 0 to 4.

Main Concepts

To solve the problem of summing numbers from 14 to 73, we need to initialize a sum variable to 0 and use a for loop to iterate through each number in the range, adding each number to the sum variable.

Here is the step-by-step approach:

  1. Initialize a variable sum to 0.
  2. Use a for loop to iterate from 14 to 73.
  3. In each iteration, add the current number to the sum variable.
  4. After the loop ends, print the sum to the console.

Examples and Use Cases

Let's look at the code implementation:

// Initialize sum variable
let sum = 0;

// Use a for loop to iterate from 14 to 73
for (let i = 14; i <= 73; i++) {
  // Add the current number to the sum
  sum += i;
}

// Print the sum to the console
console.log(sum);

This code will compute the sum of all numbers from 14 to 73 and print the result.

Common Pitfalls and Best Practices

Common mistakes include off-by-one errors where the loop might start or end at the wrong number. Always double-check the loop boundaries. Best practices include using meaningful variable names and adding comments to explain the code.

Advanced Techniques

For more advanced scenarios, you might need to sum numbers based on a condition (e.g., only even numbers). This can be achieved by adding an if statement inside the loop.

let sum = 0;
for (let i = 14; i <= 73; i++) {
  if (i % 2 === 0) { // Check if the number is even
    sum += i;
  }
}
console.log(sum);

Debugging and Testing

To debug, use console.log statements to print intermediate values. For testing, you can write functions and use testing frameworks like Jest to automate the testing process.

function sumRange(start, end) {
  let sum = 0;
  for (let i = start; i <= end; i++) {
    sum += i;
  }
  return sum;
}

// Test the function
console.log(sumRange(14, 73)); // Expected output: 2412

Thinking and Problem-Solving Tips

Break down the problem into smaller parts. First, understand how to use a for loop, then apply it to the specific range. Practice with different ranges and conditions to strengthen your understanding.

Conclusion

Summing a range of numbers using a for loop is a fundamental skill in programming. Mastering this concept will help you in various programming tasks. Keep practicing and exploring more complex scenarios to improve your skills.

Additional Resources