Skip to content Skip to sidebar Skip to footer

Prime Number Determination Javascript

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 displays a

Solution 1:

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

varUI = window.prompt("Enter a whole number to test as a prime number: \n", "0");
varTV = parseInt(UI, 10);
varHITS = 0;
varDD = 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");
}

Post a Comment for "Prime Number Determination Javascript"