
- 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
Finding sum of multiples in JavaScript
We are required to write a JavaScript function that takes in a number as a limit (the only argument). The function should calculate the sum of all the natural numbers below the limit that are multiples of 3 or 5.
For example −
If the limit is 10
Then the sum should be 3+5+6+9 = 23
Example
Following is the code −
const sumOfMultiple = (limit = 10) => { let i, sum = 0; for (i = 3; i < limit; i += 1) { if (i % 3 === 0 || i % 5 === 0) { sum += i; }; }; return sum; } console.log(sumOfMultiple(1000)); console.log(sumOfMultiple(10)); console.log(sumOfMultiple(100));
Output
Following is the output on console −
233168 23 2318
- Related Articles
- Sum of all multiples in JavaScript
- Finding lunar sum of Numbers - JavaScript
- Finding sum of all unique elements in JavaScript
- Finding sum of a range in an array JavaScript
- Finding sum of every nth element of array in JavaScript
- Finding sum of alternative elements of the array in JavaScript
- Finding Sum of Left Leaves of a BST in JavaScript
- Finding the sum of unique array values - JavaScript
- Finding desired sum of elements in an array in JavaScript
- The sum of three consecutive multiples of 7 is 63. Find multiples.
- Array of multiples - JavaScript
- Finding sum of all numbers within a range in JavaScript
- Finding n subarrays with equal sum in JavaScript
- Finding smallest sum after making transformations in JavaScript
- The sum of three consecutive multiples of 8 is 888. Find the multiples.

Advertisements