if javascript中的语句

时间:2015-10-27 18:10:44

标签: javascript

这是我的javascript类介绍中的项目。以下是说明:

建筑师的费用按建筑物成本的百分比计算。费用如下: 建筑物成本的前5,000美元的8%。 如果剩余部分大于零但小于或等于80,000.00美元,则增加剩余3%,如果剩余部分超过80,000.00美元,则增加剩余部分的2.5%。

所以我用这段代码启动了程序,但是我该如何完成呢?

var totalCost;
var architectFee;
var architectPay;

//prompt user to enter total cost of building
totalCost = prompt("What is the total cost of the building?");

//Output architect pay
if (totalCost <= 5000) {
  architectFee = 0.08
  architectPay = totalCost * architectFee;
  document.write("For a building that will cost $" + totalCost + "," + "the architect's pay will be $" + architectPay);
}

1 个答案:

答案 0 :(得分:0)

如果totalCost等于或小于5000,或者如果它大于5000,则可以单向计算成本。如果它小于5000,则只需要一次计算。如果它大于5000,则需要取余数,并根据它是大于还是小于80000,对剩余部分应用不同的百分比。这是一个如何工作的粗略布局。

var totalCost;
var architectFee;
var architectPay;

//prompt user to enter total cost of building
totalCost = prompt("What is the total cost of the building?");

//Output architect pay
if (totalCost <= 5000) {
  architectFee = 0.08
  architectPay = totalCost * architectFee;
  document.write("For a building that will cost $" + totalCost + "," + "the architect's pay will be $" + architectPay);
}
else{
  var architectPay = 0.08 * 5000;
  var remainder = totalCost - 5000;
  if(remainder <= 80000){
    architectPay = architectPay + (remainder * 0.03);
  }
  else if(remainder > 80000){
    architectPay = architectPay + (remainder * 0.025);
  }
  document.write("For a building that will cost $" + totalCost + "," + "the architect's pay will be $" + architectPay);
}