

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Finding the least common multiple of a range of numbers in JavaScript?
We are required to write a JavaScript function that takes in an array of exactly two numbers specifying a range.
The function should then calculate the least common multiple of all the numbers within that range and return the final result.
Example
The code for this will be −
const range = [8, 3]; const gcd = (a, b) => { return !b ? a : gcd(b, a % b); } const lcm = (a, b) => { return a * (b / gcd(a,b)); }; const rangeLCM = (arr = []) => { if(arr[0] > arr[1]) (arr = [arr[1], arr[0]]); for(let x = result = arr[0]; x <= arr[1]; x++) { result = lcm(x, result); } return result; } console.log(rangeLCM(range));
Output
And the output in the console will be −
840
- Related Questions & Answers
- Calculating least common of a range JavaScript
- Function to calculate the least common multiple of two numbers in JavaScript
- Write a program to calculate the least common multiple of two numbers JavaScript
- Finding sum of all numbers within a range in JavaScript
- Smallest Common Multiple of an array of numbers in JavaScript
- Finding the count of total upside down numbers in a range using JavaScript
- Finding the count of numbers divisible by a number within a range using JavaScript
- Finding Armstrong numbers in a given range in JavaScript
- Finding sequential digit numbers within a range in JavaScript
- Count of common multiples of two numbers in a range in C++
- Finding maximum length of common subarray in JavaScript
- Finding sum of a range in an array JavaScript
- Finding intersection of multiple arrays - JavaScript
- Finding the k-prime numbers with a specific distance in a range in JavaScript
- Finding lunar sum of Numbers - JavaScript
Advertisements