Sum All Primes Challenge

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

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;
type or paste code here

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.

1 Like

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