
- 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 least common of a range JavaScript
We are required to write a function that takes in an array of two numbers a and b (a >= b) and returns the least common multiple of all the numbers between [a, b].
Approach
We will first write a basic function that calculates the least common multiple of two numbers, once we have that we will recursively call it over the numbers that fall between [a, b] and finally return the result.
Example
const lcm = (a, b) => { let min = Math.min(a, b); while(min >= 2){ if(a % min === 0 && b % min === 0){ return (a*b)/min; }; min--; }; return (a*b); }; const leastCommonMultipleInRange = (arr, len = arr[0], res = 1) => { if(len <= arr[1]){ return leastCommonMultipleInRange(arr, len+1, lcm(res, len)); }; return res; }; console.log(leastCommonMultipleInRange([6, 8])); console.log(leastCommonMultipleInRange([6, 18])); console.log(leastCommonMultipleInRange([1, 8])); console.log(leastCommonMultipleInRange([10, 25]));
Output
The output in the console will be −
168 12252240 840 26771144400
- Related Articles
- Finding the least common multiple of a range of numbers in JavaScript?
- Common element with least index sum in JavaScript
- Write a program to calculate the least common multiple of two numbers JavaScript
- Function to calculate the least common multiple of two numbers in JavaScript
- Calculating h index of a citation in JavaScript
- Calculating the weight of a string in JavaScript
- Calculating average of a sliding window in JavaScript
- Calculating resistance of n devices - JavaScript
- Calculating median of an array JavaScript
- Checking if decimals share at least two common 1 bits in JavaScript
- Calculating excluded average - JavaScript
- Calculating average of an array in JavaScript
- Calculating median of an array in JavaScript
- Calculating the sum of digits of factorial JavaScript
- Calculating a number from its factorial in JavaScript

Advertisements