【问题标题】:Calculating Invoice计算发票
【发布时间】:2011-01-21 07:53:27
【问题描述】:

我需要帮助计算:

我需要这样做:

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

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

有什么想法吗?

【问题讨论】:

  • "乘以 qty * rate 和输入的总和" - 有点模棱两可。
  • 除了简单的算术之外,我在这里看不到任何东西。你试过什么了?哪里出了问题?
  • SALES TAX (0.07) or (0.7%) 应该是(7.0%)?

标签: javascript invoice


【解决方案1】:

我想如果你想让某些东西可重复使用,它看起来应该是这样的:

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

【讨论】:

  • 嘿! thanx 响应...我想这就是代码,但是如何在我的输入变量中实现它?发布功能?
  • 这是个好问题。也许 Sheavi 会好心地将它实现为一个函数?
  • 你想让函数具体做什么?输入变量是如何输入/检索的?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多