- 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
JavaScript - Find the smallest n digit number or greater
We are required to write a JavaScript function that takes in a number as the first argument, say n, and an array of numbers as the second argument. The function should return the smallest n digit number which is a multiple of all the elements specified in the array. If there exist no such n digit element then we should return the smallest such element.
For example: If the array is −
const arr = [12, 4, 5, 10, 9]
For both n = 2 and n = 3, the output should be 180
Example
Following is the code −
const arr = [12, 4, 5, 10, 9] const num1 = 2; const num2 = 3; const allDivides = (arr, num) => arr.every(el => num % el === 0); const smallestMultiple = (arr, num) => { let smallestN = Math.pow(10, (num - 1)); while(!allDivides(arr, smallestN)){ smallestN++; }; return smallestN; }; console.log(smallestMultiple(arr, num1)); console.log(smallestMultiple(arr, num2));
Output
Following is the output in the console −
180 180
- Related Articles
- Largest number less than N with digit sum greater than the digit sum of N in C++
- Smallest prime number just greater than the specified number in JavaScript
- Largest even digit number not greater than N in C++
- Find the difference between the largest 3 digit number and the smallest 6 digit number.
- Find the difference between the largest 8-digit number and the smallest 6-digit number.
- Find the sum of the largest 5 -digit number and the smallest 6 -digit number.
- How to get the smallest integer greater than or equal to a number in JavaScript?
- Greater possible digit difference of a number in JavaScript
- Find Smallest Letter Greater Than Target in JavaScript
- Which number is greater, the least $9$ digit number or the number which is $2$ more than the greatest $8$ digit number?
- Checking digit sum of smallest number in the array in JavaScript
- Smallest number after removing n digits in JavaScript
- How to find the smallest number greater than x in Python?
- Finding difference of greatest and the smallest digit in a number - JavaScript
- C++ Program to find the smallest digit in a given number

Advertisements