1
votes

I am trying to write a script which displays the prime numbers from 0 to 100, but when I execute it, the browser crashes. JSHint didn't detect any error.

I'd like to learn why this code doesn't work: I am not interested in finding a totally different code( like this one) that completes the same task.

This is the first code I've ever written, so I apologise in advance for all the silly mistakes I overlooked.

var i;
var m;
var primeArr = [2, 3, 5, 7, 11, 13, 17, 19];
var theMaxNumber = 100;
var theMinNumber = 21;
var theCounter = -1;

function myFunction() {
        for (i = theMinNumber; i < theMaxNumber; i += 2) {
            for (m = 0; m < primeArr.length; m++) {
                if (i % primeArr[m] !== 0) {
                    theCounter++;
                    if (theCounter === primeArr.length) {
                        primeArr.push(i);
                    } 
                    if (m === primeArr.length) {
                        theCounter = -1;
                    }
                } 
            } 
        } 
       console.log( primeArr.toString());
    }  

This is how it should work in theory:

1) the function finds out whether or not the number i is divisible for a prime number smaller then itself.

2) In case it is, theCounter is resetted and i is incremented by two.

3) In case it isn't, theCounter is incremented by one. If, at the end of the cycle, i is not divisibile for all the prime numbers smaller than itself, it means that it's a prime number: i is pushed in the array (because theCounter = == primeArr.length), then i is incremented by two.

edit: I fixed all the errors in the code, it works perfectly now:

var i;
var m;
var primeArr = [3, 5, 7, 11, 13, 17, 19];
var theMaxNumber = 100;
var theMinNumber = 21;
var theCounter = 0;

function myFunction() {
    for (i = theMinNumber; i < theMaxNumber; i += 2) {
        theCounter = 0;
        for (m = 0; m < primeArr.length; m++) {
            if (i % primeArr[m] !== 0) {
                theCounter++;
            }
            if (theCounter === primeArr.length) {
                primeArr.push(i);
            }
        }
    }
    primeArr.unshift(2);
   console.log( primeArr.toString());
}
1
Play it on paper. Only doing something when i % primeArr[m] == 0 makes sense as then you have found a factor. JavaScript is an unfortunate choice for fast & good learning. Maybe mainstream java would do. (Opiniated) - Joop Eggen
The array which you have defined for prime numbers is already contains only prime numbers. what you are trying to find here? in this line of code if (i % > primeArr[m] !== 0) { w++; --> Trying to increment a variable which is not declared anywhere. - shanthi_karthika
I accidentally wrote w instead of theCounter, thanks for your remark. I wrote that line of code in order to divide i for every prime number in the list. If i is not divisible for any of them (the modulus is not 0), it means that it 's a prime too and it 's added in the list. At the and of the first "for loop", all the primes from 21 to 100 should have been added to primeArr. - Corrado Biolatti

1 Answers

0
votes

Your stop condition is wrong in the inner for loop it has an infinite loop increasing primeArr for ever until it crashes the JS engine. Add an "alert" or "debugger" command to see the issue

        for (m = 0; m < primeArr.length; m++) {
            if (i % primeArr[m] !== 0) {
                theCounter++;
                if (theCounter === primeArr.length) {
                    primeArr.push(i);
                }