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

效果图:

相关推荐
我是唐青枫15 小时前
C#.NET Span 深入解析:零拷贝内存切片与高性能实战
开发语言·c#·.net
lxh011315 小时前
数据流的中位数
开发语言·前端·javascript
神仙别闹15 小时前
基于NodeJS+Vue+MySQL实现一个在线编程笔试平台
前端·vue.js·mysql
盒马盒马15 小时前
Rust:迭代器
开发语言·后端·rust
Full Stack Developme16 小时前
Java 常用通信协议及对应的框架
java·开发语言
zadyd16 小时前
Workflow or ReAct ?
前端·react.js·前端框架
飞Link17 小时前
告别盲目找Bug:深度解析 TSTD 异常检测中的预测模型(Python 实战版)
开发语言·python·算法·bug
1.14(java)17 小时前
Spring-boot快速上手
java·开发语言·javaee
北寻北爱18 小时前
vue2和vue3使用less和scss
前端·less·scss
记忆多18 小时前
c++名字空间 函数模版 左右值
开发语言·c++·算法