Non-composite numbers sum in an array in JavaScript

In JavaScript, you can calculate the sum of all prime numbers in an array by first checking each number for primality, then adding the prime numbers together. A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself.

Understanding Prime Numbers

Prime numbers are numbers like 2, 3, 5, 7, 11, 13, etc. The number 1 is not considered prime, and 2 is the only even prime number.

Creating a Prime Check Function

First, we need a helper function to determine if a number is prime:

const isPrime = n => {
    if (n === 1) {
        return false;
    } else if (n === 2) {
        return true;
    } else {
        for (let x = 2; x < n; x++) {
            if (n % x === 0) {
                return false;
            }
        }
        return true;
    }
};

// Test the function
console.log(isPrime(2));   // true
console.log(isPrime(5));   // true
console.log(isPrime(4));   // false
console.log(isPrime(1));   // false
true
true
false
false

Calculating Prime Sum

Now we can create a function that sums all prime numbers in an array:

const isPrime = n => {
    if (n === 1) {
        return false;
    } else if (n === 2) {
        return true;
    } else {
        for (let x = 2; x < n; x++) {
            if (n % x === 0) {
                return false;
            }
        }
        return true;
    }
};

const primeSum = arr => {
    let sum = 0;
    for (let i = 0; i < arr.length; i++) {
        if (!isPrime(arr[i])) {
            continue;
        }
        sum += arr[i];
    }
    return sum;
};

const arr = [43, 6, 6, 5, 54, 81, 71, 56, 8, 877, 4, 4];
console.log(primeSum(arr));
996

Optimized Version

Here's a more efficient version using array methods and an optimized prime check:

const isPrimeOptimized = n => {
    if (n <= 1) return false;
    if (n <= 3) return true;
    if (n % 2 === 0 || n % 3 === 0) return false;
    
    for (let i = 5; i * i <= n; i += 6) {
        if (n % i === 0 || n % (i + 2) === 0) return false;
    }
    return true;
};

const primeSumOptimized = arr => {
    return arr
        .filter(isPrimeOptimized)
        .reduce((sum, num) => sum + num, 0);
};

const numbers = [43, 6, 6, 5, 54, 81, 71, 56, 8, 877, 4, 4];
console.log(primeSumOptimized(numbers));
996

Breaking Down the Process

Let's see which numbers from our array are prime:

const isPrime = n => {
    if (n <= 1) return false;
    if (n === 2) return true;
    if (n % 2 === 0) return false;
    
    for (let i = 3; i <= Math.sqrt(n); i += 2) {
        if (n % i === 0) return false;
    }
    return true;
};

const arr = [43, 6, 6, 5, 54, 81, 71, 56, 8, 877, 4, 4];
const primes = arr.filter(isPrime);

console.log("Prime numbers found:", primes);
console.log("Sum:", primes.reduce((sum, num) => sum + num, 0));
Prime numbers found: [ 43, 5, 71, 877 ]
Sum: 996

Conclusion

To sum prime numbers in an array, create a prime-checking function and iterate through the array, adding only the prime numbers. The optimized version uses mathematical properties to check primality more efficiently by only testing divisors up to the square root of the number.

Updated on: 2026-03-15T23:19:00+05:30

207 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements