Is the reversed number a prime number in JavaScript

JavascriptWeb DevelopmentFront End Technology

Problem

We are required to write a JavaScript function that takes in a number and return true if the reverse of that number is a prime number, false otherwise.

Example

Following is the code −

 Live Demo

const num = 13;
const findReverse = (num) => {
   return +num
   .toString()
   .split('')
   .reverse()
   .join('');
};
const isPrime = (num) => {
   let sqrtnum = Math.floor(Math.sqrt(num));
   let prime = num !== 1;
   for(let i = 2; i < sqrtnum + 1; i++){
      if(num % i === 0){
         prime = false;
         break;
      };
   };
   return prime;
}
const isReversePrime = num => isPrime(findReverse(num));
console.log(isReversePrime(num));

Output

true
raja
Published on 19-Apr-2021 10:52:08
Advertisements