1

I have a function and it is supposed to return a number but instead it is returning NaN. Here is the code

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 is the variable that is supposed to have a number value when it is returned, for instance when I pass in student for the cust param, bus for the transit param(which has a monthly attribute which is 60), and monthly for the pass type param it should return 30, but instead I get NaN. I'm running a Jasmine test on it which is this

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);
    });
  });

});

And the function is part of this module if that matters

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
  };

})();
1
  • 2
    App.priceCalc('student', 'commuter rail', 'monthly') You're passing the string commuter rail for transit, instead of an object. Commented May 11, 2015 at 20:57

6 Answers 6

3

price = transit.monthly / 2;

Here you are telling the interpreter to use property monthly of object transit. In your code transit is a string so transit.monthly evaluates to undefined

undefined / 2 evaluates to NaN

I think you mean to pass in the variable objects you created instead of the string.

Sign up to request clarification or add additional context in comments.

1 Comment

Haha I can't believe I didn't notice that Thanks!
0

transit.monthly is likely a String, you should parse it before you do calculus on it:

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;

};

4 Comments

Interestingly ... "60"/2 returns 30. I'd like to see the value of transit.monthly at runtime.
Number("60")/2 should play safer though
Javascript will automatically convert a string to a number when you pass it to an arithmetic operator, so this isn't the problem.
Indeed my bad, the problem comes from undefined as answered by lucky2id. Worth noting, null / 2 seems to be 0
0

Your function expects three parameters:

function priceCalc(cust, transit, passType){}

In your tests, your passing a string as the second parameter transit, but the function is written as if transit is an object. So in your priceCalc function when you do:

price = transit.monthly / 2

price is set to NaN

It looks to me like the priceCalc function expects a Transport object.

Comments

0

You're passing a string into transit, where you probably meant to pass an object of class Transport.

If you add this code (yours):

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

to the beginning of your function, it will probably work.

Comments

0

You need to put the code that converts the names of transit methods to their corresponding objects into the priceCalc function:

  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;

  };

The code that you currently have that does this with the transitInput variable is useless, since that variable doesn't have a value.

Comments

0

You get NaN because you try to divide "commuter rail" with 2.

App.priceCalc('student', 'commuter rail', 'monthly') -> price = transit.monthly / 2; where transit is "commuter rail". You should probably use an object with a monthly property.

1 Comment

No, he's trying to divide "commuter rail".monthly by 2.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.