Intermediate Algorithm Scripting: Sum All Primes - need your help!

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.

Challenge: Sum All Primes

Link to the challenge:

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.

1 Like

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));


Because now your indi grows and grows and grows. So indi >=2 becomes true for every value of i.

sould i then put indi = 0 every time the second for finish looping ?


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));

I think your code might be correct except for one thing.
Here you are setting the value of indi to 2, when you should be comparing it with == or ===.

1 Like

lol how did i not see that thanks ! but its still not working very well . look

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. :slight_smile:

lol now i understand why people say it’s hard . thanks for help man

1 Like

You can use the eratothenes sieve to get a more efficient method to sum all primes under a number. Don’t click this code unless you want a spoiler

function sumPrimes(num) {
var arr = [];
for(let i = 0; i <= num; i++) {
arr.push(i);//Array of numbers below num
}
for(let j = 1; j < Math.sqrt(num); j++) {
for(let i = 0; i < arr.length; i++) {
if(arr[i] % j == 0 && arr[i] !== j) {
arr.splice(i, 1);//deletes non-primes
}
}
}
}
1 Like

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

1 Like

Thank you i’ll try to fix it