- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

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

We are required to write a JavaScript function that takes in a number as the only argument. The function should then return a random generated number which is always divisible by the number provided by the argument.

The code for this will be −

const num = 21; // function that generates random numbers divisible by n with a default upper limit of 1000000 const specialRandom = (num = 1, limit = 1000000) => { // getting a random number const random = Math.random() * limit; // rounding it off to be divisible by num const res = Math.round( random / num ) * num; return res; }; console.log(specialRandom(num));

And the output in the console will be −

6006

This output is likely to differ on each run.

- Related Questions & Answers
- Smallest number that is divisible by first n numbers in JavaScript
- Generating Random Prime Number in JavaScript
- Generating n random numbers between a range - JavaScript
- Finding a pair that is divisible by some number in JavaScript
- Greatest number divisible by n within a bound in JavaScript
- Sum which is divisible by n in JavaScript
- Generating random number list in Python
- Generating random number in a range in C
- Generating random hex color in JavaScript
- Find N digits number which is divisible by D in C++
- Generating random string with a specific length in JavaScript
- Smallest possible number divisible by all numbers from 1 to n in JavaScript
- Add N digits to A such that it is divisible by B after each addition?
- Generating random string of specified length in JavaScript
- Count n digit numbers divisible by given number in C++

Advertisements