-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathequation.js
More file actions
65 lines (51 loc) · 2.25 KB
/
Copy pathequation.js
File metadata and controls
65 lines (51 loc) · 2.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
let reactants = prompt('Please enter the reactants (separated by spaces):');
let products = prompt('Please enter the products (separated by spaces):');
function getElements(input) {
const regex = /([A-Z][a-z]?\d*)/g;
return input.match(regex) || [];
}
function countElements(elements) {
const counts = {};
for (const element of elements) {
const count = element.match(/[A-Z][a-z]?/)[0];
const number = parseInt(element.match(/\d+/)?.[0] || '1', 10);
counts[count] = (counts[count] || 0) + number;
}
return counts;
}
function balenceEquation(reactants, products) {
const reactantElements = reactants.split(' ').flatMap(getElements);
const productElements = products.split(' ').flatMap(getElements);
const reactantCounts = countElements(reactantElements);
const productCounts = countElements(productElements);
const elements = new Set([...Object.keys(reactantCounts), ...Object.keys(productCounts)]);
const coefficients = {};
for (const element of elements) {
const reactantCount = reactantCounts[element] || 0;
const productCount = productCounts[element] || 0;
const lcm = findLCM(reactantCount, productCount);
coefficients[element] = { reactantCoeff: lcm / reactantCount, productCoeff: lcm / productCount };
}
const reactantCoeff = Math.max(...Object.values(coefficients).map(({ reactantCoeff }) => reactantCoeff));
const productCoeff = Math.max(...Object.values(coefficients).map(({ productCoeff }) => productCoeff));
console.log(`Balanced equation: ${formatEquation(reactants, reactantCoeff)} -> ${formatEquation(products, productCoeff)}`);
}
function findGCD(a, b) {
return b === 0 ? a : findGCD(b, a % b);
}
function findLCM(a, b) {
const gcd = findGCD(a, b);
return (a * b) / gcd;
}
function formatEquation(equation, coefficient) {
const components = equation.split(' ').map((component) => {
const elements = getElements(component);
return elements.map((element) => {
const [elem, number] = element.match(/([A-Z][a-z]*)(\d*)/).slice(1);
const count = number ? parseInt(number, 10) : 1;
return `${elem}${count}`;
}).join('');
});
return components.map(component => coefficient > 1 ? `${coefficient}(${component})` : component).join(' + ');
}
balenceEquation(reactants, products);