- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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 smallest multiple in JavaScript
We are required to write a JavaScript function that takes in a number as the only input. The function should find the smallest such number which is exactly divisible by all the first n natural numbers.
For example −
For n = 4, the output should be 12,
because 12 is the smallest number which is divisible by 1 and 2 and 3 and 4.
Example
The code for this will be −
const smallestMultiple = num => { let res = 0; let i = 1; let found = false; while (found === false) { res += num; while (res % i === 0 && i <= num) { if (i === num) { found = true; }; i++; }; i = 1; }; return res; }; console.log(smallestMultiple(2)); console.log(smallestMultiple(4)); console.log(smallestMultiple(12)); console.log(smallestMultiple(15));
Output
And the output in the console will be −
2 12 27720 360360
- Related Articles
- Finding the smallest good base in JavaScript
- Finding the smallest fitting number in JavaScript
- JavaScript Recursion finding the smallest number?
- Finding smallest number using recursion in JavaScript
- Finding the smallest value in a JSON object in JavaScript
- Finding second smallest word in a string - JavaScript
- Finding smallest sum after making transformations in JavaScript
- Finding smallest number that satisfies some conditions in JavaScript
- Finding intersection of multiple arrays - JavaScript
- Finding the smallest positive integer not present in an array in JavaScript
- Finding difference of greatest and the smallest digit in a number - JavaScript
- Finding the largest and smallest number in an unsorted array of integers in JavaScript
- Finding smallest element in a sorted array which is rotated in JavaScript
- Smallest Common Multiple of an array of numbers in JavaScript
- Finding the least common multiple of a range of numbers in JavaScript?

Advertisements