Finding sum of multiples in JavaScript

In the given problem statement we have to find the sum of the multiples of the given input number within the given range with the help of Javascript functionalities. So we will use a loop to iterate through the numbers from the starting range to the ending range.

Understanding the Problem

The problem at hand is to find the sum of multiples of the given number in the given range. So in this problem we have to calculate the sum of all the numbers that are divisible by the given number.

For example: suppose we have a number 2, and starting range is 1 and ending range is 8 so within this range the multiples of 2 are 2, 4, 6 and 8. So the sum of these multiples is 2 + 4 + 6 + 8 = 20. So the required result will be 20. So basically we have to implement the code to produce the result like this.

Logic for the given Problem

For solving the given problem in Javascript we'll write a function which will iterate through the given range. And we will check every number for divisibility by the given number. After getting the multiples of the number we will add them together to get the sum. With the usage of a loop and conditional check we can effectively get the multiples and compute the sum.


Step 1: As we have to calculate the sum of all the multiples of the given number within the given range. So we need to create a function to do this task. Adn inside the function we will take three parameters as input. These parameters are number, startingRange and endingRange.

Step 2: The second thing is to define a variable to store the resultant sum of the multiples of the given number. So define a sum variable and initialize it with zero.

Step 3: As we have to find the multiples of the given number so we will iterate the numbers from startingRange to the endingRange.

Step 4: In the loop we will check the modulus of the number for each value. And calculate the sum in the running sum.

Step 5: And at the end we will have the sum of all the required numbers or multiples. So show as the output to the console.


//Function to get the sum of multiples of the given number
function sumOfMultiples(number, startingRange, endingRange) {
   let sum = 0;

   for (let i = startingRange; i <= endingRange; i++) {
      if (i % number === 0) {
         sum += i;

   return sum;
const number = 2;
const startingRange = 1;
const endingRange = 20;

const result = sumOfMultiples(number, startingRange, endingRange);




The time complexity of the code we have created for finding the sum of multiples of the given number is O(n), in which n is the size of the given range of numbers. Because the code traverses through every number in the given range once to check for divisibility and get the required sum.


The code we have produced effectively solved the given problem for calculating the sum. It is a straightforward and efficient method to calculate the sum with the help of a loop and a condition to check whether the number is a multiple or not.

Updated on: 14-Aug-2023

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started