Problem 10: Summation of primes problem with test case

My code gives correct output when ran, but in FCC editor it does’t pass the last two tests. Below is my code:

function primeSummation(n) {
var sum = 2;

for (var i=3; i<n; i+=2){

var flag = 0;
for (var j=2; j<=Math.floor(Math.sqrt(i)); j++){
  if(i%j==0){
    flag = 1;
  }
}
if(flag==0){
  sum += i;
}

}
return sum;
}

primeSummation(17);

Could anyone please help me in figuring out this. Thanks!

it should be i<=n and not just i<n

hope this helps.

Tried this, this time only one case is qualified. Aren’t we supposed to compute the sum without including n?

It can be a performance issue, the function may time out before arriving to the Wanted result.

To make it more performing you could wrap this part in a isItPrime() function that returns false as soon as the flag is set to 1 - if it is divisible by 3 you don’t need to check all other numbers, do you? For the biggest numbers you are removing dozens of steps in this way

initiate Sum with 0 instead of 2 and start the loop at 2 instead of 3 as otherwise it will display the wrong value when the parameter value is 1.

Can you please post the link to the challenge? Is’s been a while since I did it and I can’t find it anymore.

I’m not able to post links. Its problem 10 under project Euler.

Slightly modified:
function primeSummation(n) {
var sum = 2;

for (var i=3; i<n; i+=2){

var flag = 0;
for (var j=3; j<=Math.floor(Math.sqrt(i)); j++){
  if(i%j==0){
    flag = 1;
    break;
  }
}
if(flag==0){
  sum += i;
}

}
return sum;
}

primeSummation(2000000);

But still no luck.