Skip to content

Commit

Permalink
Added sum of primes in JavaScript
Browse files Browse the repository at this point in the history
  • Loading branch information
wendybarrios committed Aug 26, 2022
1 parent b5a926d commit 895a760
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions challenges/sum_of_primes/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Sum of Primes

// Find the sum of primes below n.

sumOfPrimes = () => {
let num = parseInt(prompt('Enter a number'))
// create array
let newArr = []

// check if number is prime
function isPrime(num){
if (num === 2) {
return true;
}

for(let i = 2; i < num; i++ ) {
if( num % i === 0) return false;
}
return true;
}
// push prime numbers to array
for(let i = 2; i <= num; i++){
if(isPrime(i)){
newArr.push(i)
}
}
return newArr.reduce((a,b) => a + b, 0)

}

sumOfPrimes();

0 comments on commit 895a760

Please sign in to comment.