
- 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
Finding smallest number that satisfies some conditions in JavaScript
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,
Output
The output should be −
180
Therefore, let’s write the code for this function −
Example
The code for this will be −
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
The output in the console will be −
180 180
- Related Articles
- Finding smallest number using recursion in JavaScript
- Finding the smallest fitting number in JavaScript
- JavaScript Recursion finding the smallest number?
- Finding a pair that is divisible by some number in JavaScript
- Finding the smallest multiple in JavaScript
- Finding difference of greatest and the smallest digit in a number - JavaScript
- Finding a number of pairs from arrays with smallest sums in JavaScript
- Finding the smallest good base in JavaScript
- Finding the largest and smallest number in an unsorted array of integers in JavaScript
- Finding second smallest word in a string - JavaScript
- Finding smallest sum after making transformations in JavaScript
- Finding number that appears for odd times - JavaScript
- Finding the greatest and smallest number in a space separated string of numbers using JavaScript
- Finding the smallest value in a JSON object in JavaScript
- Smallest number that is divisible by first n numbers in JavaScript

Advertisements