javascript - How to generate dynamic variable inside for loop and sum values to assign the dynamic variable? -


how create dynamic variable , sum values again assign dynamic variable?

for example,

var incomecr = 0, incomedr = 0;  (var j = 1; j <= noofmonths; j++)  {     if(response[i]["amounttype" + j] == "cr")     {         if(response[i]["closingbalancewithtype" + j] < 0)         {            response[i]["closingbalancewithtype" + j] = -1 * response[i]["closingbalancewithtype" + j];         }         incomecr += parsefloat(response[i]["closingbalancewithtype" + j]);     }     else if (response[i]["amounttype" + j] == "dr")     {         if (response[i]["closingbalancewithtype" + j] < 0)         {             response[i]["closingbalancewithtype" + j] = -1 * response[i]["closingbalancewithtype" + j];         }         incomedr += parsefloat(response[i]["closingbalancewithtype" + j]);     } } 

here how create dynamic variable incomecr1, incomecr2... , incomedr1, incomedr2... , sum values again assign values dynamic variable.

you can not create dynamic variables that. instead use array:

var incomecr = []; //incomecr[1], incomecr[2],... incomecr[1] = "txt"; incomecr[2] = "txt2"; 

Comments