随机数字、颜色和验证码
<!DOCTYPE html>
<html lang="UTF-8">
<head>
<meta charset="UTF-8">
<title>随机</title>
</head>
<body>
<script type="text/javascript">
function randInt(min,max) {
let intNum = Math.floor(Math.random() * (max - min) + min);
return intNum;
}
function randRGB() {
let r = randInt(0,256),g = randInt(0,256),b = randInt(0,256);
let result = 'rgb('+r+','+g+','+b+')';
return result;
}
function randCode() {
let code = '';
let codeLength = 4;
const random = [0,1,2,3,4,5,6,7,8,9,'A','B','C','D','E','F','G','H','I','J','K',
'L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'];
for (let i = 0; i < codeLength; i++){
let index = randInt(0,36);
code += random[index];
}
return code;
}
</script>
<script type="text/javascript">
console.log(randInt(1,100));
rgb = randRGB();
console.log(rgb);
document.body.style.backgroundColor = rgb;
code = randCode();
console.log(code);
document.write('<h1>${code}</h1>');
</script>
</body>
</html>