a function that determines whether a number is a prime number which has an even number of digits in javascript

index.tsx
function isPrime(num) {
  if(num <= 1) {
    return false;
  }
  for(let i = 2; i <= Math.sqrt(num); i++) {
    if(num % i === 0) {
      return false;
    }
  }
  return true;
}

function hasEvenDigits(num) {
  return num.toString().length % 2 === 0;
}

function isPrimeNumberWithEvenDigits(num) {
  return isPrime(num) && hasEvenDigits(num);
}
346 chars
20 lines

The isPrime() function takes a number as input and returns trueif the number is a prime number, otherwise it returns false. The function checks if the number is less than or equal to 1 (not a prime number by definition), and then checks if the number is divisible by any integer from 2 to the square root of the number. If it is, then it is not a prime number.

The hasEvenDigits() function takes a number as input and returns true if the number has an even number of digits, otherwise it returns false. The function checks the length of the string representation of the number, and returns true if the length is even.

The isPrimeNumberWithEvenDigits() function takes a number as input and returns true if the number is a prime number with an even number of digits, otherwise it returns false. The function first checks if the number is a prime number using the isPrime() function, and then checks if the number has an even number of digits using the hasEvenDigits() function. If both conditions are met, then the function returns true, otherwise it returns false.

gistlibby LogSnag