功能未正常运行/被调用

时间:2017-03-27 06:01:34

标签: javascript function switch-statement

变量vTotalPayment不起作用。我认为这是由于我如何调用函数,虽然我无法弄清楚我做错了什么。我知道这也可能是因为误用了switch语句。



function begin() {
  var vTicketType = prompt('Please enter the type of ticket');
  var vTicketQty = Number(prompt('Please enter amount required'));

  document.write('Ticket Type: ' + vTicketType + '<br />');
  document.write('Ticket Qty: ' + vTicketQty + '<br />');

  var vTicketPrice = calcPrice(vTicketType);
  var vTotalPayment = calcTotal(vTicketQty, vTicketPrice);

  document.write('Total Payment: ' + vTotalPayment);

}
begin();

function calcPrice(Prices) {
  var Price = 0;
  switch (vTicketType) {
    case 'A':
      Price = 100;
      break;
    case 'B':
      Price = 75;
      break;
    case 'C':
      Price = 50;
      break;
    default:
      Price = -1;
  }
  return (Price);
}


function calcTotal(Payments) {
  var Payment = (vTicketQty * vTicketPrice);
  return (Payment);
}
&#13;
&#13;
&#13;

3 个答案:

答案 0 :(得分:1)

您只将一个参数传递给函数calcTotal,甚至不使用它。用以下代码替换你的calcTotal,如果它仍然不起作用,请在评论中告诉我。

function calcTotal(vTicketQty, vTicketPrice) {
    return (vTicketQty * vTicketPrice);
}

答案 1 :(得分:0)

您的方法参数错误。错误的参数数量和错误的名称。

而不是calcPrice(Prices)我认为它应该是calcPrice(vTicketType)而不是calcTotal(Payments)它应该是calcTotal(vTicketQty, vTicketPrice)

function begin() {
    var vTicketType = prompt('Please enter the type of ticket');
    var vTicketQty = Number(prompt('Please enter amount required'));

    document.write('Ticket Type: ' + vTicketType + '<br />');
    document.write('Ticket Qty: ' + vTicketQty + '<br />');

    var vTicketPrice = calcPrice(vTicketType);
    var vTotalPayment = calcTotal(vTicketQty, vTicketPrice);

    document.write('Total Payment: ' + vTotalPayment);

}
begin();

function calcPrice(vTicketType) {
    var Price = 0;
    switch (vTicketType) {
        case 'A':
            Price = 100;
            break;
        case 'B':
            Price = 75;
            break;
        case 'C':
            Price = 50;
            break;
        default:
            Price = -1;
    }
    return (Price);
}


function calcTotal(vTicketQty, vTicketPrice) {
    var Payment = (vTicketQty * vTicketPrice);
    return (Payment);
}

答案 2 :(得分:0)

您可以执行以下操作:

&#13;
&#13;
(function begin() {
    var vTicketType = prompt('Please enter the type of ticket');
    var vTicketQty = Number(prompt('Please enter amount required'));

    document.write('Ticket Type: ' + vTicketType + '<br />');
    document.write('Ticket Qty: ' + vTicketQty + '<br />');

    var vTicketPrice = calcPrice(vTicketType);
    var vTotalPayment = calcTotal(vTicketQty, vTicketPrice);

    document.write('Total Payment: ' + vTotalPayment);

})();

function calcPrice(vTicketType) {
    var Price = 0;
    switch (vTicketType) {
        case 'A':
            Price = 100;
            break;
        case 'B':
            Price = 75;
            break;
        case 'C':
            Price = 50;
            break;
        default:
            Price = -1;
    }
    return Price;
}


function calcTotal(vTicketQty, vTicketPrice) {
    var Payment = (vTicketQty * vTicketPrice);
    return Payment;
}
&#13;
&#13;
&#13;

为了获得最佳实践,当您退回付款或返回价格时,您不需要()括号