gpt4 book ai didi

javascript - 生成给定长度的随机字符串

转载 作者:行者123 更新时间:2023-11-30 21:14:41 25 4
gpt4 key购买 nike

我这里有一些问题。

a)我想设置3-8个字符,但它只显示3个字符。

b)我想要三个不同显示时间的输出,我只需要创建不同的 Id 吗?

有人可以帮忙吗?这段代码有什么问题?谢谢。

Javascript

        function randomString(Length)
{
if(Length < 3) Length = 3;
if(Length > 8) Length = 8;
var text = "";
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
for( var i=0; i < Length; i++ )
text += possible.charAt(Math.floor(Math.random() * possible.length));

return text;
}

function ChangingRandomString(Length)
{
setInterval(function(){document.getElementById("random").style.fontSize = Math.floor((Math.random() * 20) + 10)+"px",
document.getElementById("random").innerHTML = randomString(Length);
},2000);
}

最佳答案

function randomString(length) {
if (length < 3) length = 3;
if (length > 8) length = 8;
var text = '';
var possible = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
for (var i = 0; i < length; i++) {
text += possible[Math.floor(Math.random() * possible.length)];
}
return text;
}

function ChangingRandomString(length) {
setInterval(() => {
let el = document.getElementById('random');
el.style.fontSize = Math.floor(Math.random() * 20 + 10) + 'px';
el.innerHTML = randomString(length);
}, 2000);
}

ChangingRandomString(length);
<div id="random"></div>

关于javascript - 生成给定长度的随机字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45807334/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com