-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
executable file
·87 lines (75 loc) · 2.74 KB
/
main.js
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import {BalanceMolecularEqn, uString} from "./src/balance.js";
const randomEqn = [
"Na2SO4 + H2SO4 = NaHSO4",
"H2O2 = H2O + O2",
"CH4 + O2 = CO2 + H2O",
"C3H8 + O2 = CO2 + H2O",
"H3PO3 = H3PO4 + PH3",
"Fe2(SO4)3 + NH3 + H2O = Fe(OH)3 + (NH4)2SO4",
"Ca + H2O = Ca(OH)2 + H2",
"Na + Cl2 = NaCl",
"H2 + O2 = H2O",
"NaNO3 = NaNO2 + O2",
"Cu + HNO3 = Cu(NO3)2 + H2O + NO",
"Fe2SiO4 + Mg2SiO4 + H2O + CO2 = Mg6(Si4O10)(OH)8 + Fe2O3 + CH4"
]
const $ = selector => document.querySelector(selector);
/**
* Tranform the input text to a formatted version on the display much more understandable by the reader
* @param txt is the input text
* @param outputEl is the output element
*/
const renderFormatedEqn = (txt, outputEl, subNumber = true) => {
outputEl.innerHTML = "";
let str = ""
for(let ch of txt) {
if(subNumber && uString.digits.includes(ch))
str += "<sub>" + ch + "</sub>";
else str += ch;
}
outputEl.innerHTML = str;
const snackbarEl = $("#snackbar");
snackbarEl.classList.remove("active");
}
/**
* This is the handler that perform the balacing procedures
* @param inputEl is the input element
*/
const onBalanceHandler = (inputEl) => {
const txt = inputEl.value;
const res = BalanceMolecularEqn(txt);
const snackbarEl = $("#snackbar");
if(res.error) {
snackbarEl.classList.add("active");
snackbarEl.innerHTML = res.error;
return;
}
let outputTxt = $("#output").innerHTML.replaceAll(" ", "").split("=").map(i => i.split("+")).flat();
let strRes = "";
for(let i = 0; i < outputTxt.length; i++) {
const v = Math.abs(res.coeff[i]);
const coeff = v !== 1 ? v : "";
strRes += `<strong style="color: red">${coeff}</strong>${outputTxt[i]}`;
if(i === res.reactants.length - 1) strRes += " = ";
else {
if(i < outputTxt.length - 1) strRes += " + ";
}
}
$("#result").innerHTML = strRes;
}
const onRandomHandler = (inputEl, outputEl) => {
const r = Math.floor(Math.random() * randomEqn.length);
inputEl.value = randomEqn[r];
renderFormatedEqn(inputEl.value, outputEl);
}
const main = () => {
const inputEl = $("#input");
const outputEl = $("#output");
const balanceBtn = $("#balanceBtn");
const randomBtn = $("#randomBtn");
renderFormatedEqn(inputEl.value, outputEl);
inputEl.addEventListener("input", () => renderFormatedEqn(inputEl.value, outputEl));
balanceBtn.addEventListener("click", () => onBalanceHandler(inputEl));
randomBtn.addEventListener("click", () => onRandomHandler(inputEl, outputEl));
}
addEventListener("load", main);