javascript - 难以分配折扣

标签 javascript

正如标题所示,我在为我的产品分配折扣时遇到问题。当我加载页面时,它显示 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();

关于javascript - 难以分配折扣,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42942384/

相关文章:

php - FB 好友选择器框架位于 https 中,无法与 http 中的托管页面交互

javascript - 如何在knockoutjs中使用原型(prototype)对象的实例而不是JSON

javascript - 有人可以解释一下 howtonode 中的函数包装习惯吗?

javascript - 使用 javascript 读取 bash 输出

php - 查看 PHP、Javascript 和 HTML 的源代码?

javascript - RactiveJS 将多个对象绑定(bind)到模板

javascript - 在 Javascript 中使用 LocalStorage

javascript - Angular 4 ng-content select 不适用于动态创建的嵌入元素

javascript - 为什么我的 Fullcalendar 保存的事件会转到前一天?

javascript - 如何使用 moment js 从日期时间字符串中获取 am pm