Intermediate Algorithm Scripting - Sum All Primes

Tell us what’s happening:
Describe your issue in detail here.
my code returns 18 when it is supposed to return 17. I want to know where is the error in my code.

Your code so far

function sumPrimes(num) {
  function checkPrimes(number){
    for(let i = 2; i< number; i++){
      if(number % i === 0) return false;
      
    }return true;
  }
  let answer=0;
  for(let j=0; j<= num; j++){
    if(checkPrimes(j)){
      answer += j;
    }
  }
  return answer;
}


console.log(sumPrimes(10));

Your browser information:

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

Challenge: Intermediate Algorithm Scripting - Sum All Primes

Link to the challenge:

Sorry, my previous answer was not clear enough…
Your checkPrimes function returns true for the value 1, because your i loop starts at 2, so discounts all values below this from the check.
A simple way of solving this would be to start your j loop from 2 also…

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.