Uuuuummhhhhh.. Broken console?

So the code return the correct values but the test doesn’t pass.
I can’t seem to grasp why tho…
I summon your help.
please

Your code so far


let sum = 0;
let check;
function sumPrimes(num) {
for (let i = num; i > 1; i--) {
  for (let j = i-1; j > 1; j--) {
    if (Number.isInteger(i/j) == false){
      check = false;
    } else {
      check = true;
      break;
    }
  }
    if (check === false) {
      sum += i
    }
}
return sum
}
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/80.0.3987.163 Safari/537.36.

Challenge: Sum All Primes

Link to the challenge:

Is there any particular reason you’re declaring these variables outside function body?

1 Like

When you declare sum and check outside of the function body they basically become global variables and thus sum does not start over at 0 for each function call.

1 Like

Removing the function call is not the answer. The answer is to not use global variables.

1 Like

Not really… so basically if it’s not global you shoud always declare your variables in the functions that they are used? thanks 4 your answer cheers

Yeah, unless you really really need them, you should always avoid global variables. They make for a mess of side effects.

1 Like