Answers for "check primes in array js"

0

check primes in array js

function checkPrime(number) {
  if (number <= 1) {
    return false;
  } else {
    for (let i = 2; i < number; i++) {
      if (number % i == 0) {
        return false;
      }
    }
    return true;
}

arr.forEach(function (element) {
  const isPrime = checkPrime(element);
  if (isPrime) {
    console.log(`${element} is a prime number`);
  } else {
    console.log(`${element} is NOT a prime number`);
  }
});
Posted by: Guest on October-23-2021

Code answers related to "Javascript"

Browse Popular Code Answers by Language