简体   繁体   English

创建一个 Javascript 函数,它生成一个随机字符 id,其中包含给定输入长度的字母和数字

[英]Create a Javascript function, which generates a random character id comprising of alphabets and numbers for given input length

I have completed a JavaScript course and now I am stuck with first Hands-on problem.我已经完成了 JavaScript 课程,现在我遇到了第一个动手问题。 The question is "How to generate Random Character id"问题是“如何生成随机字符ID”

Review below screen:查看以下屏幕:

HTML code HTML代码

在此处输入图片说明

JavaScript Code. JavaScript 代码。

在此处输入图片说明

Error message错误信息

在此处输入图片说明

Please let me know your suggestions请让我知道您的建议

Thanks谢谢

The code I used after few suggestions is as below:我在一些建议后使用的代码如下:

function stringGen(yourNumber){
  var text = "";
  var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";

  for (var i = 0; i < yourNumber; i++)
    text += possible.charAt(Math.floor(Math.random() * possible.length));

 return text;
}

stringGen(10);

I get the below error when I test the code in Node.js in Hackerrank在 Hackerrank 中测试 Node.js 中的代码时出现以下错误

09 10 2018 06:15:13.648:INFO [karma]: Karma v1.7.1 server started at http://0.0.0.0:9876/                                                      
09 10 2018 06:15:13.651:INFO [launcher]: Launching browser jsdom with unlimited concurrency                                                    
09 10 2018 06:15:13.656:INFO [launcher]: Starting browser jsdom                                                                                
09 10 2018 06:15:13.828:INFO [Node.js (linux; U; rv:v8.9.4)]: Connected on socket 1L0-mKrQnWEv092UAAAA with id 34735969                        
Node.js (linux; U; rv:v8.9.4) Random string checking random number FAILED                                                                      
        Expected 0 to be 4.                                                                                                                    
            at UserContext.<anonymous> (test/index_test.js:17:33)                                                                              
Node.js (linux; U; rv:v8.9.4) Random string comparing random numbers FAILED                                                                    
        Expected true to be false.                                                                                                             
            at UserContext.<anonymous> (test/index_test.js:26:16)                                                                              
Node.js (linux; U; rv:v8.9.4): Executed 2 of 2 (2 FAILED) ERROR (0.031 secs / 0.016 secs)                                                      
npm ERR! Test failed.  See above for more details.  
function stringGen(){
  var text = "";
  var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
  var x = document.getElementById("num").value;
  for (var i = 0; i < x; i++)
    text += possible.charAt(Math.floor(Math.random() * possible.length));

  return text;
}
function stringGen(yourNumber){
  var text = "";
  var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";

  for (var i = 0; i < yourNumber; i++)
    text += possible.charAt(Math.floor(Math.random() * possible.length));

  return text;
}

call Function stringGen(8)调用函数 stringGen(8)

it will generate random string它会生成随机字符串

This worked for me.这对我有用。

function stringGen()
{
   //Type your code here.
   var length = document.getElementById('num').value;
   var result           = '';
   var characters       = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
   var charactersLength = characters.length;
   for ( var i = 0; i < length; i++ ) {
      result += characters.charAt(Math.floor(Math.random() * charactersLength));
   }
   document.getElementById("result").innerHTML= result;
   return result;
}

Alright, so I updated my answer.好的,所以我更新了我的答案。 I hope this works for you.我希望这对你有用。 Happy coding and welcome to SO :)快乐编码,欢迎来到 SO :)

 const makeid = () => { let chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; let id = ""; let num = document.getElementById('input_id').value; for (let i = 0; i < num; i++) { id += chars.charAt(Math.floor(Math.random() * chars.length)); document.getElementById('info1').innerHTML = id; document.getElementById('info2').innerHTML = id.length; } }
 #info1 { color: green; font-weight: bold; letter-spacing: 1px; } #info2 { color: red; font-weight: bold; }
 <input type="number" id="input_id" value=""> <input type="button" value="Submit" onclick="makeid()" /> <div>Your unique id is: <span id="info1"></span> </div> <div>The length of your id is: <span id="info2"></span> </div>

function stringGen(yourNumber){
  var text = "";
  var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";

  for (var i = 0; i < yourNumber; i++)
    text += possible.charAt(Math.floor(Math.random() * possible.length));

 document.getElementById("result").innerHTML= text;
}

Answer回答

    function stringGen()
{
    var num = document.getElementById('num').value;
   //  alert(num);
         var text = "";
  var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";

  for (var i = 0; i < num; i++)
    text += possible.charAt(Math.floor(Math.random() * possible.length));

 //alert(text);
 document.getElementById('result').innerHTML = text;
  return text;
}
{   
    var c = document.getElementById("num").value;
    let random_string = '';
    let random_ascii;
    let low = 65;
    let high = 90
    for(let i = 0; i < c; i++) {
        random_ascii = Math.floor((Math.random() * (high - low)) + low);
        random_string += String.fromCharCode(random_ascii)
    }
    return random_string
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 javascript验证以数字,字母和特殊字符按特定顺序输入的表单? - javascript validate form input with numbers, alphabets and a special character in a specific order? 如何使用 JavaScript 中的给定输入生成完全随机数组的算法?(生成随机表情符号、数字、字符、符号等) - how to make an algorithm that generates totally random arrays with a given input in JavaScript?(generate random emoji, number, character, symbol etc.) 有没有办法只接受字母而不是数字? 如果给出数字,它应该显示验证错误,因为它只需要字母 - Is there a way to accept only alphabets as input and not numbers? If numbers are given it should show a validation error as it takes only alphabets 如何在javascript中创建三个随机数,然后判断输入是否为奇数? - how to create three random numbers in javascript and then tell input is odd or not? 如何使用生成随机数的 function 将随机项添加到数组 - how to add random items to an array using a function that generates random numbers 创建随机密码会生成长度为0的密码:Blame JavaScript或Java Servlet? - Creating a random password generates a password with length 0: Blame JavaScript or Java Servlet? 在JavaScript中使用随机数创建长度为n的数组 - Creating array of length n with random numbers in JavaScript JavaScript,生成一个长度为 9 个数字的随机数 - JavaScript, Generate a Random Number that is 9 numbers in length Javascript战舰Math.random-生成始终相同的数字 - Javascript battleship Math.random - generates always the same numbers 使用输入的Javascript随机数生成器(每个数字生成一次) - Javascript random number generator (generates every number once) using input
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM