why my code isn’t like it should ? i can’t find the bug ! help me please!

here is my code:

function sumPrimes(num) {
let sum = 0;
let indi = 0;
for(let i = 2; i < num; i++){
for(let j = 1; j <= i; j++){
if(i % j == 0){
indi = indi + 1;
}
}
if(indi >= 2){
sum = sum + i;
}
}
return sum;
}
console.log(sumPrimes(10));

Your browser information:

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

Hello!
It looks like you’re trying to determine if i is prime by setting the indi to false when i is divisible by j. The problem you’re having is that setting indi to false doesn’t stop the loop; when i = 4 and j = 2, indi gets set to false. But the j loop iterates, i = 4 and j = 3, which sets indi back to true.

how about that one ? i got ride of the true false thing but it’s still not working!

function sumPrimes(num) {
let sum = 0;
let indi = 0;
for(let i = 2; i < num; i++){
indi = 0;
for(let j = 1; j <= i; j++){
if(i % j == 0){
indi = indi + 1;
}
}
if(indi = 2){
sum = sum + i;
}
}
console.log(indi);
return sum;
}
console.log(sumPrimes(10));

If you do that, you can see that i = 4 is still passing your tests.

Let’s look at why.

for(let j = 1; j <= i; j++)
If j is always starting at 1, then indi will always have a value of at least 1. Because i % 1 will always equal 0.
If j is also allowed to equal i, then indi will now always have a value of at least 2, because i % i will always equal 0.

Take a good look at your conditions again and think about how prime numbers work.

when indi = 2 that means the i is prime because we can only can divide it on 1 and i look at my code when i do console.log(indi) it prints 2 and not 4 . i understand how prime numbers work that was not the case

function sumPrimes(num) {
let sum = 0;
let indi = 0;
for(let i = 2; i < num; i++){
indi = 0;
for(let j = 1; j <= i; j++){
if(i % j == 0){
indi = indi + 1;
}
}
if(indi = 2){
sum = sum + i;
}
}
console.log(indi);
return sum;
}
console.log(sumPrimes(10));

Welcome to optimisation. Your code works now, but it takes too long to run and trips the infinite loop protection. Now you’ve got to find ways to skip steps.

you can optimise in a couple of ways:
to check if a number if prime:

if it is divisible by number two it is not prime, so you can avoid checking all even numbers

if it is divisible by any number above 1 it is not prime, and you can stop the loop (check break keyword)

to know if a number is prime you can stop checking at the square root of that number. If there aren’t numbers equal or smaller than the square root that can divide the number, than there aren’t even above it and it is prime.

if you can implement all this your algorithm should manage