Finding prime number in javascript
There are several ways of checking if a given number is prime or not. I want to find it in by own way and if the given number is not prime, I am trying to find next higher prime number. so far I tried the following and can't understand why it's creating infinite loop. Could you please correct my code. Or tell me what wrong I am doing here. function myFunction(a){ for(i=2; i<=a; i++){ if(a % i != 0){ console.log(" Prime ") } else{ a++ // creates infinite loop if(a % i == 0){ console.log("not prime") } else{ console.log("prime") } } } console.log(a) } myFunction(38) // Expected 41
0 upvotes
View original discussion ↗
Comments
Sign in or sign up to comment and upvote.
No comments yet.