function getCode() {
let str = 'qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM0123456789'
let code = ''
// while 条件满足时执行,不满足条件则跳出不执行
while (code.length < 4) {
// Math.round四舍五入方法
// Math.random 随机获取0-1之间的数
// *61 因为验证码从a-z,A-Z,1-9有62位,str字符串从0开始计算,所以需要*61
let random = Math.round( Math.random() * 61)
if(code.indexOf(str[random]) === -1) {
code += str[random]
}
}
return code
}
const res = getCode()
console.log('res', res);
06-21
11-30
1125
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)