2014-02-18 78 views
-2

当您运行在控制台这段代码,它工作正常,但不能以这种方式工作 - http://jsbin.com/raguquka/1/出了什么问题javascript代码

这适用于控制台 -

function solveEquation() { 
    // format ax+by=c 
    var eq1 = "3x+5y=4", // equation 1 
     eq2 = "5x+6y=2", // equation 2 

     // separate ax, by & c 
     eq1Parts = eq1.split(/[\+\-\=]/), 
     eq2Parts = eq2.split(/[\+\-\=]/), 

     // get the value of a, b & c 
     a1 = parseInt(eq1Parts[0]), 
     b1 = parseInt(eq1Parts[1]), 
     c1 = parseInt(eq1Parts[2]), 
     a2 = parseInt(eq2Parts[0]), 
     b2 = parseInt(eq2Parts[1]), 
     c2 = parseInt(eq2Parts[2]), 

     // substitution and elimination 
     // https://www.khanacademy.org/math/algebra/systems-of-eq-and-ineq/fast-systems-of-equations/e/systems_of_equations 

     B1 = b1 * a2, 
     C1 = c1 * a2, 
     B2 = b2 * a1, 
     C2 = c2 * a1, 
     // A1 and A2 cancels out, no need to specify them 

     // unknowns 
     y = (C1 - C2)/(B1 - B2), 
     x = (c1 - b1 * y)/a1; // putting the value of y in equation 1 

    ansContainer.textContent = "x = " + x + ", y = " + y; 
} 

我知道这是一个bug的代码,这是非常早期的阶段,我卡住了。

+1

ansContainer.textContent没有定义 – Tarek

+1

'ansContainer =的document.getElementById( “ANS-容器”)' – user3317779

+0

控制台告诉你的一切,看它的错误消息。 – epascarello

回答

1

你得到零除。

var eq1Container = document.getElementById("input-eq1"), 
    eq2Container = document.getElementById("input-eq1"), <-- references same input 
+0

哦,屎....哈哈哈我不能相信这一点。 这是一个愚蠢的错误...我已经吸了几个小时,但无法找到。 谢谢。 – user3317779

相关问题