JavaScript code to check for composite numbers

Took 5 minutes and 22 seconds to code JavaScript prototype to check for composite numbers. Here is the code snippet.

Code (Run Code):

function checkComposite(num) {
if (num == 1) {
return num + ' is not a prime number and not a composite number';
} else if (num == 2) {
return num + ' is not a composite number';
}

for (var x = 2; x < num; x++) {
if (num % x == 0) {
return num + ' is a composite number';
}
}
return num + ' is not a composite number';
}

function main(){
document.getElementById("result").innerHTML =
checkComposite(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 ...