js中随机生成4位数的验证码,要有字母和数字

代码:

javascript 复制代码
function code(){
            let num1 = Math.floor(Math.random()*10)
            let num2 = Math.floor(Math.random()*10)
            let letter1 = String.fromCharCode(Math.floor(Math.random() * 26) + 97);
            let letter2 = String.fromCharCode(Math.floor(Math.random() * 26) + 65);
            var arr = [num1,num2,letter1,letter2]
            var str = "";
            for(var i=0;i<4;i++){
                str += arr[Math.floor(Math.random()*(arr.length))]
            }
            return str
        }
        document.write(code());

效果图:

相关推荐
Code Warrior3 分钟前
【C++】智能指针的使用及其原理
开发语言·c++
05大叔3 分钟前
多线程的学习
java·开发语言·学习
lly20240611 分钟前
C 位域:深度解析其概念、应用与未来趋势
开发语言
刺客xs13 分钟前
多路IO复用
开发语言
用户289079421627116 分钟前
Spec-Kit应用指南
前端
酸菜土狗24 分钟前
🔥 手写 Vue 自定义指令:实现内容区拖拽调整大小(超实用)
前端
ohyeah27 分钟前
深入理解 React Hooks:useState 与 useEffect 的核心原理与最佳实践
前端·react.js
Cache技术分享29 分钟前
275. Java Stream API - flatMap 操作:展开一对多的关系,拉平你的流!
前端·后端
apollo_qwe1 小时前
前端缓存深度解析:从基础到进阶的实现方式与实践指南
前端
周星星日记1 小时前
vue中hash模式和history模式的区别
前端·面试