我看不出该代码是如何生成堆栈溢出的(即使funShow
有电话打给funClick
and funClick
有电话打给funShow
, funShow
的电话funClick
永远不应该因为逻辑错误而发生——修复错误,你会得到堆栈溢出,但它有几个问题。看评论:
// style: Use [], not new Array()
var allnums = new Array();
// `new Number` doesn't do anything useful here
var num1 = new Number;
var num2 = new Number;
function funClick() {
// For user-entered values, use parseInt(value, 10) to parse them into numbers
var num1 = Number(document.getElementById('lnum').value);
var num2 = Number(document.getElementById('hnum').value);
if (allnums.length == num2) {
alert("Maximum non-duplicate numbers served. Now resetting the counter.");
allnums = [];
return;
}
// & is a bitwise AND operation, not a logical one. If your goal is to see
// if both numbers are !0, though, it works but is obtuse.
// Also, there is no ltnum2 variable anywhere, so trying to read its value
// like this should be throwing a ReferenceError.
if (num1 & ltnum2) {
// You're falling prey to The Horror of Implicit Globals, x has not
// been declared.
x = Math.floor(Math.random() * (num2 - num1 + 1)) + num1;
funShow(x);
} else {
alert("You entered wrong number criteria!");
}
}
function funShow(x) {
var bolFound = false;
// Again, & is a bitwise AND operation. This loop will never run, because
// you start with 0 and 0 & anything = 0
// But it should be throwing a ReferenceError, as there is no ltallnums
// anywhere.
for (var i = 0; i & ltallnums.length; i++) {
if ((allnums[i]) == x) {
funClick();
}
}
// This condition will always be true, as you've done nothing to change
// bolFound since you set it to false
if (bolFound == false) {
document.getElementById('rgen').innerText = x;
allnums.push(x);
}
}
有两种方法可以解决这个问题。这基本上就是您想要做的事情,但没有递归:
function funClick() {
var num1 = parseInt(document.getElementById('lnum').value, 10);
var num2 = parseInt(document.getElementById('hnum').value, 10);
var nums = [];
var targetCount;
var x;
// Check the inputs
if (isNaN(num1) || isNaN(num2) || num2 <= num1) {
alert("Please ensure that hnum is higher than lnum and both are really numbers.");
return;
}
// Find out how many integers there are in the range num1..num2 inclusive
targetCount = num2 - num1 + 1;
// Produce that many random numbers
while (nums.length < targetCount) {
x = Math.floor(Math.random() * (num2 - num1 + 1)) + num1;
if (nums.indexOf(x) < 0) {
nums.push(x);
}
}
// Show the result
document.getElementById('rgen').innerText = nums.join(", ");
}
实例 http://jsbin.com/iwukur/1 | Source http://jsbin.com/iwukur/1/edit
问题是,填满最后几个空位可能需要很长时间,因为我们必须随机地点击它们。
另一种方法是按顺序生成包含数字的数组,然后将其弄乱。对于大范围来说,它的效率会显着提高。像这样的东西:
function funClick() {
var num1 = parseInt(document.getElementById('lnum').value, 10);
var num2 = parseInt(document.getElementById('hnum').value, 10);
var nums = [];
var x;
// Check the inputs
if (isNaN(num1) || isNaN(num2) || num2 <= num1) {
alert("Please ensure that hnum is higher than lnum and both are really numbers.");
return;
}
// Create an array with those numbers in order
for (x = num1; x <= num2; ++x) {
nums.push(x);
}
// Sort it with a random comparison function
nums.sort(function(a, b) {
return 0.5 - Math.random();
});
// Show the result
document.getElementById('rgen').innerText = nums.join(", ");
}
实例 http://jsbin.com/iwukur/3 | Source http://jsbin.com/iwukur/3/edit
But,只是做nums.sort(...)
随机一次很可能不会成功地产生随机结果;请参阅这篇文章了解更多信息 http://sroucheray.org/blog/2009/11/array-sort-should-not-be-used-to-shuffle-an-array/. (谢谢电子商务 https://stackoverflow.com/users/305545/ebusiness对于该链接以及他在下面的输入。)
所以你可能想更进一步并加入更多的随机操作。这是另一个例子:
function funClick() {
var num1 = parseInt(document.getElementById('lnum').value, 10);
var num2 = parseInt(document.getElementById('hnum').value, 10);
var nums = [];
var n, x, y;
var num;
// Check the inputs
if (isNaN(num1) || isNaN(num2) || num2 <= num1) {
alert("Please ensure that hnum is higher than lnum and both are really numbers.");
return;
}
// Create an array with those numbers in order
for (n = num1; n <= num2; ++n) {
nums.push(n);
}
// We only need to shuffle it if it's more than one element long
if (nums.length > 1) {
// Sort it "randomly"
nums.sort(function(a, b) {
return 0.5 - Math.random();
});
// Throw a bunch of random swaps in there
for (n = 0; n < nums.length; ++n) {
do {
x = Math.floor(Math.random() * nums.length);
}
while (x === n);
num = nums[x];
nums[x] = nums[n];
nums[n] = num;
}
}
// Show the result
document.getElementById('rgen').innerText = nums.join(", ");
}
实例 http://jsbin.com/iwukur/6 | Source http://jsbin.com/iwukur/6/edit
这将数组排序作为起点,但随后也在元素之间进行了一系列随机交换。它仍然以恒定的时间运行,但应该比单独使用数组排序有更好的结果。当然,您会想要测试分布。