JavaScript prototype to find opposite primes

Opposite Primes are prime numbers that have the same digits but in reverse order.

Here is the JavaScript prototype to find opposite primes up to a maximum limit of 100. Here is the code snippet.


Code (
Run Code):

function checkPrime(num) {
if (num == 1) {
return false;
} else if (num == 2) {
return true;
}

for (var x = 2; x < num; x++) {
if (num % x == 0) {
return false;
}
}
return true;
}

function primeNumbers(num) {
var arr = [];
for (var j = 2; j < num; j++) {
if (checkPrime(j)) {
arr.push(j);
}
}
return arr;
}

function oppositePrimes() {
var num = Number(document.getElementById('num').value);
var arr = primeNumbers(num);
var text = '';
text += 'Prime numbers till ' + num + ' are: ';
text += '<br>' + arr;
text += '<br> Opposite Primes:'
for (var i = 0; i < arr.length; i++) {
for (var x = i + 1; x < arr.length; x++) {
if (arr[i].toString()[0] ===
arr[x].toString()[1] && arr[i].toString()[1] === arr[x].toString()[0]) {
text += '<br>' + arr[i] + ' and ' + arr[x];
}
}
}
document.getElementById('result').innerHTML = text;
}

No comments:

Post a Comment

GITEX Dubai Talk by Saion Gupta and Saiasmi Gupta

Good morning, everyone. I'm Saion Gupta, the Founder of 10xCoderKids and the Guinness World Record holder for the youngest computer prog...