简体   繁体   English

JavaScript for Loop使浏览器崩溃

[英]JavaScript for loop crashes browser

This loop crashes the browser when run, but I can't see why - function getN is one function of three designed to factorise quadratic equations. 该循环在运行时使浏览器崩溃,但我不明白为什么-函数getN是旨在分解二次方程式的三个函数之一。 I am sure it is the loop causing the problem, I have tested this and the browser only crashes when the for loop is present. 我确定这是导致问题的循环,我已经对此进行了测试,并且仅当存在for循环时浏览器才会崩溃。 Any help will be appreciated. 任何帮助将不胜感激。

function getN(decP){
var a = document.getElementById("a-f").value;
var b = document.getElementById("b-f").value;
var c = document.getElementById("c-f").value;
var n_1 =0;
var n_2 =0;
var result = Math.pow(10, (decP*-1));
var a_c = a*c;
var neg_a_c = 0;
var pos_a_c = 0;
if(a_c<0){
    neg_a_c = a_c;
    pos_a_c = a_c*-1
}
else{
    pos_a_c = a_c;
    neg_a_c = a_c*-1;
}
for(x=neg_a_c;x<=pos_a_c;x+result){
    if(x!==0){
        if(x+(a_c/x)===b){
            var num1 = x;
            var num2 = a_c/x;
        }
    }
}
divideByCoefficient(num1, num2)
};
for(x=neg_a_c;x<=pos_a_c;x+result){
                          ^
                           `-----here

You likely meant x += result (or x = x + result ), not just x+result . 您可能意味着x += result (或x = x + result ),而不仅仅是x+result

x+result never modifies x . x+result永远不会修改x So x always equals neg_a_c and the loop runs forever. 因此, x始终等于neg_a_c ,循环永远运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM