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());

效果图:

相关推荐
懂点Tech2 小时前
ctfshow MISC入门图片篇(信息附加):misc41解题思路
开发语言·python
怕浪猫2 小时前
外包和大厂的前端代码,差在凌晨三点
前端·面试·github
m0_敲代码的彭于晏1682 小时前
Vercel 前端部署与全栈开发实战指南
前端·状态模式
徐小夕2 小时前
花了3个月,我们做了一款 AI 版的“飞书文档”
前端·算法·github
贾伟康2 小时前
【化学实验室|17】HarmonyOS ArkTS 亮暗色与视觉令牌实战:集中颜色、间距和交互状态避免页面割裂
前端·移动开发·harmonyos·arkts·arkui
catchadmin3 小时前
PHP 8.7 协程新提案 让跨协程代码共享请求身份
开发语言·php
念何架构之路3 小时前
go-grpc设计思想
开发语言·后端·golang
Csvn3 小时前
SSR/SSG/ISR(上):峡谷与渲染策略的时间预算
前端
Csvn3 小时前
SSR/SSG/ISR(下):可交互性、新鲜度与回源账
前端
酷炫码神4 小时前
Day 10|Pinia 状态管理:把散落在各组件的数据,收进一个全局仓库
android·开发语言·javascript