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