Skip to content

Commit 7a1b86a

Browse files
committed
Intermediate Algorithm Scripting (ex. 13)
1 parent 700a8af commit 7a1b86a

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

09-intermediate-algorithm-scripting/13-sum-all-primes.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,24 @@ Rewrite sumPrimes so it returns the sum of all prime numbers that are less than
1010
- sumPrimes(10) should return 17.
1111
- sumPrimes(977) should return 73156.
1212
*/
13+
function sumPrimes(num) {
14+
let sum = 0;
15+
for (let i = 2; i <= num; i++) {
16+
if (isPrime(i)) {
17+
sum += i;
18+
}
19+
}
20+
return sum;
21+
}
22+
23+
function isPrime(num) {
24+
for (let i = 2; i < num; i++) {
25+
if (num % i === 0) {
26+
return false;
27+
}
28+
}
29+
return num > 1;
30+
}
31+
32+
console.log(sumPrimes(10));
33+
console.log(sumPrimes(977));

0 commit comments

Comments
 (0)