JavaScript 函数正在返回 NaN

JavaScript function is returning NaN

我有一个函数,它应该是 return 一个数字,但它是 returning NaN。这是代码

function priceCalc(cust, transit, passType){

    var price = 0;
    if (passType === "monthly"){
      if (cust === "student" || cust === "elderly"){
        price = transit.monthly / 2;
      } else if (cust === "transit worker"){
        price = 0;
      } else {
        price = transit.monthly;
      }

    } else if (passType === "pre paid"){
      if (cust === "transit worker") {
        price = Infinity;
      } else {
        value = prompt('How much would you like on your pass?');
        price = parseInt(value);
      }

    }
    return price;

  };

price 是在 returned 时应该具有数字值的变量,例如,当我将 student 传递给 cust 参数时,将 bus 传递给 transit 参数(它具有 monthly 属性这是 60),每月对于传递类型参数它应该 return 30,但我得到的是 NaN。我正在 运行 对其进行 Jasmine 测试,这是

describe('the application', function(){
  describe('publicPrice function', function(){

    it('takes in customer status, transit choice, and pass choice to calculate a price', function(){
      expect(App.priceCalc('adult', 'commuter rail', 'monthly')).toBe(80);
      expect(App.priceCalc('student', 'commuter rail', 'monthly')).toBe(40);
      expect(App.priceCalc('elderly', 'subway', 'monthly')).toBe(35);
      expect(App.priceCalc('transit worker', 'bus', 'monthly')).toBe(0);
    });
  });

});

如果重要的话,该函数是该模块的一部分

var App = (function(){
  var Transport = function(mode, monthly, prepaid){
    this.mode = mode;
    this.monthly = monthly;
    this.prepaid = prepaid;
  };

  var commuterRail = new Transport('commuter rail', 80, 5);
  var bus = new Transport('bus', 60, 2);
  var subway = new Transport('subway', 70, 3);

  var customerStatus = prompt('Please enter your status. \nAdult \nElderly \nStudent \nTransit worker');
  var transitInput = prompt('Please select your method of transport: \ncommuter rail \nbus \nsubway');
  var passSelection = prompt('Please select a pass: \nMonthly \nPrepaid');

  var transitMethod;

  if(transitInput === "commuter rail"){
    transitMethod = commuterRail;
  } else if(transitInput === "bus"){
    transitMethod = bus;
  } else if (transitInput === "subway"){
    transitMethod = subway;
  }

  console.log(transitMethod);

  function priceCalc(cust, transit, passType){

    var price = 0;
    if (passType === "monthly"){
      if (cust === "student" || cust === "elderly"){
        price = transit.monthly / 2;
      } else if (cust === "transit worker"){
        price = 0;
      } else {
        price = transit.monthly;
      }

    } else if (passType === "pre paid"){
      if (cust === "transit worker") {
        price = Infinity;
      } else {
        value = prompt('How much would you like on your pass?');
        price = parseInt(value);
      }

    }
    return price;

  };

  var publicPrice = function(customerStatus, transitMethod, passSelection){
    return priceCalc(customerStatus, transitMethod, passSelection);
  };

  priceCalc(customerStatus, transitMethod, passSelection);

  return {
    // publicPrice: publicPrice,
    priceCalc: priceCalc
  };

})();

transit.monthly 可能是一个字符串,你应该在对它进行微积分之前解析它:

function priceCalc(cust, transit, passType){

var price = 0;
if (passType === "monthly"){
  if (typeof transit.monthly === 'string') {
    transit.monthly = Number(transit.monthly);
  }
  if (cust === "student" || cust === "elderly"){
    price = transit.monthly / 2;
  } else if (cust === "transit worker"){
    price = 0;
  } else {
    price = transit.monthly;
  }

} else if (passType === "pre paid"){
  if (cust === "transit worker") {
    price = Infinity;
  } else {
    value = prompt('How much would you like on your pass?');
    price = parseInt(value);
  }

}
return price;

};

您的函数需要三个参数:

function priceCalc(cust, transit, passType){}

在您的测试中,您传递了一个字符串作为第二个参数 transit,但是该函数的编写方式好像 transit 是一个对象。所以在你的 priceCalc 函数中,当你这样做时:

price = transit.monthly / 2

价格设置为 NaN

在我看来,priceCalc 函数需要一个 Transport 对象。

price = transit.monthly / 2;

在这里您告诉解释器使用对象 transit 的 属性 monthly。在您的代码中 transit 是一个字符串,因此 transit.monthly 的计算结果为 undefined

undefined / 2 计算结果为 NaN

我想你的意思是传递你创建的变量对象而不是字符串。

您正在将字符串传递到传输中,您可能打算在其中传递 class 传输的对象。

如果您添加此代码(您的):

if(transit === "commuter rail"){
    transit = commuterRail;
  } else if(transit === "bus"){
    transit = bus;
  } else if (transit === "subway"){
    transit = subway;
  }

到你函数的开头,它可能会起作用。

需要将中转方法名称转换为对应对象的代码放入priceCalc函数中:

  function priceCalc(cust, transit, passType){

    var price = 0;
    if(transit === "commuter rail"){
        transit = commuterRail;
    } else if(transit === "bus"){
        transit = bus;
    } else if (transit === "subway"){
        transit = subway;
    }

    if (passType === "monthly"){
      if (cust === "student" || cust === "elderly"){
        price = transit.monthly / 2;
      } else if (cust === "transit worker"){
        price = 0;
      } else {
        price = transit.monthly;
      }

    } else if (passType === "pre paid"){
      if (cust === "transit worker") {
        price = Infinity;
      } else {
        value = prompt('How much would you like on your pass?');
        price = parseInt(value);
      }

    }
    return price;

  };

您当前使用 transitInput 变量执行此操作的代码是无用的,因为该变量没有值。

你得到 NaN 因为你试图将 "commuter rail" 除以 2.

App.priceCalc('student', 'commuter rail', 'monthly') -> price = transit.monthly / 2; 其中 transit"commuter rail"。您可能应该使用每月 属性.

的对象