Just use parseInt(tankX)
for every tank variable and it will work as expected.
This is because your values come from input fields as strings not integers.
Reference: parseInt
Question
So I am working on a formula with javascript. and this is the formula. The data for the tank variables is gathered from inputfields.
y = ((tank1 + tank2 + (tank 3 /25)) - (tank4 + tank4))/100;
alert(y);
so for tank1 = 100
tank2 = 100
and tank3 = 0
tank4 = 100
and tank5 =100
according to javascript the answer to this is 9009 while it is supposed to be 0
.
for tank1 = 90
tank2 = 90
tank3 = 0
tank4 = 90
and tank5 = 90
answer = 818.6
I tried changing the divisions to multiplications /25
to 0.04
and /100
to 0.01
but the results were the same.
I also tried renaming the tanks in case they were referring to the wrong tanks.
I have also tried alerting the tanks and they gave the right inserted numbers back.
I am running Jquery.
Does anyone Know what is causing this?
Solution
Just use parseInt(tankX)
for every tank variable and it will work as expected.
This is because your values come from input fields as strings not integers.
Reference: parseInt