Recently, I made a program that detects prime numbers and non-prime numbers!
It’s very complicated!
Here it is!
Javascript:
function isPrime() {
//all the variables
var num = prompt("What is the number you seek?");
var squarerooted = Math.sqrt(num); //defines the square root of num
var primeTrue = false; // the true/false determiner
var i = 2; //the first number to be divided into num
var count = 0; //how many numbers below the square root of num divide evenly into num
var Type = typeof num; //just a test
console.log(Type); //just a test
console.log(num);
var finalOutput = "";
//the test
while (i<=squarerooted){
if(num%i === 0){
count++;
}
i++;
}
//determining the state of primeTrue : true or false?
if(count === 0){primeTrue = true;}
else{primeTrue = false;}
//the final outputs
if (primeTrue === true){console.log("PRIME"); finalOutput = "Prime";}
else{console.log("NOT PRIME"); finalOutput = "Not prime";}
console.log(finalOutput); // just a test
document.getElementById("truefalse").innerHTML = finalOutput;
}
HTML:
<html>
<head>
<title>prime testing</title><style></style>
</head>
<body>
<script>alert("1 is not prime, 0 is not prime, strings are not supposed to be prime.");</script>
<button onclick="isPrime()">Try it</button>
<p id="truefalse"></p>
<script src="primetest.js"></script>
</body>
</html>
Copy and Paste these code pieces into two separate .txt files and save the Javascript as (your choice).js and the HTML as (your choice).html