[英]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.