- Related Questions & Answers
- Finding the least common multiple of a range of numbers in JavaScript?
- Function to calculate the least common multiple of two numbers in JavaScript
- Get the smallest array from an array of arrays in JavaScript
- Equal partition of an array of numbers - JavaScript
- Write a program to calculate the least common multiple of two numbers JavaScript
- Realtime moving average of an array of numbers in JavaScript
- Maximum sum of smallest and second smallest in an array in C++
- Retrieving n smallest numbers from an array in their original order in JavaScript
- Product of all other numbers an array in JavaScript
- Sum of all prime numbers in an array - JavaScript
- Calculating variance for an array of numbers in JavaScript
- Sort an array of objects by multiple properties in JavaScript
- Finding the smallest multiple in JavaScript
- Maximum sum of smallest and second smallest in an array in C++ Program
- Calculating the LCM of multiple numbers in JavaScript

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

Suppose, we have an array of two numbers that specify a range. We are required to write a function that finds the smallest common multiple of the provided parameters that can be evenly divided by both, as well as by all sequential numbers in the range between these parameters.

The range will be an array of two numbers that will not necessarily be in numerical order.

For example, if given [1, 3], then we are required to find the smallest common multiple of both 1 and 3 that is also evenly divisible by all numbers between 1 and 3. The answer here would be 6.

The code for this will be −

const range = [1, 12]; const smallestCommon = (array = []) => { arr = array.slice().sort((a, b) => a − b); let result = []; for(let i = arr[0]; i <= arr[1]; i++){ result.push(i); }; let i = 1; let res; while(result.every(item=>res%item==0)==false){ i++; res = arr[1]*i; } return res; } console.log(smallestCommon(range));

And the output in the console will be −

27720

Advertisements