0

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. 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. 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)
};
2
  • 6
    infinite loops tend to do that Commented Nov 7, 2014 at 18:26
  • 1
    x+result only evaluates the value, you are not storing it anywhere. Guess you want: x+=result Commented Nov 7, 2014 at 18:27

1 Answer 1

5
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 never modifies x. So x always equals neg_a_c and the loop runs forever.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.