这个 js 程序应该显示前 100 个素数,但它每次都崩溃,我找不到错误!有人可以指出我调试 js 代码的最佳方法吗?!谢谢!
// initialisation of the array p holding the first 100 prime numbers
var p = [];
// set the first prime number to 2
p.push(2);
// find the first 100 prime numbers and place them in the array p
var i = 3;
while (p.length < 100) {
var prime = true;
loop:
for (var item in p){
if (i%item === 0){
prime = false;
break loop;
}
}
if (prime)
p.push(i);
i = i + 2;
}
// display the first 100 prime numbers found
var i=1;
for (var item in p){
document.writeln(i,item);
i++;
}