2015-10-27 74 views
-2

這是我在介紹javascript類時的一個項目。以下是說明:if語句中的javascript

建築師的費用是以建築物成本的百分比計算的。費用組成如下: 建築成本的前5,000,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); 
} 
+3

你需要的是打開一些JavaScript的書,並開始學習:) – Romko

+0

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/round – epascarello

+0

開始通過檢查它是否超過$ 80k,elseif超過$ 5k,否則(它是<= 5k) – James

回答

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); 
}