开发者

Calculating Invoice

I need help with a calculation:

I need to do this:

Item ---- 开发者_StackOverflow中文版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?


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜