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

Coding for Kids

What is coding? In coding we build a program to do a specific task for us. Code: A code is a set of computer instructions and when you will ...