帮助计算发票

时间:2011-01-21 07:53:28

标签: javascript javascript-events invoice

我需要帮助进行计算:

我需要这样做:

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

代码。

我的伪代码是:

Multiply qty * rate and input in total

subtotal = sum of item totals

sales tax = 0.07 * subtotal 

total = sum of subtotal and sales tax

我刚才解释的功能的任何特定或预制代码?

有什么想法吗?

1 个答案:

答案 0 :(得分:2)

我想如果你想让某些东西可以重复使用,它会是这样的:

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