Sum All Primes don't work with 977 number in test

Sum All Primes don't work with 977 number in test
0

Tell us what’s happening:

Your code so far


function sumPrimes(num) {
        let count = num / 2-1;
        let arr = [];
        let sum = 0;
        for (let i = 2; i < num; i++) {
            arr.push(i)
        }
while (count !== 1) {
    for (let i = 0; i < arr.length; i++) {
        if (arr[i] !== 0 && arr[i] !== count) {
                if (arr[i] % count === 0) {
                    arr[i] = 0
                }
        }
    }
    count--;
}
        for (let i = 0; i < arr.length; i++) {
            if(arr[i] != 0) {
               sum += arr[i];
            }
        }
        return sum
}
console.log(sumPrimes(977));

Your browser information:

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

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/sum-all-primes

Here is a working approach did:

function sumPrimes(num) {
  let arr = [];
  let prime = 0;
  for (let i = 1; i <num + 1; i++) {
    arr.push(i);
  }

  for (let ele of arr) {
    let count = 0;
    for (let lel = 1; lel < ele + 1; lel++) {
      if (ele%lel == 0) {
        count++
      }
    }
    if (count == 2) {
      prime += ele;
    }
  }
  return prime;
}

Your method is very computationally heavy. I tried to keep mine as simple as possible, to avoid too much confusion.

1 Like