# Can variables add the subtotals of a table?

Hello! I’m learning to use variables for some basic arithmetic. I made a cute little prototype where you can add or remove types of fruit to an order. A subtotal for each fruit is shown.

Currently I am using one mode for each fruit, though I think this is the wrong approach, not only because I have no way to add all the subtotals together for the order total.

How can I achieve this in the most elegant way with the least amount of variables?

I don’t know if this the most elegant method but I’d make two variables per fruits

• one for quantity,
• and one for price
(the one for price is more for comfort, so if you change the price, you’ll just have to change its value from `local variables` rather than having to go through every interaction to change it.

+ an additionnal variable for the Total.
Note that `#total` has to be calculated everytime you click on a button, at the last step of the interaction

i.e: when (+) for Apples is clicked you assign the new value to `#apple_quantity` and then add the price to total: `#total` = `#total` + `#apple_price`

Let me know if it helped

Thanks! If I am understanding correctly, this gets me to the subtotal, but not the entire total. I think I need three variables per fruit… for example:

#appleprice × #applequantity = #appletotal and then an #ordertotal for the entire order which would be the sum of #appletotal, #orangetotal, etc.

I thought I could apply the prototype interactions to the components, but I guess not

Unless there is a way to do it with fewer variables?

I think I need three variables per fruit…

Yeah 3 will do. Sorry, I forgot to take the subtotal into account

I thought I could apply the prototype interactions to the components, but I guess not

You can make interaction directly on the component. You’ll have to change the fruit variable for every instance tho but it’s definitely possible to do so

Unless there is a way to do it with fewer variables?

There maybe is but I’m not aware of it. While working with variables for a prototype I always tend to have as much variable as needed. If I want to change something later I want it to be easy and fast. I mean, I’m their to design not to develop a bulletproof solution.