2

I am trying to print all prime numbers between two given numbers (num1 and num2). If I am using a different variable name in the for loop of isPrime function, that works as expected. But while using same variable name as i it goes into an infinite loop. Why is this the case?

Below is my code:

showPrimeNumbers();

function showPrimeNumbers(){
    var num1 = 10;
    var num2 = 15;
    for (i=num1; i<=num2; i++){
        if(isPrime(i)){
            console.log(i+" is a prime number.");
        }
    }
}

function isPrime(num){
    var flag = true;
    for (i=2;i<=num-1;i++){
        if(num%i == 0){
            flag=false;
            break;
        }
    }
    return flag;
}
halfer
  • 19,824
  • 17
  • 99
  • 186
  • 2
    You haven't declared `i` in either functions, it is global and both loops are messed up with its value. – Teemu Jul 06 '18 at 06:55
  • 1
    because `i` is global. Declare it individually in each `for`. So, instead of `for (i` have `for ( var i` – Adelin Jul 06 '18 at 06:56
  • 1
    btw your `isPrime` function can be made to be a slight bit faster https://stackoverflow.com/questions/40200089/is-a-number-prime – Jhecht Jul 06 '18 at 07:04

3 Answers3

2

Your is do not have var or let in front of them, so they're both referring to (and implicitly assigning to) the same global variable. So, every time isPrime runs, i gets reset to either 3 or 4, repeating - it'll never get to 15.

Never implicitly assign to global variables. Put let in front of the is instead.

Something else that might make the code a bit clearer, instead of having a flag variable that might be reassigned, would be to simply return false or return true:

showPrimeNumbers();

function showPrimeNumbers(){
    var num1 = 10;
    var num2 = 15;
    for (let i=num1; i<=num2; i++){
        if(isPrime(i)){
            console.log(i+" is a prime number.");
        }
    }
}

function isPrime(num){
    for (let i=2;i<=num-1;i++){
        if(num%i == 0){
            return false;
        }
    }
    return true;
}
CertainPerformance
  • 356,069
  • 52
  • 309
  • 320
1

You aren't using var. This means that JavaScript searches up and up the nested locals until it finds a variable i, and if it can't find one it creates a global variable. This means that the two functions share the variable i.

Fix it by writing var i; somewhere in each function.

In future, consider using strict mode by writing "use strict" at the beginning of each function body. It makes these sorts of errors much easier to spot.

wizzwizz4
  • 6,140
  • 2
  • 26
  • 62
0

Your problem is that you declared the i variable without var keyword in your loops, so it will be a global variable in the window scope.

So both functions will be accessing the same i variable, and it will be updated by both functions in the same time, so isPrime() will reset the i variable to 2 and it won't be higher than 4, so the exiting condition of your first for loop won't be met because i<=num2 will remain always true, that's why you got the infinite loop.

Just use var when you declare it inside each loop, so you will be dealing with two separate variables, where each one is local to a different function's scope.

for (var i=num1; i<=num2; i++)

And:

for (var i=2;i<=num-1;i++)
cнŝdk
  • 31,391
  • 7
  • 56
  • 78