JavaScript code to check for prime numbers

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

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...