Quotes help make search much faster. Example: "Practice Makes Perfect"

Saturday, May 13, 2017

FreeCodeCamp Sum All Primes Sample Solution


function isPrime(value) {
  for(var i = 2; i < value; i++) {
    if(value % i === 0) {
      return false;
    }
  }
  return true;
}

function genPrimeArr(num){
  var arr = [];
  for(var i=2; i<=num; i++){
   
    if(isPrime(i)){
      arr.push(i);
    }
  }
  return arr;
}

function sumArr(arr){
  var sum = 0;
  for(var i=0; i<arr.length; i++){
    sum += arr[i];
  }
  return sum;
}

function sumPrimes(num) {
  return sumArr(genPrimeArr(num));
}

sumPrimes(10);

No comments:

Post a Comment


This is an example of scrolling text using Javascript.

Popular Posts