EWPS07
EWPS07

Reputation: 21

How do I use the result of one function in another?

sorry this is all of it now...taking me a while to get used to this site. I had no idea you guys would be so fast. Like I was saying the first functions runs fine, so I don't understand why I can't use this.DonutsPH in the getDonutsPD function.

  function donutStore(location, minCustPh, maxCustPh, averageDpC, hoursOpen) {
      this.location   = location;
      this.minCustPh  = minCustPh;
      this.maxCustPh  = maxCustPh;
      this.averageDpC = averageDpC;
      this.hoursOpen  = hoursOpen;
      this.donutsPerHourValue = 0;
      this.getDonutsPH = function() {
       console.log(this.donutsPerHourValue = (this.location + " needs " + (Math.floor(Math.random() * (this.maxCustPh - this.minCustPh) + this.minCustPh)) * this.averageDpC) + " donuts per hour.");
    };
    this.getDonutsPD = function() {
     console.log(this.getDonutsPH * this.hoursOpen);
    };
     var Downtown    = new donutStore("Downtown",       8, 43, 4.5,  24);
    var CapitolHill = new donutStore("CapitolHill",    4, 37, 2,    12);
    var SLU         = new donutStore("SouthLakeUnion", 9, 23, 6.33, 24);
    var wWood       = new donutStore("WestWood",       2, 28, 1.25, 12);
    var Ballard     = new donutStore("Ballard",        8, 58, 3.75, 16);
    var dNutStores = [Downtown, CapitolHill, SLU, wWood, Ballard];

    for(i=0; i < dNutStores.length; i ++) 
    {
      dNutStores[i].getDonutsPH();
      dNutStores[i].getDonutsPD();
    }

Upvotes: 2

Views: 123

Answers (1)

Populus
Populus

Reputation: 7680

this.getDonutsPH is a variable. In javascript functions can also be variables.

In order to run any function you need to append () to the function name, e.g.

this.getDonutsPH()

Second thing, doing a console.log('blah') does not return any value, you need to tell the function to return a value

this.getDonutsPH = function() {
    return this.location + " needs " + (Math.floor(Math.random() * (this.maxCustPh - this.minCustPh) + this.minCustPh)) * this.averageDpC) + " donuts per hour.";
}

Update: JoshBeam comment:-

I want to point out, too, that this.getDonutsPH * this.hoursOpen will return NaN, because this.getDonutsPH will return a string. Multiplying a string by a number will always return NaN.

Upvotes: 3

Related Questions