finding prime in js
function showPrime(n){
for(let number=2;number<n;number++)
if(isPrime(number)) console.log(number);
}
function isPrime(number){
for (let factor = 2;factor<number;factor++ )
if(number%factor===0)
return false;
return true;
}
showPrime(20)
This code generates prime numbers from 2 to n-1. The isPrime function checks whether a number is prime or not by dividing it by all numbers from 2 to number-1. If it finds a factor of number, it immediately returns false, indicating that the number is not prime. If it doesn't find any factors, it returns true, indicating that the number is prime.
The output of showPrime(20) would be: 2 3 5 7 11 13 17 19.