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
Further reading …
Order of Operations - BODMAS
coothead
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
system
Closed
November 16, 2017, 3:45am
9
This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.