
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Sum of all multiples in JavaScript
We are required to write a JavaScript function that takes in a number, say n, as the first argument, and then any number of arguments following that.
The idea is to sum all numbers upto n which are divided by any of the numbers specified by second argument and after.
For example −
If the function is called like this −
sumMultiples(15, 2, 3);
Then the output should be −
const output = 83;
Because the numbers are −
2, 3, 4, 6, 8, 9, 10, 12, 14, 15
Example
The code for this will be −
const num = 15; const sumMultiple = (num, ...arr) => { const dividesAny = num => arr.some(el => num % el === 0); let sum = 0; while (num) { if (dividesAny(num)) { sum += num; }; num−−; }; return sum; }; console.log(sumMultiple(num, 2, 3));
Output
And the output in the console will be −
83
- Related Articles
- Finding sum of multiples in JavaScript
- Find the sum of all 3-digit natural numbers, which are multiples of 11.
- Array of multiples - JavaScript
- The sum of three consecutive multiples of 7 is 63. Find multiples.
- Sum of all prime numbers in JavaScript
- Find the sum of all integers between 84 and 719, which are multiples of 5.
- The sum of three consecutive multiples of 8 is 888. Find the multiples.
- Finding sum of all unique elements in JavaScript
- The sum of the squares of two consecutive multiples of 7 is 637. Find the multiples.
- Sum of all prime numbers in an array - JavaScript
- Sum of All Possible Odd Length Subarrays in JavaScript
- Find the sum of first 8 multiples of 3.
- Find the sum of first five multiples of $3$.
- Sum of the multiples of two numbers below N in C++
- Constructing multiples array - JavaScript

Advertisements