JS Coding Questions Logo
JS Coding Questions
#151💼 Interview💻 Code

Can you write a random integers function to print integers within a range

Advertisement

728x90

Yes, you can create a proper random function to return a random number between min and max (both included)

javascript
1function randomInteger(min, max) {
2  return Math.floor(Math.random() * (max - min + 1)) + min;
3  }
4
5randomInteger(1, 100); // returns a random integer from 1 to 100
6
7randomInteger(1, 1000); // returns a random integer from 1 to 1000

Advertisement

Responsive Ad
🎯 Practice NowRelated Challenge

JavaScript Coding Exercise 66

Test your knowledge with this interactive coding challenge.

Start Coding

Advertisement

728x90
151of476
Can you write a random integers function to print integers within a range | JSCodingQuestions.com