Java Script to enable calculations that also take into account precedence of * and/ and parentheses ()

Currently, the calculation is processed from left to right – meaning 2 + 3 * 2 = 10 and not 8.

Task is:
To enable calculations that also take into account precedence of * and/ and parentheses ().
we have to change the function that takes "*", "/" and () into account. For this, we need to change the function below which take two input (area and term) at the end, it returns the calculated operation. So we have to make the change to this code which takes into account precedence of * and/ and parentheses ()

// Terms have to be separated by full spaces. (E.g 50% + 2)
           function calculate(area, term) {
               var term, val, calc, add, sub, mul, div;
           term = term.split(" ");

               calc = 0;   // no current value
               add = sub = mul = div = false; // no operation
               int Length_ = term.length;

               for (var i = 0; i < Length_; i++) {

                   val = cm2Px(term[i]);

                   if (term[i].indexOf("%") !== -1) {
                       val = term[i].split("%")[0];
                       val = area / 100 * val;
                   }

                   if (term[i] === "HS") {
                       val = areaSpine / 2;    // already in cm, half the spine.
                   }

                   if (term[i] === "LHFS") {
                       val = (area / 2 - areaSpine / 2) / 2;
                   }

                   if (term[i] === "RHFS") {
                       val = area - (area / 2 - areaSpine / 2) / 2;
                   }


                   // First term
                   if (!isNaN(val) && add === false && sub === false && mul === false && div === false)
                       calc += val;

                   // Operation was requested in previous term
                   if (add)
                       calc += val; //cal = calc + val;

                   if (sub)
                       calc -= val; //cal = calc - val;

                   if (mul) {
                       if (term[i].slice(-1) === "d")
                           calc *= term[i].split("d")[0];  // use decimal number
                       else
                           calc *= val;    // use centimeter cal = calc * val;
                   }

                   if (div) {
                       if (term[i].slice(-1) === "d")
                           calc /= term[i].split("d")[0];  // use decimal number
                       else
                           calc /= val;    // use centimeter calc = calc / val;
                   }

                   // No operation for next term
                   if (add || sub || mul || div) // || or operator return the value true when one of the operator is true or both are true 
                       add = sub = mul = div = false;

                   // Operation for next term
                   if (term[i] === "+") {
                       add = true;
                   }
                   if (term[i] === "-") {
                       sub = true;
                   }
                   if (term[i] === "*") {
                       mul = true;
                   }
                   if (term[i] === "/") {
                       div = true;
                   }
               }

               return calc;
           }
`` 

1 thought on “Java Script to enable calculations that also take into account precedence of * and/ and parentheses ()”

Leave a Comment