为什么我的 javascript for 循环没有执行任何操作,而是冻结了我的网页?

我无法弄清楚为什么我的代码不起作用。这是一个非常简单的程序,所以我确信我缺少或没有看到一些东西,但我似乎无法弄清楚。请帮忙!


它应该做什么:接受两个数字并找到下一个大于第一个数字并且也能被第二个数字整除的数字。


发生了什么:它基本上只是冻结了我的计算机,似乎循环继续运行而不停止。


JS:


function divisibleBy(first, second) {

    var firstNum = document.getElementById('firstNumber').value;

    var secondNum = document.getElementById('secondNumber').value;


    for (var i = firstNum + 1; i > firstNum; i++) {

        if (i % secondNum === 0) {

            document.getElementById('result').innerHTML = i;

        }

    }

}

HTML:


<body>

    <header>

        <h1>Let's Do Some Math!</h1>

        <h2>Type in <u>two</u> numbers and let's figure out the next <br>

            number greater than those two numbers but with a catch...<br>

            the number has to be <u>divisible</u> by the second number you choose.</h2>

    </header>

    <hr>

    <ul>

        <li>

            <label for="firstNumber">First Number:</label>

            <input type="number" id="firstNumber">

        </li>

        <li>

            <label for="secondNumber">Second Number:</label>

            <input type="number" id="secondNumber">

        </li>

        <li>

            <button type="submit" onclick="divisibleBy();">GO</button>

        </li>

    </ul>


    <h2 id="result"></h2>

    <hr>

</body>


噜噜哒
浏览 94回答 2
2回答

皈依舞

问题:问题是你的for-loop的条件语句是always ,这会导致无限循环,因此页面无响应。在这种情况下也不需要为函数提供参数true解决方案:这个问题可以用数学方法解决,无需循环。// BEST APPROCHfunction divisibleBy() {&nbsp; &nbsp; var firstNum = parseInt(document.getElementById('firstNumber').value);&nbsp; &nbsp; var secondNum = parseInt(document.getElementById('secondNumber').value);&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; var result = secondNum*parseInt(firstNum/secondNum+1);&nbsp; &nbsp; document.getElementById('result').innerHTML = result;&nbsp; &nbsp;&nbsp;}但是,如果您确实需要使用循环,这里是解决方案,但请记住,在for 循环中设置限制几乎与上述解决方案相同(或将条件设置为i <= firstNum*secondNum,因为它在遇到解决方案时已经有了break语句)// WORST APPROCHfunction divisibleBy() {&nbsp; &nbsp; var firstNum = parseInt(document.getElementById('firstNumber').value);&nbsp; &nbsp; var secondNum = parseInt(document.getElementById('secondNumber').value);&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; for (var i = firstNum+1; i <= secondNum*(firstNum/secondNum+1); i++) {&nbsp; &nbsp; &nbsp; &nbsp; if (i % secondNum === 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; document.getElementById('result').innerHTML = i;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}但我建议使用循环,这将更令人满意地解决这个问题,并忽略对上述代码中的do-While限制进行不必要的计算。i// BEST LOOP/SIMPLE APPROCH&nbsp;function divisibleBy() {&nbsp; &nbsp; var firstNum = parseInt(document.getElementById('firstNumber').value);&nbsp; &nbsp; var secondNum = parseInt(document.getElementById('secondNumber').value);&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; do{&nbsp; &nbsp; &nbsp; &nbsp; firstNum++;&nbsp; &nbsp; }&nbsp; &nbsp; while(firstNum%secondNum != 0)&nbsp; &nbsp; document.getElementById('result').innerHTML = firstNum;}

万千封印

你正在使用 for (var i = firstNum + 1; i > firstNum; i++)并且这个条件总是满足。因此它会无限循环。尝试这个 :&nbsp;for (var i = firstNum + 1; ; i++) {&nbsp; &nbsp; &nbsp; &nbsp; if (i % secondNum === 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; document.getElementById('result').innerHTML = I;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript