为了账号安全,请及时绑定邮箱和手机立即绑定

为什么我的 JavaScript 开关返回“NaN”?

为什么我的 JavaScript 开关返回“NaN”?

Smart猫小萌 2023-09-25 16:26:04
我必须创建一个票价计算器,它还可以计算附加费率和金额。我的票价开关有效,但在我在提示中输入大小写后,附加费率开关返回为 NaN。我已将函数 surchargeRate(cardType) 返回的值转换为数字,但由于某种原因它仍然给我 NaN。我一直试图找出问题所在,但无法弄清楚。这是我的代码:function surchargeRate(cardType) {  var surchargeRate;  switch (cardType) {    case "1":      surchargeRate = 0.02;      break;    case "2":      surchargeRate = 0.01;      break;  } // end switch  return surchargeRate;}// This function calculates the surcharge amount.function surchargeAmount(ticketCost, surchargeRate) {  return ticketCost * surchargeRate;}function determineTicketPrice(seatingAreaCategory) {  var ticketPrice;  switch (seatingAreaCategory) {    case "S":    case "s":      ticketPrice = 180;      break;    case "C":    case "c":      ticketPrice = 150;      break;    case "B":    case "b":      ticketPrice = 100;      break;    case "R":    case "r":      ticketPrice = 70;      break;    default:      ticketPrice = 0;      break;  } // end switch  return ticketPrice;}function totalAmount(ticketPrice, ticketQty) {  return ticketPrice * ticketQty;}function validateQty(ticketQty) {  if (ticketQty >= 1 && ticketQty <= 20) {    return true;  } else {    alert("Ticket number must be between 1 and 20");    return false;  }}function init() {  surchargeRate = surchargeRate(cardType);  surchargeRate = Number(surchargeRate);  var surcharge;  var finalCost;  if (surchargeRate == -1) {    surchargeRate = new Error("Invalid input");  } else {    surcharge = surchargeAmount(ticketCost, surchargeRate);  }
查看完整描述

1 回答

?
白衣非少年

TA贡献1155条经验 获得超0个赞

  1. 默认值var surchargeRate = 0;default: surchargeRate = 0;var ticketPrice = 0;

  2. 没有提升变量var- 函数windows.surchargeRate被覆盖surchargeRate = surchargeRate(cardType);

  3. Init 在没有任何输入的情况下进行大量计算 - 首先获取输入,然后使用它

function surchargeRate(cardType) {


  var surchargeRate;


  switch (cardType) {

    case "1":

      surchargeRate = 0.02;

      break;

    case "2":

      surchargeRate = 0.01;

      break;

  } // end switch

  return surchargeRate;

}


// This function calculates the surcharge amount.


function surchargeAmount(ticketCost, surchargeRate) {


  return ticketCost * surchargeRate;

}


function determineTicketPrice(seatingAreaCategory) {


  var ticketPrice = 0;

  switch (seatingAreaCategory) {


    case "S":

    case "s":

      ticketPrice = 180;

      break;

    case "C":

    case "c":

      ticketPrice = 150;

      break;

    case "B":

    case "b":

      ticketPrice = 100;

      break;

    case "R":

    case "r":

      ticketPrice = 70;

      break;

    default:

      ticketPrice = 0;

      break;

  } // end switch

  return ticketPrice;

}


function totalAmount(ticketPrice, ticketQty) {


  return ticketPrice * ticketQty;

}


function validateQty(ticketQty) {


  if (ticketQty >= 1 && ticketQty <= 20) {

    return true;

  } else {

    alert("Ticket number must be between 1 and 20");

    return false;

  }

}


function init() {


  var beforeSurchargeOut = document.getElementById("beforeSurcharge");

  var costOut = document.getElementById("amountDue");

  var priceOut = document.getElementById("pricePerTicket");

  var seatOut = document.getElementById("seatingArea");

  var surchargeOut = document.getElementById("surcharge");

  var validQtyOut = document.getElementById("ticketQty");



  var surcharge;

  var finalCost;

  var seatingAreaCategory;

  seatingAreaCategory = prompt("Please enter seating area category: ");


  var ticketPrice = determineTicketPrice(seatingAreaCategory);

  ticketPrice = Number(ticketPrice);


  var ticketQty;

  ticketQty = prompt("Please enter ticket quantity: ");

  ticketQty = Number(ticketQty);


  var ticketCost;

  ticketCost = totalAmount(ticketPrice, ticketQty);


  var cardType;

  cardType = prompt("Enter your Card type. Enter 1 for American Express and 2 for Visa/Mastercard ");


  var validQty = validateQty(ticketQty);


  if (surcharge == -1) {

    surcharge = new Error("Invalid input");

  } else {

    surcharge = surchargeAmount(ticketCost, surchargeRate(cardType));

  }


  finalCost = surcharge + ticketCost;



  console.log(cardType);

  console.log(surcharge);


  beforeSurchargeOut.innerHTML = "Gross amount: " + ticketCost.toFixed(2);

  costOut.innerHTML = "Amount due: " + finalCost.toFixed(2);

  priceOut.innerHTML = "Price per ticket: " + ticketPrice.toFixed(2);

  seatOut.innerHTML = "Your seating area is: " + seatingAreaCategory;

  surchargeOut.innerHTML = "Card fee: " + surcharge.toFixed(2);

  validQtyOut.innerHTML = "Number of tickets ordered: " + ticketQty;

}


window.onload = init;

<span id="beforeSurcharge"></span><br/>

<span id="amountDue"></span><br/>

<span id="pricePerTicket"></span><br/>

<span id="seatingArea"></span><br/>

<span id="surcharge"></span><br/>

<span id="ticketQty"></span><br/>


查看完整回答
反对 回复 2023-09-25
  • 1 回答
  • 0 关注
  • 118 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信