我想创建一个javascript程序,让你生成一个运行x次的随机数。取决于用户输入的内容。但是我已经尝试过太多次了,但仍然无法正常工作。
这是我的代码:
function randomString() {
var totalIterations = parseInt(document.getElementById('input').value);
var output = document.getElementById('output');
output.innerHTML = '';
var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
var string_length = 8;
var randomstring = '';
for (var i = 0; i < string_length; i++) {
var rnum = Math.floor(Math.random() * chars.length);
randomstring = randomstring + chars.substring(rnum, rnum + 1);
for (var randomstring = 0; randomstring <= totalIterations; i++) {
var item = document.createElement('div');
item.innerHTML = randomstring;
output.appendChild(item);
}
}
}
<form name="randform">
<input type="button" id="input" value="Create Random String"
onClick="randomString();">
<input type="text" name="randomfield" value="">
</form>
<div id="output"></div>
答案 0 :(得分:0)
您必须更改一些代码 添加id输入,如id =“randNum” 函数randomString(){
//and use id instead of input because it is selecting first input element;
var totalIterations = parseInt(document.getElementById('#randNum').value);
// Add # before output;
var output = document.getElementById('#output');
output.innerHTML = '';
var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
var string_length = 8;
var randomstring = '';
for (var i=0; i<string_length; i++) {
var rnum = Math.floor(Math.random() * chars.length);
randomstring= randomstring + chars.substring(rnum,rnum+1);
for(var randomstring=0; randomstring <= totalIterations; i ++) {
var item = document.createElement('div');
item.innerHTML = randomstring;
output.appendChild(item);
}
答案 1 :(得分:0)
也许以下内容可以提供帮助:
const pickRandom = (min,max)=>
Math.floor(Math.random()*max)+min;
const pickFrom = array =>
array[pickRandom(0,array.length-1)];
const pick = (array,amount,doubles) => {
const recur = (numbers,array) => {
if(numbers.length>=amount){
return [numbers,array];
}
const picked = pickFrom(array);
return recur(
numbers.concat(picked),
array.filter(item=>item!==picked || doubles)
);
}
return recur([],array);
}
console.log(pick(
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz".split(""),
10,//how many characters
true//if double values are allowed
)[0].join())
答案 2 :(得分:0)
您的代码中存在以下几个问题:
input
元素读取用户输入,这是一个按钮i
。我不知道它是什么。 我在下面的代码段中解决了上述所有问题。那是你想要实现的吗?
function randomString() {
var totalIterations = parseInt(document.getElementById('input').value);
var output = document.getElementById('output');
output.innerHTML = '';
var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
var stringLength = 8;
for (var iterations = 0; iterations < totalIterations; iterations++) {
var item = document.createElement('div');
// generating the randomString by calling another function :
item.innerHTML = generateRandomString(stringLength, chars);
output.appendChild(item);
}
}
function generateRandomString(length, chars) {
var randomstring = '';
// generating the random string
for (var i = 0; i < length ; i++) {
var rnum = Math.floor(Math.random() * chars.length);
randomstring = randomstring + chars.substring(rnum, rnum + 1);
}
return randomstring;
}
&#13;
<form name="randform">
<input type="button" value="Create Random String"
onClick="randomString();"> </input>
<input type="text" id="input" name="randomfield" value="">
</form>
<div id="output"></div>
&#13;
既然我知道你想去哪里,我强烈建议你使用另一个函数来生成randomstring。它会使您的代码更具可读性。