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

我必须创建一个票价计算器,它还可以计算附加费率和金额。


我的票价开关有效,但在我在提示中输入大小写后,附加费率开关返回为 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);

  }


Smart猫小萌
浏览 119回答 1
1回答

白衣非少年

默认值var surchargeRate = 0;或default: surchargeRate = 0;和var ticketPrice = 0;没有提升变量var- 函数windows.surchargeRate被覆盖surchargeRate = surchargeRate(cardType);Init 在没有任何输入的情况下进行大量计算 - 首先获取输入,然后使用它function surchargeRate(cardType) {&nbsp; var surchargeRate;&nbsp; switch (cardType) {&nbsp; &nbsp; case "1":&nbsp; &nbsp; &nbsp; surchargeRate = 0.02;&nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; case "2":&nbsp; &nbsp; &nbsp; surchargeRate = 0.01;&nbsp; &nbsp; &nbsp; break;&nbsp; } // end switch&nbsp; return surchargeRate;}// This function calculates the surcharge amount.function surchargeAmount(ticketCost, surchargeRate) {&nbsp; return ticketCost * surchargeRate;}function determineTicketPrice(seatingAreaCategory) {&nbsp; var ticketPrice = 0;&nbsp; switch (seatingAreaCategory) {&nbsp; &nbsp; case "S":&nbsp; &nbsp; case "s":&nbsp; &nbsp; &nbsp; ticketPrice = 180;&nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; case "C":&nbsp; &nbsp; case "c":&nbsp; &nbsp; &nbsp; ticketPrice = 150;&nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; case "B":&nbsp; &nbsp; case "b":&nbsp; &nbsp; &nbsp; ticketPrice = 100;&nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; case "R":&nbsp; &nbsp; case "r":&nbsp; &nbsp; &nbsp; ticketPrice = 70;&nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; default:&nbsp; &nbsp; &nbsp; ticketPrice = 0;&nbsp; &nbsp; &nbsp; break;&nbsp; } // end switch&nbsp; return ticketPrice;}function totalAmount(ticketPrice, ticketQty) {&nbsp; return ticketPrice * ticketQty;}function validateQty(ticketQty) {&nbsp; if (ticketQty >= 1 && ticketQty <= 20) {&nbsp; &nbsp; return true;&nbsp; } else {&nbsp; &nbsp; alert("Ticket number must be between 1 and 20");&nbsp; &nbsp; return false;&nbsp; }}function init() {&nbsp; var beforeSurchargeOut = document.getElementById("beforeSurcharge");&nbsp; var costOut = document.getElementById("amountDue");&nbsp; var priceOut = document.getElementById("pricePerTicket");&nbsp; var seatOut = document.getElementById("seatingArea");&nbsp; var surchargeOut = document.getElementById("surcharge");&nbsp; var validQtyOut = document.getElementById("ticketQty");&nbsp; var surcharge;&nbsp; var finalCost;&nbsp; var seatingAreaCategory;&nbsp; seatingAreaCategory = prompt("Please enter seating area category: ");&nbsp; var ticketPrice = determineTicketPrice(seatingAreaCategory);&nbsp; ticketPrice = Number(ticketPrice);&nbsp; var ticketQty;&nbsp; ticketQty = prompt("Please enter ticket quantity: ");&nbsp; ticketQty = Number(ticketQty);&nbsp; var ticketCost;&nbsp; ticketCost = totalAmount(ticketPrice, ticketQty);&nbsp; var cardType;&nbsp; cardType = prompt("Enter your Card type. Enter 1 for American Express and 2 for Visa/Mastercard ");&nbsp; var validQty = validateQty(ticketQty);&nbsp; if (surcharge == -1) {&nbsp; &nbsp; surcharge = new Error("Invalid input");&nbsp; } else {&nbsp; &nbsp; surcharge = surchargeAmount(ticketCost, surchargeRate(cardType));&nbsp; }&nbsp; finalCost = surcharge + ticketCost;&nbsp; console.log(cardType);&nbsp; console.log(surcharge);&nbsp; beforeSurchargeOut.innerHTML = "Gross amount: " + ticketCost.toFixed(2);&nbsp; costOut.innerHTML = "Amount due: " + finalCost.toFixed(2);&nbsp; priceOut.innerHTML = "Price per ticket: " + ticketPrice.toFixed(2);&nbsp; seatOut.innerHTML = "Your seating area is: " + seatingAreaCategory;&nbsp; surchargeOut.innerHTML = "Card fee: " + surcharge.toFixed(2);&nbsp; 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/>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Html5