js all prime numbers code example
Example 1: prime numbers javascript
function isPrime(num) {
for (let i = 2; i * i <= num; i++)
if (num % i === 0)
return false;
return num > 1;
}
Example 2: sum prime numbers javascript
function sumPrimes(num) {
let i = 1;
let sum = 0;
while (i <= num) {
if (isPrime(i)) {
sum += i;
}
i++;
}
return sum;
}
//function to check if a number is prime or not
function isPrime(x) {
for (let i = 2; i < x; i++) {
if (x % i === 0) return false;
}
return x !== 1 && x !== 0;
}
//test here
sumPrimes(10);