英文:
javascript generate code of 8 characters errors "undefined"
问题
你的HTML页面有一个用于生成8个字符(大写字母和数字)的随机代码的脚本。它的工作方式是,我通过按F5刷新页面,新代码会出现在页面上。有时,在生成过程中,我会收到一个"undefined"错误而不是单个字符。请帮助修复这个问题。以下是我的脚本。
function generate(len,type)
{
var ints =[0,1,2,3,4,5,6,7,8,9];
var chars=['A','B','C','D','E','F','G','H','J','K','L','M','N','O','P','R','S','T','U','V','W','X','Y','Z'];
var out='';
for(var i=0;i<len;i++){
var ch=Math.random(1,2);
if(ch<0.5){
var ch2=Math.ceil(Math.random(1,ints.length)*10);
out+=ints[ch2];
}else{
var ch2=Math.ceil(Math.random(1,chars.length)*10);
out+=chars[ch2];
}
}
document.getElementById("generatecode").innerHTML = out;
}
generate(9);
我不知道该怎么做,所以我在这里发帖。
英文:
My HTML page has a script to generate a random code of 8 characters (capital letters and numbers). It works like this, I refresh the page via F5 and the new code appears on the page. Sometimes, when generating, I get an "undefined" error instead of a single character. Help fix this. Below is my script.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
function generate(len,type)
{
var ints =[0,1,2,3,4,5,6,7,8,9];
var chars=['A','B','C','D','E','F','G','H','J','K','L','M','N','O','P','R','S','T','U','V','W','X','Y','Z'];
var out='';
for(var i=0;i<len;i++){
var ch=Math.random(1,2);
if(ch<0.5){
var ch2=Math.ceil(Math.random(1,ints.length)*10);
out+=ints[ch2];
}else{
var ch2=Math.ceil(Math.random(1,chars.length)*10);
out+=chars[ch2];
}
}
document.getElementById("generatecode").innerHTML = out;
}
generate(9);
<!-- language: lang-html -->
<span id="generatecode"></span>
<!-- end snippet -->
I don't know what to do, that's why I'm posting here
答案1
得分: 1
Math.random
不接受任何参数。
如果你想要在范围内获取一个随机值,请使用:
function random(min, max) {
return Math.random() * (max - min) + min
}
function generate(len, type) {
var ints = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
var chars = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'];
var out = '';
for (var i = 0; i < len; i++) {
var ch = Math.random();
if (ch < 0.5) {
var ch2 = Math.floor(random(0, ints.length));
out += ints[ch2];
} else {
var ch2 = Math.floor(random(0, chars.length));
out += chars[ch2];
}
}
document.getElementById("generatecode").innerHTML = out;
}
generate(9);
<span id="generatecode"></span>
英文:
Math.random
doesn't take any arguments.
If you want a random value in range use:
function random(min, max) {
return Math.random() * (max - min) + min
}
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
function generate(len, type) {
var ints = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
var chars = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'];
var out = '';
for (var i = 0; i < len; i++) {
var ch = Math.random();
if (ch < 0.5) {
var ch2 = Math.floor(random(0, ints.length));
out += ints[ch2];
} else {
var ch2 = Math.floor(random(0, chars.length));
out += chars[ch2];
}
}
document.getElementById("generatecode").innerHTML = out;
}
generate(9);
function random(min, max) {
return Math.random() * (max - min) + min
}
<!-- language: lang-html -->
<span id="generatecode"></span>
<!-- end snippet -->
答案2
得分: -1
- 你在定义
generate
函数时使用了参数(len, type)
,但在调用时却写成了generate(9);
,为什么? Math.random();
函数不接受参数,它只是返回一个介于 0 到 1 之间的随机值,但永远不会返回 1。你可以使用Math.floor(Math.random() * maxNumber)
,其中maxNumber
是最大值。它会返回从 0 到maxNumber-1
的随机整数(如果需要,你可以使用Math.floor(Math.random() * maxNumber) + 1
来得到 1 到maxNumber
的随机整数)。- 数组是从零开始索引的,也就是说它们从 0 开始。
<array>.length
返回数组中元素的数量,但要获取最后一个元素,你必须使用<array>[<array>.length-1]
。
英文:
Couple of things to note here:
- You're defining
generate
with args(len, type)
, but you call it asgenerate(9);
Why? Math.random();
takes no arguments; it simply returns a value between 0 and 1, but it never returns 1. You can instead useMath.floor(Math.random() * maxNumber)
wheremaxNumber
is the maximum number. It'll return 0 to maxNumber-1. (if you want, make itMath.floor(Math.random() * maxNumber) + 1
to make it 1 to maxNumber).- Arrays are zero indexed; meaning they start at 0.
<array>.length
returns the amount of elements are there in the array, but to get the last element you must use<array>[<array>.length-1]
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论