如标题所述,我无法为产品分配折扣。当我加载页面时,它说nan和undefined。任何帮助表示赞赏。谢谢!

这是我的JavaScript代码:

var ProductType;
var ProductQty;
var ProductPrice;
var DiscountPercent;
var TotalAmount;
function calculate() {
  ProductType = prompt("Please enter the type of product you require!").toUpperCase();
  document.write("<br>");
  ProductQty = prompt("Please enter the number of products you require!");
  elsestatement();
  ProductQty = parseInt(ProductQty);
  document.write("Type of Products:" + ProductType);
  document.write("<br>");
  document.write("Number of Products:" + ProductQty);
  document.write("<br>");
  var GrossAmount =(ProductPrice) * (ProductQty);
  document.write("Gross Amount is:" + GrossAmount);
  GrossAmount = parseInt(GrossAmount);
  discountAmt();
  var DiscountAmount = (GrossAmount) - (GrossAmount) * (DiscountPercent)
  var TotalAmount = (GrossAmount) * (DiscountPercent)
  document.write("<br>");
  document.write("Discount Amount:" + DiscountAmount)
  document.write("<br>");
  document.write("Discount Percent:" + DiscountPercent)
  document.write("<br>");
  document.write("Total Amount:" + TotalAmount)
}

function elsestatement(){
  if (ProductType == 'A') {
    ProductPrice = 100;
  } else if (ProductType == 'B') {
    ProductPrice = 75;
  }  else if (ProductType == 'C'){
    ProductPrice = 50;
  }
  else {
    document.write("<br>");
    document.write("Invalid Product Type");
    document.write("<br>");
  }
  if (ProductQty <1|| ProductQty >100) {
    document.write("Invalid Quantity")
  }
}

function discountAmt() {
  if (GrossAmount <200) {
    DiscountPercent = '0';
  }  else if (GrossAmount >= 200 && GrossAmount<=399.99) {
    DiscountPercent = '.05';
  }  else if (GrossAmount>=400 && GrossAmount<=599.99 ) {
    DiscountPercent = '.075';
  }  else if (GrossAmount >=600)
    DiscountPercent = '.1';
}


这是我的HTML代码:

<!DOCTYPE html>
<html>
<title>Product</title>
<body>
<h1>Product Calc</h1>
<script src="Product.js">   </script>
<script>calculate()</script>
<script>elsestatement()</script>
<script>discountAmt()</script>
</body>

最佳答案

对困惑感到抱歉。我被误认为是未封闭的功能。相反,问题在于GrossAmount是在calculate函数而不是外部范围中定义的。因此,在discountAmt函数中无法达到。

这是您的固定代码,除了删除了document.write以便可以在沙箱中运行:



var ProductType;
var ProductQty;
var ProductPrice;
var DiscountPercent;
var TotalAmount;
var GrossAmount;

function calculate() {
  ProductType = prompt("Please enter the type of product you require!").toUpperCase();
  ProductQty = prompt("Please enter the number of products you require!");
  elsestatement();
  ProductQty = parseInt(ProductQty);
  GrossAmount = ProductPrice * ProductQty;
  GrossAmount = parseInt(GrossAmount);
  discountAmt();
  var DiscountAmount = GrossAmount - GrossAmount * DiscountPercent;
  var TotalAmount = GrossAmount * DiscountPercent;
}

function elsestatement(){
  if (ProductType == 'A') {
    ProductPrice = 100;
  } else if (ProductType == 'B') {
    ProductPrice = 75;
  } else if (ProductType == 'C'){
    ProductPrice = 50;
  } else {}
  if (ProductQty < 1|| ProductQty > 100) {}
  console.log('ProductPrice: ', ProductPrice);
}

function discountAmt() {
  if (GrossAmount < 200) {
    DiscountPercent = '0';
  } else if (GrossAmount >= 200 && GrossAmount <= 399.99) {
    DiscountPercent = '.05';
  } else if (GrossAmount >= 400 && GrossAmount <= 599.99) {
    DiscountPercent = '.075';
  } else if (GrossAmount >= 600) {
    DiscountPercent = '.1';
  }
  console.log('DiscountPercent: ', DiscountPercent);
}

calculate();

09-25 18:32