Home » Javascript » Node Dynamic Parameters and Passing Info From Function to Vars

Node Dynamic Parameters and Passing Info From Function to Vars

Posted by: admin August 19, 2018 Leave a comment

Questions:

Say I have three variables, and each variable has an ID and a Price. I have the ID, and a function to figure out the price.

I want the price function to be dynamic so I don’t have to write an if statement for each ID.

How would I pass that into the price function, and then pass the price back to the right ID? (i.e. make sure it goes to t1Price instead of t2Price).

Bonus points if you can point me in the right direction so I’m not limited to 3 ID’s/prices.

var t1ID = 0;
var t1Price = 0;
var t2ID = 0;
var t2Price = 0;
var t3ID = 0;
var t3Price = 0;

//get the ID of order
order.then(function(value){
  if (t1ID == 0) {
    t1TXID = value.result.id[0];
    boughtPrice(t1ID, t1BoughtPrice);  //is this right?
  } else if (t2ID == 0) {
    t2ID = value.result.id[0];
    boughtPrice(t2ID, t1BoughtPrice);
  } else if (t3ID == 0) {
    t3ID = value.result.id[0];
  } else {
 console.log('IDs all full!');
}
//Get price based on ID
function boughtPrice (tradeID, tradeBoughtPrice) {
 var idInfo = api call ({
 ID: tradID
})
idInfo.then(function(value) {
    tradeBoughtPrice = value.result.tradeID.price;
    //I want to write the price back to var t1Price
};
Answers: