当您在控制台中运行此代码时,它可以正常工作但不能以这种方式工作 - http://jsbin.com/raguquka/1/
这适用于控制台 -
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;
}
我知道这是一个错误的代码,这是非常早期的阶段,我卡住了。
答案 0 :(得分:1)
你得到零除。
var eq1Container = document.getElementById("input-eq1"),
eq2Container = document.getElementById("input-eq1"), <-- references same input