Q. Rewrite sumPrimes so it returns the sum of all prime numbers that are less than or equal to num.

Ans.

//Question: Rewrite sumPrimes so it returns the sum of all prime numbers that are less than or equal to num.
answer:
function sumPrimes(num) {
// Helper function to check primality
function isPrime(num) {
for (let i = 2; i <= Math.sqrt(num); i++) {
//this is the confusion, if num = 10, wont num % 2 == 0, hence the prime 2
//becomes false
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;
}
sumPrimes(10);

Learning to describe problems is hard, but it is an important part of learning how to code.

Also, the more information you give us, the more likely we are to be able to help.

Iâ€™ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the â€śpreformatted textâ€ť tool in the editor (</>) to add backticks around text.