Searched for factors

A test does not work while another does. This code with the argument 10 works, but not with 977. The factors of num appear as num on the high number. This might be due to lack of memory. I tried making the second loop condition i < (num/2) without entirely understanding it, and that failed. I am looking for problems.

function sumPrimes(num) {
  let countup = [];
  for (let i = 2; i <= num ; i++)  {
    countup.push(i);
  }
  let factors = [];
  for (let i = 0; i < countup.length; i++)  {
    if (num % countup[i] == 0) {
    factors.push(countup[i]);
    }
  }
  let sum = factors.reduce((accumulator, currentValue) =>  {
    return accumulator += currentValue;
}
);
console.log(factors);  
return sum;
}

sumPrimes(977);

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36 Edg/88.0.705.63.

Challenge: Sum All Primes

Link to the challenge:

I think your code is approaching this wrong. You are getting the factors not the prime numbers.

For example for 10, you are supposed to get all the prime numbers less than or equal to 10, i.e., 2, 3, 5, and 7. Instead, you are finding the factors greater than 1, i.e., 2, 5, and 10. These coincidentally both sum to 17. But your return value is 977 because it’s only factor greater than 1 is 977. This is not the correct answer for the problem.

So, you double check the instructions:

Rewrite sumPrimes so it returns the sum of all prime numbers that are less than or equal to num.

1 Like

This function does not find factors of all composite numbers. 977 is not prime.

Regardless of what your function is doing, the important point is that it is not doing what the challenge asks.

function sumPrimes(num) {
  // Make a array of all numbers from 2 to num
  // (Note: this will be *very slow* and is not recommended)
  let countup = [];
  for (let i = 2; i <= num ; i++)  {
    countup.push(i);
  }
  // Find every number in 'countup' that divides into num
  let factors = [];
  for (let i = 0; i < countup.length; i++)  {
    if (num % countup[i] == 0) { // num % countup[i]  means num is a multiple of countup
      factors.push(countup[i]);
    }
  }
  // Sum the 'factors' array
  // (Note: making an array just for the purpose of summing that array is *very* inefficient and slow)
  let sum = factors.reduce((accumulator, currentValue) =>  {
    return accumulator += currentValue;
  });
  console.log(factors);  
  return sum; // Returning the sum of all of the numbers that divide evenly into num
}

sumPrimes(977);

This function finds all of the factors of the argument num that are greater than or equal to 2. This isn’t what the challenge wants you to do.

The challenge wants you to find the sum of all prime numbers that are less than or equal to the argument num. A prime number is a number greater than or equal to 2 that is only divisible by 1 and itself. You need to find every prime number in between 2 and num.


977 is prime by the way. https://en.wikipedia.org/wiki/List_of_prime_numbers#The_first_1000_prime_numbers. When in doubt, you can always Google “is 977 prime” or use the list I’ve linked to double check if any particular number is prime while you are debugging.

I put a prime test inside a loop for each number in the range (1, num). The result is a block of undefined elements. What problems are there?

function sumPrimes(num) {
  let primes = [];
  let range = [];
  for (let i = 2; i <= num; i++)  {
    range.push(i);
  for (let j = 2; j < (num); j++)  {
    if (range[i] % j != 0) {
      primes.push(range[i]);
    }}
  }
  return primes;
}

sumPrimes(7);

You’re indexing into an array with uninitialized entries. In JavaScript, when you index into an array at an index that was never set, the data is undefined.


Why are you making the range? Surely you know what the 5th number in between 2 and 977 is without storing that number somewhere?


Also, this will push a lot of stuff into the primes array. I do not think this works the way you think that it does.

@JeremyLT
The unindexed array pointer helped me understand that mistake.
I made a range because I did not know how to access a value without an array that could be stored in one. After looking at examples, I have a better idea of how to cycle through values with a test for desired ones.
I agree, there were mysterious values inside of the primes array.

This function has a prime check function below. The bottom function works. I am confused over the results of the top function.

function sumPrimes(num) {
  let countUp = 2;
  let sum = 0;
  while (countUp <= num)  {
  if (isPrime(countUp))  {
    console.log(countUp);
  sum += countUp;
  }
  countUp++;
  }
  return sum;
}
function isPrime(singleNumber)  {
  let countUp = 2;
  while (countUp < singleNumber)  {
    if (singleNumber % countUp) {
      return false;
    }
    countUp++;
  }
  return true;
}
sumPrimes(7);

What are you confused about on the first function?

I don’t think your second function works as you expect it to.

sumPrimes returns an unexpected value. console.log(countUp) returns 2 instead of 2, 3, 5, 7. I thought the return statement might have been made before incrementing, and I checked that.

Let me check the second function

Right, the problem is in your second function. As a hint, take a look at this:

function sumPrimes(num) {
  let countUp = 2;
  let sum = 0;
  while (countUp <= num) {
    console.log('sumPrimes countup at start of loop', countUp);
    if (isPrime(countUp)) {
      sum += countUp;
    }
    countUp++;
  }
  return sum;
}

function isPrime(singleNumber) {
  console.log('isPrime, checking', singleNumber);
  let countUp = 2;
  while (countUp < singleNumber) {
    console.log('isPrime countup at start of loop', countUp);
    if (singleNumber % countUp) {
      console.log(singleNumber, 'is not prime');
      return false;
    }
    countUp++;
  }
  console.log(singleNumber, 'is prime');
  return true;
}
console.log('final value', sumPrimes(10));

There is a problem with your logic. But you’re very close. I just had to make a change on one line to get it to pass the tests.

I used a test number in my head, then as an argument. @kevinSmith, the descriptive console statements are something I might use in the future in while loops. They helped me learn where to place a console log statement in a while loop to display all iterations

function sumPrimes(num) {
  let countUp = 2;
  let sum = 0;
  while (countUp <= num)  {
  if (isPrime(countUp))  {
    //console.log(countUp);
  sum += countUp;
  }
  countUp++;
  }
  return sum;
}
function isPrime(singleNumber)  {
  let countUp = 2;
  while (countUp < singleNumber)  {
    if (singleNumber % countUp == 0) {
      return false;
    }
    countUp++;
  }
  return true;
}
sumPrimes(7);

Thank you, everybody.

Congrats on getting it working! If you end up coming back to this challenge at some point, there are ways to make your isPrime function faster to make your code overall run faster.