I am creating a external javascript file. This is for homework. What I am supposed to do is determine if the number that the user enters in is a prime number or not, and dis
I think you should set the var DD = TV;
after the TV = parseInt(UI, 10)
.
And you should decrement DD in the while loop, if you don't want it to be infinite.
Here is the code corrected
var UI = window.prompt("Enter a whole number to test as a prime number: \n", "0");
var TV = parseInt(UI, 10);
var HITS = 0;
var DD = TV;
while (DD > 0) {
if (TV % DD === 0) {
HITS++;
}
DD--;
}
if (HITS > 2) {
document.write(UI + " is a NOT prime number");
} else {
document.write(UI + " is a prime number");
}