0 votes
in javascript by
<script>
function isPrime(val){

  //test if number is prime
  for(var i=2; i < val; i++){
    if(val % i === 0){
      return false;
    }
  }
  return true;
}
function sumPrimes(num) {
  var answer = 0;

  //loop through all numbers from 2 to input value

  for(var i=2; i <= num; i++){   

    //sum only prime numbers, skip all others
    if(isPrime(i)){
      answer += i;
    }
  }
  return answer;
}

var sum = sumPrimes(977); // 73156
alert(sum);
</script>

Please log in or register to answer this question.

Welcome to My QtoA, where you can ask questions and receive answers from other members of the community.
...