// Turns a string equation into a value eg '2*2 + 4' will return '8'functioncalcString(input){constvalues=input.split("");constout=[];constops=[];constallOps=["*","/","+","-",")","("];letnum="";for(letindex=0;index<values.length;index++){constv=values[index];if(v===""){continue;}if(!allOps.includes(v)){while(!allOps.includes(values[index])&&values[index]!==""&&index<values.length){num+=values[index];index++;}index--;out.push(num);num="";continue;}if(v===")"){lettop=ops.pop();while(top!=="("&&ops.length){out.push(top);top=ops.pop();}if(top!=="("){return"Mismatched parens"}continue;}if(ops.length&&v!=="("){lettop=ops[ops.length-1];lettopP=getPrecedence(top);constcurrP=getPrecedence(v);while(currP>=topP&&top!=="("&&ops.length){out.push(top)ops.pop();top=ops[ops.length-1];topP=getPrecedence(top);}}ops.push(v);}while(ops.length){out.push(ops.pop());}conststack=[];out.forEach((o)=>{if(!allOps.includes(o)){stack.push(parseFloat(o,10));}else{constr=stack.pop();constl=stack.pop();switch(o){case"+":stack.push(l+r);break;case"-":stack.push(l-r);break;case"*":stack.push(l*r);break;case"/":stack.push(l/r);break;}}})return""+stack[0]}functiongetPrecedence(value){constprecedence=[["("],["*","/"],["+","-"]];returnprecedence.findIndex((p)=>{returnp.includes(value);})}
Here you go so you don't have to use eval.
Great. Thanks for the explanation!😄
Totally forgot to mention! Looks really great the design is super nice.
Thank you!😇