Stuck on: Sum All Primes (Solved)

Stuck on: Sum All Primes (Solved)
0

#1

My code gets the correct answer, but I get the red ‘x’ and don’t pass. Any advice?

var holder = 0;
var numHolder =[];
function sumPrimes(num) {
for (var j = 1; j <= num; j++){
numHolder.push(j);

}

for (var x = 0; x < numHolder.length; x++){
if (testPrime(numHolder[x])){
holder += numHolder[x];
}
}

function testPrime(n){

if (n===1){
return false;
}
else if(n===2){
return true;
}

else {
for (var i = 2; i < n; i++){
if (n % i === 0){
return false;
}

}
return true;
}

}
return holder;
}

sumPrimes(10);


#2

You need to move the following lines in to be inside your function named sumPrimes. Because they are global variables, for each subsequent test, these variables do not get reset. Moving them inside the function allows them to reset for each test.

var holder = 0;
var numHolder =[];

#3

Thanks! I don’t know why I declared them globally in the first place…