
How it works Data representationĪ raw chemical element is a string of letters that begins with an uppercase letter and is followed by any number of lowercase letters. In this case, each term that has a negative coefficient should be put on the other side of the equation, and its new coefficient should be the absolute value of the negative coefficient. Note: For simplicity of implementation, if the equation is successfully balanced but one or more terms have a negative coefficient, the program doesnât consider this outcome to be an error condition. This error should not happen, but if it does please contact me. The author/programmer made a serious logic mistake.

CHEMICAL EQUATION BALANCER UNIVERSITY OF TOLEDO CODE
There is no workaround the code would need to be rewritten to use bigints. I donât expect this error to occur for real-world chemical formulas, only deliberately contrived ones. Your equation used numbers that are too big, or a term has an element that occurs too many times, or the internal calculation used numbers that are too big. Furthermore, the equation can be separated as H â H 2 and O â O 2, each of which does have a unique solution. For example, H + O â H 2 + O 2 has no unique solution because two solutions are 2H + 4O â H 2 + 2O 2 and 6H + 2O â 3H 2 + O 2, which are not multiples of each other. Your equation can be considered as two or more independent equations added together. There exist multiple solutions to your equation that are not simply multiples of each other.

For example, C â N 2 has no solution because the only solution is 0C â 0N 2. The only mathematical solution to your equation has all coefficients set to zero, which is a trivial solution for every chemical equation. Check each letter carefully, and follow the examples as a guide to the correct syntax. Your input does not describe a proper chemical equation. Syntax guide Feature & demoįoo 5+ + Bar 3â â FooBar 2 + FooBar â The source TypeScript code and compiled JavaScript code are available for viewing. This program was hand-written in JavaScript in year 2011, received minor feature updates and clarifications and refactorings throughout the years, and was ported to TypeScript in 2018.

Because the program is entirely client-side JavaScript code, this web page can be saved and used offline. The algorithm used is Gauss-Jordan elimination, slightly modified to operate using only integer coefficients (not fractions). The program calculates the coefficients to balance your given chemical equation. This is an easy-to-use, no-nonsense chemical equation balancer. Chemical equation balancer (JavaScript) Program Input:
