Can't find the error in my javascript code

Posted by Olivier on Stack Overflow See other posts from Stack Overflow or by Olivier
Published on 2013-06-27T12:30:08Z Indexed on 2013/06/27 16:21 UTC
Read the original article Hit count: 202

Filed under:
|

This js program should display the first 100 prime numbers, but instead it crashes each and every time and I can't find the error! Could someone point me towards the best way to debug js code?! Thank you!

// 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++;
}

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about debugging