Sum All Primes question about .every

I am wondering how .every is working here when not all numbers are not % !==0 there are some that are % ==0 help me understand this thank you .

  **Your code so far**
function sumPrimes(num) {
// Check all numbers for primality
let myArr =[];
for (let i= 2; i<=num; i++){
 if(myArr.every((prime) => i % prime !==0)){
  myArr.push(i)
 }
  console.log(myArr)

}
return myArr.reduce((initial,end) => initial + end,0)
}


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/102.0.0.0 Safari/537.36

Challenge: Sum All Primes

Link to the challenge:

Adding extra comments.

function sumPrimes(num) {
// Check all numbers for primality
//initializes empty array
let myArr =[];
//iterates through numbers from two to the given input
for (let i= 2; i<=num; i++){
//if i isn't divisible by any of the primes in the array so far, pushes i onto the array 
 if(myArr.every((prime) => i % prime !==0)){
  myArr.push(i)
 }
  console.log(myArr)

}
return myArr.reduce((initial,end) => initial + end,0)
}


console.log(sumPrimes(10));

If any of the primes in the list so far have a 0 remainder when i is divided by them, i is prime and does not belong in the list. It doesn’t get added.

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