Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

prime number in javascript using for loop

function sumPrimes(num) {
  // Check all numbers for primality
  let primes = [];
  for (let i = 2; i <= num; i++) {
    if (primes.every((prime) => i % prime !== 0))
      primes.push(i);
  }
  return primes.reduce((sum, prime) => sum + prime, 0);
}
Source by forum.freecodecamp.org #
 
PREVIOUS NEXT
Tagged: #prime #number #javascript #loop
ADD COMMENT
Topic
Name
8+2 =