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

效果图:

相关推荐
摇滚侠11 小时前
尚硅谷 Java 零基础全套视频教程,System、Runtime、BigDecimal、BigInteger、Random,笔记 151
java·开发语言·笔记
鲨莎分不晴11 小时前
【实战】老项目焕发新生:从 Webpack 平滑迁移到 Vite 避坑全记录
前端·webpack·node.js
独行soc11 小时前
2026年渗透测试面试题总结-1(题目+回答)
android·开发语言·网络·安全·web安全·渗透测试·php
IT瑞先生11 小时前
php unicode与中文互转
android·开发语言·php
小镇学者11 小时前
【python】python有必要像go或者nodejs那样做多版本切换吗?
开发语言·python·golang
星火开发设计11 小时前
变量与常量:C++ 中 const 关键字的正确使用姿势
开发语言·c++·学习·const·知识
hoiii18711 小时前
C# 俄罗斯方块游戏
开发语言·游戏·c#
huaqianzkh11 小时前
WinForm + DevExpress 控件的「完整继承关系」
开发语言
web小白成长日记11 小时前
从零起步,用TypeScript写一个Todo App:踩坑与收获分享
前端·javascript·typescript
a***592612 小时前
C++跨平台开发:挑战与解决方案
开发语言·c++