Closed haishangfeng closed 3 years ago
Do you convert v_code to Number type (for instance with parseInt
)?
Maybe codes with leading 0
is converted to 3-digits number?
const id = `0123`
parseInt(id) //=> 123
no, I quote directly。
const random4 = Math.floor(Math.random()*(8999))+1000; So how to use customAlphabet to generate four fixed random numbers? such as above code
So how to use customAlphabet to generate four fixed random numbers?
let first = customAlphabet ('123456789', 1)
let tail = customAlphabet ('1234567890', 3)
function getCode4 () {
return first() + tail()
}
Good job,tks very much.
nodejs
I used the above code to generate a random number with a length of 4 digits, But sometimes the random number generated is only 3 digits, and the probability of this happening is very high.