I wish to find the sum of all prime numbers between range defined from 1 to N. The code gives an infinite loop when I call the sumPrimes function with a value of 3. I have debugged the code and found out that it does that only for the number 3. It does not do so for any other numbers above 2.
JavaScript:
function sumPrimes(num) {
var sum=0;
for (i = 2; i <= num; i++) {
if (checkPrime(i)) {
sum += i;
}
}
return sum;
}
function checkPrime(num) {
for (i = 2; i <= Math.sqrt(num); i++) {
if (num % i === 0) {
return false;
}
}
return true;
}