Skip to content

Commit

Permalink
Merge pull request #66 from wendybarrios/sumOfPrimes.solution
Browse files Browse the repository at this point in the history
Added sum of primes in JavaScript
  • Loading branch information
ademclk authored Aug 26, 2022
2 parents 5a8482d + 895a760 commit 2f68404
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 2f68404

Please sign in to comment.