【问题标题】:Function not displaying correct results函数未显示正确结果
【发布时间】:2015-02-12 01:23:58
【问题描述】:

我已经编写了一个因子查找器,它应该首先检查输入框中输入的值是 1 还是 0,因此它会显示“1 的因子是 1”。或“0的因数是0。”,但是当我在输入框中输入数字0时,它会显示“0的因数是。”但我不知道为什么。这可能是一个我无法弄清楚的简单错误。这是我的代码。 HTML:

<!DOCTYPE HTML>

<html lang="en">
    <head>
        <title>JavaScript</title>
        <script type="text/javascript" src="javaScript/prime.js"></script>
    </head>
    <body>
        <!--Number-->
        <h1>Factor finder</h1>
        <p>Enter a number to find the factors!</p>
        <form name="prime" id="here">
            Number:
            <input class ="enter2" type="text" name="primeinput" size ="20"/>
            <input type="button" name="addnumber" value="Go" onclick="findFactor();"/>
            <br />
            <h2>Results</h2>
        </form>
    </body>
</html>

JavaScript:

//counters
var numbernum = 0;
var numinput = 1;
//function
function findFactor(){
    var array = new Array();
    //get input
    thenumber=document.prime.primeinput.value;
    //special circumstances for 1 and 0
    if (thenumber == 1 || thenumber == 0){
        amount = "The factor of ";
        verb = " is ";
    } else{
        amount = "The factors of ";
        verb = " are ";
    }
    //factor finder
    for (i=1; i<thenumber + 1; i++){
        //check to see if the number is a factor
        if(thenumber % i == 0){
            //check if the number is 1 or 0 to state the factors are 1 or 0
            //not working with 0             
            if(thenumber == 1 || thenumber == 0){
                if (thenumber == 1){
                    array[0] = 1;
                } else if (thenumber == 0){
                    array[0] = 0;
                }
            //if the number isn't 0 or 1
            } else if(thenumber != 1 && thenumber != 0){
                if (thenumber == numinput){
                    array[numbernum] = "and " + numinput;
                } else{
                    array[numbernum] = numinput;
                    numbernum ++
            }

        }

    }
    numinput ++
}
//append to the HTML
var make = document.createElement("p");
var apply = document.createTextNode(amount + thenumber + verb + array.join(", ") + ".");
make.appendChild(apply);
document.getElementById("here").appendChild(make);
//reset counters and clear array
numbernum = 0;
numinput = 1;
var array = 0;
}

【问题讨论】:

  • 如果thenumber0,则循环立即终止,因为1 &lt; thenumber + 1 为假。
  • 顺便说一句,您不需要else if(thenumber != 1 &amp;&amp; thenumber != 0),只需使用else。这与if 条件正好相反,else 只有在if 为假时才会执行。

标签: javascript html arrays primes factors


【解决方案1】:

01 的特殊情况下,不要打扰for 循环,因为您知道正确的结果应该是什么。所以把if放在for之外:

if (thenumber < 2) {
    array[0] = thenumber;
} else {
    for (var i = 1; i <= thenumber; i++) {
        if (thenumber % i == 0) {
            array.push(i);
        }
    }
}

【讨论】:

    【解决方案2】:

    for 不会因此而循环:

    for (i=1; i 0 + 1

    尝试将其更改为

    for (i=1; i

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-11-14
      • 2015-04-21
      • 1970-01-01
      • 1970-01-01
      • 2022-05-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多