<!doctype html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Document</title>
</head>
<body>
<input type="text" id="text"><input type="button" value="提交" onclick="submit()">
<script>
function submit(){
var text = document.getElementById("text").value;
var arr = fn(text);
if(arr.length>0){
alert(arr);
}
}
function fn(n) {
var min = 5;var max = 39; //随机数范围
var pattern = /^[1-9]$|^[1-3][0-5]$/;//验证5-39的数字
var arr = [];var temp = [];
if (pattern.test(n)) {
arr = arr.range(min, max);
console.log(arr);
for (var int = 0; int < n; int++) {
temp.push(arr.splice(parseInt(Math.random() * (arr.length-1)), 1).toString());
}
} else {
alert("输入不合法,请输入不大于" + (max - min + 1) + "的正整数!");
}
return temp;
}
Array.prototype.range = function(start, end) {//生成指定范围的数组
var _self = this;
var length = end - start + 1;
var step = start - 1;
return Array.apply(null, {
length : length
}).map(function(v, i) {
step++;
return step;
});
}
</script>
</body>
</html>