Intermediate Algorithm Scripting1: Sum All Primes

Tell us what’s happening:
Why this method of finding prime number is not working.

Your code so far


function sumPrimes(num) {
let flag=0;
let a=[1,2,3];
for(let i=4;i<=num;i++){
  for(let j=2;j<=i/2;j++){    
if(i%j===0)    
{    
flag=1;    
break;    
}    
}    
if(flag==0)    
{   
 a.push(i);
 }
  
}
console.log(a);
return num;
}

console.log(sumPrimes(10));

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36.

Challenge: Sum All Primes

Link to the challenge:

You need to reset your flag on every loop iteration.

Also, note that 1 is not prime.

Lastly, you haven’t created a sum from your array.

Thank you so much. I haven’t done sum because my prime number was not coming correct.

1 Like