Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialFrank Kirok
Full Stack JavaScript Techdegree Student 391 PointsHow would you generate a random two-digit number from 01 to 99?
I would like to create number generator where has to be two digits, numbers has to be integels and has to any number from 00 to 99
1 Answer
Steven Parker
231,072 PointsThe issue here is that "00" isn't actually a number, since the value of zero doesn't need 2 digits!
But for display purposes, you can convert a number into a string and provide a leading 0 if needed:
let rand = Math.floor(Math.random() * 100); // standard 0-99 random formula
let dualDigits = (rand > 9) ? rand.toString() : "0" + rand; // make a string of 2 digits