I was working on the Sum All Primes question and made this code:
function sumPrimes(num) {
var primeArray = [];
//Add all odd prime numbers to array
for (var i = 2; i <= num; i++) {
var isPrime = true;
for (var j = 2;j < i;j++){
if (i % j===0) {
isPrime = false;
}
}
if(isPrime) {
primeArray.push(i);
}
}
//calculate final total
var output = 0;
for (var k = 0; k < primeArray.length; k++) {
output += primeArray[k];
}
console.log(output);
return output;
}
When I test the code on JSBin.com and Chrome for sumPrimes(977), I get 73156 which is the correct answer. However, when I test the code on Firefox, the sum is inconsistent and gives me a different answer each time.
Is this a browser issue or is there something wrong with my code?