If you look majority of calculator projects from others I think 2/3 of them would have hard-coded buttons
The easiest way to calculate a string expression is to evaluate it, and strictly speaking, there is absolutely nothing wrong about that in client-only systems like this. In fact I would actually suggest you to refactor it using eval()
:
const expression = '7 * 6';
const result = eval(expression);
console.log(result); // 42
If you still want to try non-eval solution, then you really shouldnât come up with your custom solution, parsing and calculating a string is common algorithm group, like breadth- / depth-first search or sorting algorithms etc., meaning you are expected to know (already invented) algorithm, rather then come up with something custom. If youâre interested in this topic, look at Postfix (aka Reversed Polish notation):
And, regarding selecting elements by id for styling - donât do it. Always prefer classes