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

效果图:

相关推荐
朝朝辞暮i8 分钟前
第 01 讲:Python 变量、对象、数据类型与内存引用
开发语言·python
2601_9516152020 分钟前
网站模板 网站网页模板下载
前端·网络·html
外收内放31 分钟前
Python基础语法练习题(69)
开发语言·python
lojox35 分钟前
深入理解 C++ STL:vector 底层实现、迭代器失效与 list 特性
开发语言·c++
iotxiaohu38 分钟前
用 cflow 查看 C 函数调用关系
c语言·开发语言·cflow
zhang-ge38 分钟前
python包和项目管理器uv在ubuntu下的安装与使用
开发语言·python·uv
酷炫码神1 小时前
Day 1|HTML5:一个商城首页的骨架,从 DOCTYPE 到语义化
前端·html·html5
AI源码1 小时前
鲜花商城(HTML 版)
前端·html
hiahiahia1231 小时前
Message 和对话
服务器·前端·数据库
微笑星河q1 小时前
C++类和对象(下)初始化列表、类型转换、static、友元、内部类、匿名对象
开发语言·c++