Millenial2020
Millenial2020

Reputation: 2923

Math Formula for updating values dynamically

I have a problem that I want to calculate the food calories depending on the size. I would explain.

Users would be able to enter food calories. for example

lets say he enters the sugar

he saids 1/2 sugar = 50 calories.

I want to be able to update teh sugar if the user updates that food for example

if another user goes to that item and change the quantity to 1 sugar it should automatically update to 100 calories.

So far i have

const calories = (quantity / DatabaseCalories) * DatabaseCalories ;

thats not working.

Upvotes: 0

Views: 50

Answers (1)

Berg_Durden
Berg_Durden

Reputation: 1781

If I understood it seems to be very simple. All you need to do is to store the base value, divide it by the new input and multiply the result by the base calories.

For example:

baseValue = 0.5
baseCalories = 50
newInput = 1

newValue = (newInput / baseValue) * baseCalories
newValue = (1 / 0.5) * 50
newValue = 100 

Upvotes: 1

Related Questions