Intermediate Algorithm Scripting - Sum All Primes

Hi everyone. I have a doubt regarding the following code:


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;


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:

This code will return 0 in such case. Remember that we are returning sum which is initialized with 0.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.