# Calculation problem

hello all i have a issue here

if i do this

``````<script>
var x = ( 150 + 150 * .95 ) ;
document.getElementById("win").innerHTML = x;
</script>
``````

i get this 292.5

but if i do this

``````<script>
var x = ( 300 * .95 ) ;
document.getElementById("win").innerHTML = x;
</script>
``````

i get this 285 which is what am looking for but i need the script to look like the first one, what am i doing wrong here

Maybe try the following:

``````var x = (150 + 150) * .95  ;
``````
2 Likes

3 Likes

My guess is, that without the brackets, itâ€™s effectively doing this

``````var result = 150 + (150 * .95);
``````
3 Likes

Andres is 100% correct. You need the brackets.

The reason is that multiplication will be completed before addition. (This is simple maths as well as programming.)

1 Like

yeah the bracket stuff seem to do d trick, thanks but

300 -5% = 285

150 + 150 -5% = 292.5

which is correct here

1 Like

How are you performing the â€ś-5%â€ť part of it? If youâ€™re doing by multiplying by 0.95, then the same applies - the multiplication happens before the addition.

As for â€śwhich is correct hereâ€ť, there isnâ€™t really an answer because youâ€™re not defining the question sufficiently. Even in normal maths youâ€™d add brackets to remove ambiguity.

2 Likes

yeah seem when its whole its takes 5% off once but when it scombination of two numbers it takes 5% from each thats why am getting different answers

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.