These are chat archives for **camperbot/bonfire-sum-all-primes**

`hint [1/7]`

Make sure you've tried to hard to solve it yourself before proceeding. :construction:

type

`hint`

for next hint :pencil: [Contribute at the FCC Wiki]

`hint [2/7]`

The explanation for this problem is very simple. You will generate a list of prime numbers up to the number you are given as a parameter. Then you need to add them all up and return that value. The tricky part is on generating the list of prime numbers. I suggest you find a code or a good math algorithm that you can turn into code.

type

`hint`

for next hint :pencil: [Contribute at the FCC Wiki]

`hint [3/7]`

Generate a list of all the numbers up to and including the one you got as a parameter. This will be needed to determine which numbers are prime or not.

type

`hint`

for next hint :pencil: [Contribute at the FCC Wiki]

`hint [4/7]`

Check this link if you prefer to find a solution for finding primes, or try learning and implementing your own Sieve of Eratosthenes

type

`hint`

for next hint :pencil: [Contribute at the FCC Wiki]

`hint [5/7]`

This problem is hard if you have to create your own code to check for primes, so don't feel bad if you had to use someone's code for that bit. Either way, you are most likely using array, so once you generate an array of primes, then just add them all up and return the number you get.

type

`hint`

for next hint :pencil: [Contribute at the FCC Wiki]

`hint [6/7]`

**Solution ahead!**

type

`hint`

for next hint :pencil: [Contribute at the FCC Wiki]

`hint [7/7]`

```
function sumPrimes(num) {
var res = 0;
// FUnction to get the primes up to max in an array
function getPrimes(max) {
var sieve = [];
var i;
var j;
var primes = [];
for (i = 2; i <= max; ++i) {
if (!sieve[i]) {
// i has not been marked -- it is prime
primes.push(i);
for (j = i << 1; j <= max; j += i) {
sieve[j] = true;
}
}
}
return primes;
}
// Add the primes
var primes = getPrimes(num);
for (var p = 0; p < primes.length; p++) {
res += primes[p];
}
return res;
}
```

- Create a function that generates the numbers from 1 to
**num**and check if they are prime along the way. - Declare the variables that will be needed.
- Start with 2, if it has not been marked and added to the sieve array then it is a prime and we add it to the prime array.
- Add the others to the sieve array.
- Return the primes
- Loop through the returned array and add all the elements to then return the final value.

If you found this page useful, you can give thanks by copying and pasting this on the main chat: `thanks @Rafase282`

NOTE:Please add your username only if you have added anyrelevant main contentsto the wiki page. (Please don't remove any existing usernames.)type

`hint`

for next hint :pencil: [Contribute at the FCC Wiki]