Prime Number Determination Javascript

六月ゝ 毕业季﹏ 提交于 2019-11-28 14:11:22

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");
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!