Saturday, January 27, 2024

how to generate otp in javascript

 // Function to generate OTP

function generateOTP(length) {

    // Digits to be used for OTP

    const digits = "0123456789";


    // Initialize an empty string to store the OTP

    let OTP = "";


    // Loop to generate each digit of the OTP

    for (let i = 0; i < length; i++) {

        // Append a randomly selected digit to the OTP string

        OTP += digits[Math.floor(Math.random() * 10)];

    }


    // Return the generated OTP

    return OTP;

}


// Generate a 6-digit OTP

const otp = generateOTP(6);


// Print the generated OTP to the console

console.log(otp);


No comments:

Post a Comment