cyberoot
cyberoot

Reputation: 340

how to call function variable into the other function parameter or argument?

I want to insert total_dist value in the calculatePrice parameter. How can i call total_dist in the total_dist function into calcautePrice total_dist paramter.

 function total_dist(){

      total_dist = getDistanceFromLatLonInKm(origin_lat,origin_lng,pickup_lat,pickup_lng);
      return total_dist;
    }


    function calculatePrice(base_cost, base_dist, additional_cost, additional_dist, total_dist) {  
      if (total_dist > base_dist) {
        var dist_diff = total_dist - base_dist;
        var time = Math.ceil(dist_diff / additional_dist);
        return (time * additional_cost) + base_cost;
      }else {
        return base_cost;
      }
    }

Thanks you so much.

Upvotes: 0

Views: 45

Answers (3)

Balayesu Chilakalapudi
Balayesu Chilakalapudi

Reputation: 1406

first obtain the return value of total_dist() and pass the same value as argument to calculatePrice() like this,

var td=total_dist();
calculatePrice(base_cost, base_dist, additional_cost, additional_dist, td)

Upvotes: 1

mulla.azzi
mulla.azzi

Reputation: 2676

Store the value returned by total_dist in a variable, say _t_dist. Now use this variable whereever you need. Below is the modified code.

function total_dist(){

  total_dist = getDistanceFromLatLonInKm(origin_lat,origin_lng,pickup_lat,pickup_lng);
  return total_dist;
}


function calculatePrice(base_cost, base_dist, additional_cost, additional_dist, total_dist) {
   var _t_dist = total_dist ();
  if (_t_dist > base_dist) {
    var dist_diff = _t_dist - base_dist;
    var time = Math.ceil(dist_diff / additional_dist);
    return (time * additional_cost) + base_cost;
  }else {
    return base_cost;
  }
}

Upvotes: 0

SeanCannon
SeanCannon

Reputation: 77966

You need to invoke it: if (total_dist() > base_dist) and so on.

You have a bigger problem in which you're implicitly declaring total_dist inside a function called total_dist so you're going to have reference issues. Do this instead:

function total_dist(){
    return getDistanceFromLatLonInKm(origin_lat, origin_lng, pickup_lat, pickup_lng);
}

Upvotes: 0

Related Questions