Hi everyone. I have a doubt regarding the following code:
/spoiler/
function sumPrimes(num) {
// Helper function to check primality
function isPrime(num) {
for (let i = 2; i <= Math.sqrt(num); i++) {
if (num % i == 0)
return false;
}
return true;
}
// Check all numbers for primality
let sum = 0;
for (let i = 2; i <= num; i++) {
if (isPrime(i))
sum += i;
}
return sum;
}
/spoiler/
Wouldn’t this code simply return false in the case where the provided number is not a prime rather than finding the sum of all prime numbers available that are less that than the user provided number.
Also how does the primality of the target number help in this particular case?
Challenge: Intermediate Algorithm Scripting - Sum All Primes
Link to the challenge: