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

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