我希望构建一个非常简单的网页,它会生成一个与用户提供的三个参数相对应的随机数(整数):
到目前为止,这就是我所拥有的:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<!--
https://docs.google.com/document/d/15tsEtcmWrukVp8NBBxQnBpW-YoegjD_fpyhAgH_7ijA/edit
-->
<form name="settings">
How many numbers should be generated? <input type="number" min="1" max="6" name="digits"/>
<br/>
Smallest number in the range <input type="number" min="1" max="999998" name="lownum"/>
<br/>
Biggest number in the range <input type="number" min="2" max="999999" name="highnum"/>
<input type="submit" onclick="validate()">
</form>
<script>
function validate(){
if(parseInt(document.settings.lownum.value)>parseInt(document.settings.highnum.value))
{
alert("!");
}
else
{
document.write(Generator());
}
}
function generator() {
var min = parseInt(document.settings.lownum.value);
var max = parseInt(document.settings.highnum.value);
return Math.floor(Math.random() * (max - min +1)) + min
}
</script>
</body>
</html>
&#13;
我想我已经找到了上限/下限,但我不能对数字位置说同样的话。
谢谢!
答案 0 :(得分:0)
function lineGenerator() {
// Array is better than this, but I'm keeping my answer the simplest
var result = '';
// Assume it's already validated
var amount = document.settings.numbers.value;
// Build the string
for (var i = 0; i < amount; i++) {
if (result) {
result += ', ';
}
result += generator();
}
return result;
}