gpt4 book ai didi

范围内的Javascript素数程序不显示结果

转载 作者:行者123 更新时间:2023-12-01 03:37:21 24 4
gpt4 key购买 nike

我的程序当前未在其指定的 HTML 元素中生成任何文本输出。它只说“黄金时间分配:”

var numOne = Number(prompt("Please enter the first whole number to begin the range you'd like to explore."));
var numTwo = Number(prompt("Enter the second whole number to conclude the range you'd like to explore. Hint: Second choice > first."));

if (numOne < numTwo && Number.isInteger(numOne) && Number.isInteger(numTwo)) {
for (; numOne <= numTwo; numOne++) {
for (i = 2; numOne <= i; i++) {
if (numOne % i == 0) {
break;
}

if (numOne == i) {
document.getElementById("primeTime").innerHTML += numOne + " is a prime number.";
}
}
}
} else {
window.alert("Please reload the webpage and try again. Error code: User error.");
}
<!-- Prime Time Assignment -->
<p id="primeTime"> Prime Time assignment: <br/></p>

最佳答案

您的内循环条件是错误的,您需要>=,并且您的if (numOne == i)位于错误的位置:

  var numOne = Number(prompt("Please enter the first whole number to begin the range you'd like to explore."));
var numTwo = Number(prompt("Enter the second whole number to conclude the range you'd like to explore. Hint: Second choice > first."));

if (numOne < numTwo && Number.isInteger(numOne) && Number.isInteger(numTwo)) {
for(; numOne <= numTwo; numOne++) {
for(var i = 2; numOne >= i; i++) {
if (numOne == i) {
document.getElementById("primeTime").innerHTML += numOne + " is a prime number.";
}

if (numOne % i == 0) {
break;
}
}
}
}
else {
window.alert("Please reload the webpage and try again. Error code: User error.");
}

关于范围内的Javascript素数程序不显示结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44158744/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com