2

I need help with a calculation:

I need to do this:

Item ---- Qty ( 2  )  --- Rate ( $2  )   = Total (  $4  )
Item ---- Qty ( 3  )  --- Rate ( $3  )   = Total (  $9  )

SUBTOTAL  = $13
SALES TAX (0.07) or (0.7%) = $0.91
TOTAL = $13.91

in code.

my pseudocode is:

Multiply qty * rate and input in total

subtotal = sum of item totals

sales tax = 0.07 * subtotal 

total = sum of subtotal and sales tax

Any specific or pre-made code for the function I have just explained?

Any ideas?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Norman
  • 79
  • 1
  • 1
  • 7

1 Answers1

2

I guess if you want to make something re-usable it would look as such :

var items = []; // array that contains all your items

function Item(quantity, rate) { // Item constructor
    this.quantity = quantity;
    this.rate = rate;
    this.total = quantity * rate;
}

items.push(new Item(2, 4), new Item(3, 9)); // creates 2 items and add them to the array

// Processing through every items to get the total
var total = 0;
for (var i = 0; i < items.length - 1; i++) {
    total += items[i].total;
}

total += total * 0.07; // calculates taxes
Sheavi
  • 250
  • 1
  • 7
  • hey! thanx for responding... i guess thats the code, but how do I implement it in my input variables? post functions? – Norman Jan 21 '11 at 15:44
  • That is a good question. Perhaps Sheavi would be so kind as to implement it into a function? –  Jan 21 '11 at 18:38
  • What do you want the function to do exactly? How are the input variables entered / retrieved? – Sheavi Jan 22 '11 at 09:02