Sum All Primes - JavaScript Solution and Walkthrough
(13/21) Learn how to solve coding challenges using FreeCodeCamp's curriculum.
13/21 Sum All Primes
A prime number is a whole number greater than 1 with exactly two divisors: 1 and itself. For example, 2 is a prime number because it is only divisible by 1 and 2. In contrast, 4 is not prime since it is divisible by 1, 2 and 4.
Rewrite
sumPrimes
so it returns the sum of all prime numbers that are less than or equal tonum
.
function sumPrimes(num) {
return num;
}
sumPrimes(10);
Credit: FreeCodeCamp.org
Understanding the Challenge
In this challenge, you are expected to complete the sumPrimes()
function.
This function takes a number num
as argument. And it should return the sum of all primes numbers that are less than or equal to num
.
As explained in the challenge description, "a prime number is a whole number greater than 1 with exactly two divisors: 1 and itself."
Let's do some pseudocoding before we proceed.
Pseudocode
Given a number (num)
create a variable sum which will hold final answer
Loop through numbers less than or equal num
For each iteration, check if number is a prime number
if it is, add the number to num
Return the value of sum after the loop is done.
Solving the Challenge
For this challenge, we'll create another function within the sumPrimes()
function (a helper function). The purpose of this function would be to check whether a number is prime or not. Let's call it checkPrime()
.
To create the checkPrime()
function, we need a loop. This loop will start from 2 i = 2
, and end at the square root of num i <= Math.sqrt(num)
We start from i < 2
because we know 1 is not a prime number. Therefore, there's no need for the checkPrime()
to deal with 1. Also, we end at i <= Math.sqrt(num)
because the square root of a number is the highest possible divisor that exist. Therefore, that there's no need for our function to go beyond that.
function sumPrimes(num) {
function checkPrime(num) {
for (let i = 2; i <= Math.sqrt(num); i++) {
if (num % i == 0)
return false;
}
return true;
}
}
}
For every iteration of the loop, we check if the given number num
modulo i
is equal to zero. If it is equal to zero, then it means that number is a divisor of num
. This means num
is not a prime number because it has more more than 2 divisors so the checkPrime()
function returns false
.
However, if after the loop, there was no case of num
modulo i
equalling zero, then it means num
has only two divisors (1 and num
itself). In that case, checkPrime()
returns true
.
Now that we have a function to help us check whether or a number is a prime number, we can use that function to find all prime numbers less than or equal to num
We would declare a variable sum
. For any number less than or equal to num
that returns true
for the checkPrime()
function, we would add it to sum
let sum = 0;
for (let i = 2; i <= num; i++) {
if (checkPrime(i)) {
sum += i;
}
}
`
Finally, we return sum
. Our function is complete!
return sum;
Final Solution
function sumPrimes(num) {
function checkPrime(num) {
for (let i = 2; i <= Math.sqrt(num); i++) {
if (num % i == 0)
return false;
}
return true;
}
let sum = 0;
for (let i = 2; i <= num; i++) {
if (checkPrime(i)) {
sum += i;
}
}
return sum;
}
sumPrimes(10) // 17;
Congratulations!
You just cracked the 13th challenge in this series.
Cheers and happy coding!