# 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.

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
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] === "+") {
}
if (term[i] === "-") {
sub = true;
}
if (term[i] === "*") {
mul = true;
}
if (term[i] === "/") {
div = true;
}
}

return calc;
}
``
``````

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

1. Thank you for some other great post. The place else could anyone get that kind of information in such
an ideal approach of writing? I have a presentation subsequent week,
and I’m on the look for such information. https://herreramedical.org/hydroxychloroquine

2. Pingback: hydroxychloroquine
3. Hi, I do think this is an excellent web site. I stumbledupon it ðŸ˜‰ I’m going
to revisit yet again since I bookmarked it. Money and freedom is the greatest way to change, may you be rich and continue to
guide other people. http://antiibioticsland.com/Bactrim.htm

4. 773516 455450Discover how to deal with your domain get in touch with details and registration. Realize domain namelocking and Exclusive domain name Registration. 696300

5. 437291 871601But a smiling visitant here to share the adore (:, btw excellent style and style . 542361

6. female viagra pills online – viagra usa

7. who takes 50 mg cialis

8. generic cialis sale – tadalafil best online pharmacy

9. Cheap 100mg Cialis Pills

10. Levitra Bayer Prospecto

11. Cheapest Generic Viagra

12. We are a gaggle of volunteers and starting a new scheme in our community.
Your website offered us with useful information to work on. You’ve done an impressive task
and our whole group will probably be thankful to you. http://herreramedical.org/dapoxetine

14. Propecia Liver Side Effects

15. Cialis Dopo Quanto Fa Effetto

16. viagra without a doctor – realsilpll viagra online purchase india

17. Pingback: plaquenil mk
18. Cheap Generic Kamagra

19. Staph Aureus Keflex