Prime Number Determination Javascript

后端 未结 1 955
伪装坚强ぢ
伪装坚强ぢ 2020-12-12 01:06

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

相关标签:
1条回答
  • 2020-12-12 01:32

    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");
    }
    
    0 讨论(0)
提交回复
热议问题