JavaScript生成的8个字符的代码错误是”undefined”。

huangapple go评论91阅读模式
英文:

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=[&#39;A&#39;,&#39;B&#39;,&#39;C&#39;,&#39;D&#39;,&#39;E&#39;,&#39;F&#39;,&#39;G&#39;,&#39;H&#39;,&#39;J&#39;,&#39;K&#39;,&#39;L&#39;,&#39;M&#39;,&#39;N&#39;,&#39;O&#39;,&#39;P&#39;,&#39;R&#39;,&#39;S&#39;,&#39;T&#39;,&#39;U&#39;,&#39;V&#39;,&#39;W&#39;,&#39;X&#39;,&#39;Y&#39;,&#39;Z&#39;];
    var out=&#39;&#39;;
    for(var i=0;i&lt;len;i++){
        var ch=Math.random(1,2);
        if(ch&lt;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(&quot;generatecode&quot;).innerHTML = out;
}
generate(9);

<!-- language: lang-html -->

&lt;span id=&quot;generatecode&quot;&gt;&lt;/span&gt;

<!-- 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 = [&#39;A&#39;, &#39;B&#39;, &#39;C&#39;, &#39;D&#39;, &#39;E&#39;, &#39;F&#39;, &#39;G&#39;, &#39;H&#39;, &#39;J&#39;, &#39;K&#39;, &#39;L&#39;, &#39;M&#39;, &#39;N&#39;, &#39;O&#39;, &#39;P&#39;, &#39;R&#39;, &#39;S&#39;, &#39;T&#39;, &#39;U&#39;, &#39;V&#39;, &#39;W&#39;, &#39;X&#39;, &#39;Y&#39;, &#39;Z&#39;];
  var out = &#39;&#39;;
  for (var i = 0; i &lt; len; i++) {
    var ch = Math.random();
    if (ch &lt; 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(&quot;generatecode&quot;).innerHTML = out;
}
generate(9);

function random(min, max) {
  return Math.random() * (max - min) + min
}

<!-- language: lang-html -->

&lt;span id=&quot;generatecode&quot;&gt;&lt;/span&gt;

<!-- 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 as generate(9); Why?
  • Math.random(); takes no arguments; it simply returns a value between 0 and 1, but it never returns 1. You can instead use Math.floor(Math.random() * maxNumber) where maxNumber is the maximum number. It'll return 0 to maxNumber-1. (if you want, make it Math.floor(Math.random() * maxNumber) + 1 to make it 1 to maxNumber).
  • Arrays are zero indexed; meaning they start at 0. &lt;array&gt;.length returns the amount of elements are there in the array, but to get the last element you must use &lt;array&gt;[&lt;array&gt;.length-1].

huangapple
  • 本文由 发表于 2023年6月26日 18:05:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76555641.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定