Tuday i learn about random numbers genrator
Using math.random keyword
To using this type of functions are mostly in otp generation using proposed .
To generate a numeric One-Time Password (OTP) in JavaScript, you can use the Math.random() and Math.floor() functions.
Math.random(): This function returns any random number between 0 to 1.
Math.floor(): It returns the floor of any floating number to an integer value.
Function to generate OTP
function generateOTP(l=4) {
let digits = '0123456789';
let OTP = '';
let len = digits.length
for (let i = 0; i < l; i++) {
OTP += digits[Math.floor(Math.random() * len)];
}
return OTP;
}
console.log("OTP of 4 digits: ")
console.log(generateOTP(6));
console.log(generateOTP(4));`
Top comments (0)