- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Is the reversed number a prime number in JavaScript
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 −
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
Advertisements
To Continue Learning Please Login
Login with Google