Sum All Primes problem. orgaization? emptying values?

the j loop var “factors” is building up and ruining the code. Not sure how to fix. :slight_smile:

function sumPrimes(num) {
  var allNum= [];
  var factors = [];
  var answer =[];
  var i;
  var j;
  var prime= [];
  
  //loop thru all numbers (num) and down
  for (i=2; i <= num; i++){

  //build array of suspect numbers factors of [i]
    for (j = 1; j <= num; j++) {
      if (num % j === 0) {
        factors.push(j);
      }
    }
  // check array for length to determine if prime
    if (factors.length < 3){
      prime= Math.max.apply(Math, factors);
    }
    
    answer.push(prime);
  }
  return answer;
}
sumPrimes(7);

OK, one problem is that you have your factors variable that never gets reset for each check, so it just keeps getting added to. I put it as a local variable in the i loop.

Next, you need to check if j is a factor of i, not num. You are checking is i is prime, not num. (Although i will eventually = num and we’ll check it then.)

Then, when you assign prime, you want it to be i, i.e., if the number of factors is less 3, then i is prime so it gets pushed. And that push needs to be inside the if statement - you don’t want a residual prime value pushed if it is leftover.

Lastly, this needs to return a sum of the primes, not an array of the primes (as you have here.) But I’ll leave that task to you. As this is, it returns an array of primes up to num:

function sumPrimes(num) {
var allNum= [];
var answer =[];
var i;
var j;
var prime= [];

//loop thru all numbers (num) and down
for (i=2; i <= num; i++){
	var factors = [];

	//build array of suspect numbers factors of [i]
	for (j = 1; j <= i; j++) {
		if (i % j === 0) {
			factors.push(j);
		}
	} // for j
// check array for length to determine if prime
	if (factors.length < 3){
			prime= i;
			answer.push(prime);
	}
	} // for i
	return answer;
} // sumPrimes()

console.log(" answer = " + sumPrimes(13));

Note that you could save a little time by looping from j = 2 to j < i. You know that i will always be divisible by 1 and i, so why check? Just change your if statment to (factors.length < 1).

That would give you this:

function sumPrimes(num) {
var allNum= [];
var answer =[];
var i;
var j;
var prime= [];

//loop thru all numbers (num) and down
for (i=2; i <= num; i++){
	var factors = [];

	//build array of suspect numbers factors of [i]
	for (j = 2; j < i; j++) {
		if (i % j === 0) {
			factors.push(j);
		}
	} // for j
// check array for length to determine if prime
	if (factors.length < 1){
			prime= i;
			answer.push(prime);
	}
	} // for i
	return answer;
} // sumPrimes()

console.log(" answer = " + sumPrimes(27));

Like I said, you still need to reduce that array of primes down to a sum. I did it a little differently - rather than generating an array of the primes, I just summed them as I went. So, you answer would just be a number starting at 0 (not an array) and you’d += each prime. You don’t even need the variable prime, just += i.

1 Like

Thanks so much… didnt think about moving “var factors” into the loop.
the looping j with num… I apologize! I had that worked out and changed that back and forth while trying to debug and just gave you the wrong copy.
and “array of primes” to primes total was just the next step.
I really appreciate your help.

No need to apologize. It’s easy to get your brain twisted into a pretzel on these things. On more than one of these I’ve spent too much time and then had to start from scratch aND rethink my algorithm. Just keep at it, the more of these you do, the easier it gets. Even after completing your FE cert, seek out algorithm challenges like Codewars to hone your skills. Just keep at it and don’t get frustrated.

1 Like