1

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());
}
Community
  • 1
  • 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 Aug 26 '16 at 12:18
  • 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 Aug 26 '16 at 12:44
  • 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 Aug 26 '16 at 16:53

1 Answers1

0

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);
                } 
JavaSheriff
  • 6,447
  • 17
  • 79
  • 143
  • 1
    Wow, I didn't thought about that! Even though simply adding a "break;" doesn't make the code work, at least now is clear why it crashed! I'll upvote as soon as I'll have 15 or more reputation (now I can't) – Corrado Biolatti Aug 26 '16 at 18:00