JSFiddle - React, Tailwind, and code Playground
HTML
<input id="textbx" type="text" /><button id="btn" disabled="disabled">go</button>
<span id="msg"></span>
JavaScript
$("#textbx").keyup(function(){
if ($("#textbx").val().length > 0) {
$("#btn").removeAttr('disabled');
}
});
$("#btn").click(function(){
var num =parseInt($("#textbx").val());
if(isPrime(num))
{
$("#msg").html("yupp").show().fadeOut("slow");
}
else
{
$("#msg").html("huh").show().fadeOut("slow");
}
});
function isPrime(n) {
// If n is less than 2 or not an integer then by definition cannot be prime.
if (n < 2) {return false}
if (n != Math.round(n)) {return false}
// Now assume that n is prime, we will try to prove that it is not.
var isPrime = true;
// Now check every whole number from 2 to the square root of n. If any of these divides n exactly, n cannot be prime.
for (var i = 2; i <= Math.sqrt(n); i++) {
if (n % i == 0) {isPrime = false}
}
// Finally return whether n is prime or not.
return isPrime;
}