
- 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
Calculating the LCM of multiple numbers in JavaScript
We are required to write a JavaScript function that takes in an array of numbers of any length and returns their LCM.
We will approach this problem in parts −
Part 1 − We will create a helper function to calculate the Greatest Common Divisor (GCD) of two numbers
Part 2 − Then using Part 1 helper function we will create another helper function to calculate the Least Common Multiple (LCM) of two numbers.
Part 3 − Finally, using Part 2 helper function we will create a function that loops over the array and calculates the array LCM.
Example
The code for this will be −
const calculateLCM = (...arr) => { const gcd2 = (a, b) => { // Greatest common divisor of 2 integers if(!b) return b===0 ? a : NaN; return gcd2(b, a%b); }; const lcm2 = (a, b) => { // Least common multiple of 2 integers return a * b / gcd2(a, b); } // Least common multiple of a list of integers let n = 1; for(let i = 0; i < arr.length; ++i){ n = lcm2(arr[i], n); } return n; }; console.log(calculateLCM(12, 18, 7, 15, 20, 24, 28));
Output
And the output in the console will be −
2520
- Related Articles
- Calculating 1s in binary representation of numbers in JavaScript
- Calculating variance for an array of numbers in JavaScript
- Calculating the weight of a string in JavaScript
- Explain the HCF and LCM of numbers.
- Find LCM of two numbers
- Finding the least common multiple of a range of numbers in JavaScript?
- Calculating the sum of digits of factorial JavaScript
- Smallest Common Multiple of an array of numbers in JavaScript
- Calculating average of an array in JavaScript
- Calculating median of an array in JavaScript
- LCM of an array of numbers in Java
- What is LCM and how do we find the LCM of given numbers?
- Function to calculate the least common multiple of two numbers in JavaScript
- Calculating resistance of n devices - JavaScript
- Calculating median of an array JavaScript

Advertisements