我希望构建一个非常简单的网页,它生成一个随机数(整数),对应于用户提供的三个参数:
到目前为止,这就是我所拥有的:
<!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>
我想我已经找到了下限/上限,但我不能说数字位数相同。
谢谢!
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;
}