var solveEquation = function(equation) { let a = 0, b = 0, c = 0, sign = 1, left = 1, hasC = false; for (let z of equation) { switch (z) { case'x': a += hasC ? sign * left * c : sign * left, c = 0, hasC = false; break; case'+': b += sign * left * c, c = 0, sign = 1, hasC = false; break; case'-': b += sign * left * c, c = 0, sign = -1, hasC = false; break; case'=': b += sign * left * c, c = 0, sign = 1, left = -1, hasC = false; break; default: c = c * 10 + (z - '0'), hasC = true; break; } } b += sign * left * c; return a === 0 ? b === 0 ? "Infinite solutions" : "No solution" : "x=" + -b / a; };