Took 10 minutes and 16 seconds to code JavaScript prototype to check for prime numbers. Here is the code snippet.
Code (Run Code):
function checkPrime(num) {
if (num == 1) {
return num + ' is not a prime number and not a composite number';
} else if (num == 2) {
return num + ' is a prime number';
}
for (var x = 2; x < num; x++) {
if (num % x == 0) {
return num + ' is not a prime number';
}
}
return num + ' is a prime number';
}
function main(){
document.getElementById("result").innerHTML =
checkPrime(document.getElementById("num").value);
}
No comments:
Post a Comment