2015-10-13 70 views
-1

我的代碼有錯誤。Javascript College CA Bug

其目標是,如果用戶購買足夠250歐元以上的運動衫,我必須在15%之後將所有下一個商品折扣。

我的代碼的第一部分工作(如果成本< = 250)上面的任何東西給我一個(NaN)。

我的代碼如下:

<!DOCTYPE html> 
<html> 
<head> 
    <meta charset="UTF-8" /> 

    <title> 
     Q1 - Jerseys 
    </title> 

    <style type="text/css"> 
    </style> 

    <script> 
     "use strict"; 

     function processOrder() 
     { 
      //Calculations for Order 
      var type = document.getElementById("getType").value; 
      var number = parseInt(document.getElementById("getNumber").value); 
      var cost; 
      var PER_FIVE = parseFloat(0.15); 
      var overPrice = cost - 250; 

      //Logo Variables 
      var logo = document.getElementById("getLogo").value || "N"; 
      var logoCost = 1.50; 

      //Empty Variables for Returned Values 
      var outputText; 

      //Polo Shirt Case 
      if (type === "Polo" && logo === "Y") 
      { 
       cost = (number * (22.50 + logoCost)); 

      } else if (type === "Polo" && logo === "N") { 

       cost = parseFloat(number * 22.50); 

      }//End Polo 

      //Short Sleeve Case 
      if (type === "Short" && logo === "Y") { 

       cost = (number * (22.50 + logoCost)); 

      } else if (type === "Short" && logo === "N") { 

       cost = number * 25.50; 

      }//End Short 

      //Long Sleeve Case 
      if (type === "Long" && logo === "Y") { 

       cost = (number * (22.50 + logoCost)); 

      } else if (type === "Long" && logo === "N") { 

       cost = number * 28.50; 

      }//End Long 

      //Output To "results" Text Area 
      if (cost <= 250) { 

       outputText = "The cost of your jerseys is €" + cost; 
       document.getElementById("results").value = outputText; 

      } else if (cost > 250) { 

       outputText = "The cost of your jerseys is €" + (250 +  (overPrice - (overPrice * PER_FIVE))); 
       document.getElementById("results").value = outputText; 

      }//End If 

     }//End Function 


    </script> 
    </head> 

    <body> 
    Please Enter details of your jersey order: 
    <br> <br> 
    Type of jersey (Polo, Short,Long): 
    <input id="getType" /> 
    <br> <br> 

    Number of Jerseys: 
    <input id="getNumber" /> 
    <br> <br> 

    Add A Logo: 
    <input id="getLogo" maxlength="1"/> Type: "Y" or "N". 
    <br> <br> 

    <button onclick="processOrder()" >Click to see results below</button> 
    <br> <br> 

    Results: 
    <br> 
    <textarea id="results" rows="4" cols="50" readonly > 
    </textarea> 

    </body> 
    </html> 
+0

請儘量保持你的數學和字符串連接的獨立性。 – VoronoiPotato

回答

0

overPricecost之前已經確定你分配:

var overPrice = cost - 250; 

此時costundefined,並undefined - 250是什麼給你NaN

變量不以這種方式變化 - 更新cost不會自動更新overPrice - 您需要在知道cost的值後設置它。在因爲沒有其他地方需要用到它的else if塊將是適當的:

//Output To "results" Text Area 
if (cost <= 250) { 
    outputText = "The cost of your jerseys is €" + cost; 
    document.getElementById("results").value = outputText; 
} else if (cost > 250) { 
    var overPrice = cost - 250; 
    outputText = "The cost of your jerseys is €" + (250 +  (overPrice - (overPrice * PER_FIVE))); 
    document.getElementById("results").value = outputText; 
}//End If 

在一個單獨的說明,您正在使用parseInt不指定基數的參數。 It's recommended總是指定它以避免潛在問題:

var number = parseInt(document.getElementById("getNumber").value, 10); 
+0

非常感謝您的快速回復和幫助! – RidiculousBeans