2015-10-13 51 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