2011-04-24 82 views
1

我是JavaScript新手,我一整天都在做這個任務,而且我非常沮喪。我無法弄清楚我做錯了什麼,或者我可能錯過了哪些代碼。任何幫助非常感謝它!以下是我已經完成的任務和代碼。謝謝!!新來的JS和麻煩!

作業: 許多公司通常會爲購買收取運費和手續費。創建一個 網頁,允許用戶在文本框中輸入購買價格,幷包含一個用於計算運輸和處理的JavaScript函數 。爲腳本 添加功能,對於任何少於 的購買,將最低運費和手續費加在1.50美元上,而不超過或等於25美元。對於任何超過$ 25.00的訂單,請在總貨款 的基礎上增加10%的運費和手續費,但不包括最低運費$ 1.50和處理 的費用。計算百分比的公式爲價格*百分比/ 100。例如,計算$ 50.00購買價格的10%的 公式爲50 * 10/100,這導致 運費和手續費爲5.00美元。確定訂單 (購買加運輸和處理)的總成本後,將其顯示在警報對話框中。將文檔 保存爲CalcShipping.html。

書面

代碼:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<link rel="stylesheet" href="js_styles.css" type="text/css" /> 
<title>Calculating Shipping & Handling</title> 
<script type="text/javascript"> 
/* ![CDATA[ */ 

getShipping(); 
{ 
    if (price <= 25.00) 
    shipping = 1.50 
    else (price > 25.00) 
    shipping = (price * (10/100)); 
} 
/* ]]> */ 
</script> 
</head> 

<body> 

<script type="text/javascript"> 
/* ![CDATA[ */ 
document.write("<h1>Purchase Price with Shipping</h1>"); 
/* ]]> */ 
</script> 

<script type="text/javascript"> 
/* <![CDATA[ */ 
var price=prompt("What is your purchase price?"); 

getShipping(); 
document.write ("<p>The purchase price entered is $" + (price) + "</p>"); 
document.write ("<p>Shipping is $" + (shipping) + "</p>"); 

var total = price + shipping; 
document.write("Your total price with shipping is $ " + (total) + ""); 

/* ]]> */ 
</script> 

</body> 
</html> 

回答

4

你寫這樣的:

getShipping(); 
{ 
    if (price <= 25.00) 
    shipping = 1.50 
    else (price > 25.00) 
    shipping = (price * (10/100)); 
} 

這是調用當前未定義功能,然後進入一個塊。要定義一個函數,你必須使用function關鍵字:

function getShipping() 
{ 
    if (price <= 25.00) 
    shipping = 1.50 
    else (price > 25.00) 
    shipping = (price * (10/100)); 
} 

你也寫else (price > 25.00)。您需要在else(使其成爲else if (price > 25.00))或刪除(price > 25.00)之後添加if

1

你在這個代碼中的錯誤:

else (price > 25) 

你要是以後還缺少一個:

else if (price > 25) 

然而,由於第二個條件是第一的完全相反的,你不需要它。您可以使用:

else