sum of prime numbers javascript

Solutions on MaxInterview for sum of prime numbers javascript by the best coders in the world

showing results for - "sum of prime numbers javascript"
Mia
16 Jul 2016
1function sumPrimes(num) {
2  let i = 1;
3  let sum = 0;
4  while (i <= num) {
5    if (isPrime(i)) {
6      sum += i;
7    }
8    i++;
9  }
10  return sum;
11}
12//function to check if a number is prime or not
13function isPrime(x) {
14  for (let i = 2; i < x; i++) {
15    if (x % i === 0) return false;
16  }
17  return x !== 1 && x !== 0;
18}
19//test here
20sumPrimes(10);
Lilly
24 Jan 2020
1function sumPrimes(num) {
2  //create an array with values from 1 to 10
3  var val =Array.from({length: num}, (_, i) => i + 1)
4  
5  val = val.filter((number)=> {
6    for(var i = 2; i < number; i++){
7      if(number % i === 0){
8        
9        return false
10      }
11    }
12    return number > 1    // [2, 3, 5, 7]
13  }).reduce(function(a, b){
14    return a + b   //sum = 17
15  })
16return val
17  }
18  
19
20console.log(sumPrimes(10));