2014-03-13 170 views
0

我想在calculatePrice參數中插入total_dist值。如何將total_dist函數中的total_dist調用到calcautePrice total_dist參數中。如何將函數變量調用到其他函數參數或參數中?

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

非常感謝你。

+1

我不明白你的問題..why你要做到這一點 –

+0

我知道了老兄感謝你的幫助。 – cyberoot

回答

1

首先獲得的total_dist()返回值和值相同參數傳遞給calculatePrice()這樣,

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

是兄弟它正確。非常感謝你。很不錯 – cyberoot

+0

我需要投票,但我不能兄弟。對不起。 <3你 – cyberoot

0

您需要調用它:if (total_dist() > base_dist)等等。

您有一個更大的問題,即您在隱含聲明total_dist的函數內部,因此您將遇到參考問題。取而代之:

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

感謝兄弟,現在我得到了由BalaYesu Chilakalapudi答案 – cyberoot

0

將total_dist返回的值存儲在變量中,比如_t_dist。現在使用這個變量,無論你需要什麼。以下是修改後的代碼。

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

謝謝你,但我只是需要函數參數value.but現在修復好 – cyberoot

相關問題