Sum All Primes Challenge

Sum All Primes Challenge
0

#1

It runs and return no output, plz help me!!!
Tks,

function checkPrime(num) {
  if (num == 1) return false
  for (i=2; i <= Math.sqrt(num); i++) {
    if (num % i == 0) return false;
  }
  return true;
}

function sumPrimes(num) {
  var result = 0;
  for (i=2; i <= num; i++) {
  	if (checkPrime(i) == true) {
  		result += i;
  	}
  }
  return result;
}

var input = 10;
console.log(sumPrimes(input));
type or paste code here

#2

This for loop declaration is the source of your problems. You did not use var i = 2, so i is global and since the checkPrime starts i at 2 each time it is called, it gets reset.

Always declare your variables within your functions or local scopes to prevent them from becoming global.


#3

:slight_smile: many thanks. It made me worry a lot :frowning: