# Sum All Primes - I passed the challenge but confused because it doesn't work when I take out unit testing code

Tell us what’s happening:
So I passed this challenge , I vaguely remember having a similar challenge in a java or c++ class about 2 years ago and breezed right through it but I know every language is different this took me awhile in Javascript.
I was unit testing the code by populating an array to see what my if statement was capturing and all the sudden it worked… but the array and its initialized variable has nothing to do with the accumulator variable.

The confusing part is that when I take the array variable out all the sudden it doesn’t work anymore but, from what I can see it serves no readily apparent purpose… any help would be appreciated…

``````/* Intermediate Algo Challenge
*Sum All Primes
*Sum all the prime numbers up to and including the provided number.
*
*A prime number is defined as a number greater than one and having only two divisors, one and *itself. For example, 2 is a prime number because it's only divisible by one and two.
*
*The provided number may not be a prime.
*/

/* PSEUDOCODE
1. Pass argument num into funciton sumPrimes.
2. Declare and initialize variable currentPrime assign a value 1.
3. Declare a and initialize variable sumOfPrimes assign a value 0.
4. Start a for loop where i=0 and incraments by 1 that runs until <= argument sqrt num.
5. In the loop check if a number is prime if so add to sumOfPrimes.
6. Return sumOfPrimes when loop is complete.
***&. added another function for checking Prime not sure why I need array to pass challenge
*/

function sumPrimes(num) {
var currentNum = 2;
var sumOfPrimes = 0;
var primeArray = []; //WHYY?

function isPrime(numTest) {

for (x = 2; x < numTest; x++) {
if (numTest % x === 0) {
return false;
}

}

return true;

}

for (i= 2; i <= num; i++) {
//console.log(i);
if (isPrime(i) == true) {
primeArray.push(i);//WHYY??? Doesn't appear to do anything the accumulator below seems to do the job
sumOfPrimes += i;
}
// console.log(sumOfPrimes);

}
console.log(primeArray);
console.log(sumOfPrimes);
return sumOfPrimes;
}

sumPrimes(10);
``````

Your Browser User Agent is: `Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36`.

https://www.freecodecamp.org/challenges/sum-all-primes

Your code has been blurred out to avoid spoiling a full working solution for other campers who may not yet want to see a complete solution. In the future, if you post a full passing solution to a challenge and have questions about it, please surround it with `[spoiler]` and `[/spoiler]` tags on the line above and below your solution code.

If I comment out the following three lines of your code, I can pass all the tests just fine. You never used the contents of primeArray in any calculation, so it was really an unnecessary variable.

``````var primeArray = []; //WHYY?
``````
``````primeArray.push(i);//WHYY??? Doesn't appear to do anything the accumulator below seems to do the job
``````
``````  console.log(primeArray);
``````
1 Like

If you comment out all lines referencing primeArray then your code runs just fine with expected answers (at least for me).

1 Like

Sorry about that, thought I clicked blur. I don’t know maybe its my browser caching something no idea. So it works fine for everyone but not for me when I comment out those lines

Do you get any errors showing in the browser console (Ctrl + Shft + J in Chrome)?

1 Like

No, it’s working fine now. I tried several times before. Maybe I just forgot to comment out one of the lines in my previous attempts.

I was working on it for awhile. Well this wasn’t so bad… one challenge I spent 2 days tinkering with it and eventually basically refactoring it 3 or 4 times only to realize that the only thing wrong with my first go at it was a transposed orphanArray to orhpan. haha. thanks everyone.

1 Like