function sumPrimes(num) {
var primeArr = [];
// array to store prime numbers
var numArr = [];
// array to store all numbers up to prime number
for (var i = 2; i <= num; ++i) {
// SO Sieve of Eratosthenes implementation
if (!numArr[i]) {
// i has not been marked -- it is prime
primeArr.push(i);
for (var x = i << 1; x <= num; x += i) {
numArr[x] = true;
}
}
}
var primeSum = primeArr.reduce(function(total,currentVal) {
// sum all prime numbers together & store result
return total + currentVal;
});
return primeSum;
// return result
}
sumPrimes(10);

i looked at the forum help for this as iâ€™ve been struggling with it for some time. iâ€™ll admit math isnâ€™t really my strong suit.

Anyway, iâ€™ll be 100% honest, i implemented someones elses alg for finding primes.

I know the general rule is â€śnot to copy others codeâ€ť, but the forum help article by Rafase282 seems to suggest that itâ€™s ok in this instance?

Can anyone confirm that this is definitely the case?

By â€śdonâ€™t copyâ€ť they mean to not just blindly copy without understanding or attempting to work through it on your own. Otherwise, looking up othersâ€™ algorithms and implementing them is at least half of what programming is about.

Thatâ€™s cool man, i always strive to do that & to document my solutions so otherâ€™s could understand it aswell. Infact iâ€™ve only ever done what i mentioned for this challenge alone.

I know itâ€™s normal behaviour in the wider scope of programming in general, but i just wanted to clarify in terms of the course. Thanks for the reply!

Because in my head it means â€śIf NOT sieve at index i, then carry on the loopâ€ť.
However sieve has no numbers in it yet? Im a bit confuzzledâ€¦ Can someone please help?

!sieve[i] is checked whether itâ€™s truthy or falsy inside the if-condition (it doesnâ€™t matter if itâ€™s boolean or not; what matters is its â€śtruthinessâ€ť or â€śfalsinessâ€ť). The ! coerces sieve[i] into a boolean value. If sieve[i] has no value, itâ€™s undefined (which is falsy), then because of the !, the entire thing is true.