Sum of all primes numbers

hello,

Can someone help me please with this algorithm?

my example is 10.
(A prime number is a whole number greater than 1 with exactly two divisors: 1 and itself. For example, 2 is a prime number because it is only divisible by 1 and 2. In contrast, 4 is not prime since it is divisible by 1, 2 and 4.
Rewrite sumPrimes so it returns the sum of all prime numbers that are less than or equal to num.)

function sumPrimes(num) {
var x=2;
var added=2;
while (x<num) {
x++;
for (var i=2;i<x;i++){
if (x%i===0){
break;
}
else if (i===x-1) {
added += x;
}
}
}
return added;
}

but I am lost whem x =4.

x++; (x=3)
i=2;
(i===x-1); (2 === 3 -1 )
added += 3;

then
x++; x=4;
i=3;
(i===x-1); (3 === 4 -1 )
added += 4; But… 4 should not be add since is about all primes numbers <10.

Can someone help me please?
Thank you in advance!

Reformatted using “preformatted text”. Also, I’m guessing this is Javascript, you should identify the language.

function sumPrimes(num) {
  var x=2;
  var added=2;
  while (x<num) {
    x++;
    for (var i=2;i<x;i++){
      if (x%i===0){
        break;
      }
      else if (i===x-1) {
        added += x;
      }
    }
  }
return added;
}

I’ve tested this as Javascript, and it works correctly.

In your analysis of x=4, you’re looking at the (i===x-1) portion of the if statement. But you’ve failed to take the first part into account. When x=4, then x%i===0 is true when i=2, and the break statement exits the loop; i should not reach 3 in that case.