Intermediate Algorithm Scripting - Sum All Primes

if the input is 10 the code works correctly but on the higher numbers like 977 something goes wrong.

function sumPrimes(num) {
    let result = 2
    for(let i = 2; i <= num; i++){

        for(let j = 2; j < i; j++){
            if(i % j !== 0){
                if(j == i - 1){
                result += i
                }
            } else if(i % j == 0){
            j =  2
            i++
        }
            if( i == num){
                break
            }
        }

    }
  return result;
}

console.log(sumPrimes(977));

Your browser information:

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

Challenge: Intermediate Algorithm Scripting - Sum All Primes

Link to the challenge:

What is different about the test cases? What is different about those two numbers, 10 and 977? OK, granted 977 is a difficult number. What I said that your code fails for 5, 7, and 11, among others.?

A big part of being a good programmer is being a good debugger, and that is mostly being a good detective.

1 Like

wat is the difference between your answer and the given correct answer?

You can add some console.log statements inside the code so you can see how the 977 is being handled and identify the issue.

1 Like

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