Sum All Primes bug

Tell us what’s happening:

hello guys my code is working just fine in repl.it but not here for the last step

Your code so far


function sumPrimes(num) {
  let arr = []
  let pri = []
  for (let i = 2 ; i <= num ; i++){
    arr.push(i)
  }
 for (let i = 0 ; i<arr.length ;i++){
   for (let j = 0 ; j<arr.length ;j++){
     if (arr[i]%arr[j] === 0 && arr[i] !== arr[j]){
     delete arr[i]
     }
   }
 }
 
  return arr.reduce(function (a,b){return a+b});
}

sumPrimes(10);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:66.0) Gecko/20100101 Firefox/66.0.

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

It’s not a bug: the execution time is heavily constrained on FCC, and the huge amount of work your code is doing makes your solution very slow. You need to try to rethink/optimise the algorithm you’ve chosen to use.

For example, if you try to run it here (an app that allows you to walk though every operation a function goes through), the app will refuse to run your function because of the number of operations you’re doing:

http://pythontutor.com/javascript.html#mode=edit

1 Like