thepeskykid
05-14-2003, 09:38 AM
Hello,
I have an array (mainArray) that stores sub-arrays of debt data
Each sub-array or 'record' has an ID integer
Each debt 'record' is assigned a 'division answer' by dividing the total debt by the minimum monthly payment - thus each debt record has an ID integer (mainArray[n][0]) and a divisional answer (mainArray[n][4])
What I have to do is assign each debt record a debt priority integer (starting at 1) by finding the lowest divisional answer (assigning 1 to it), then finding the next lowest answer (assigning 2 to it) and so on...
The function below works, so long as there are no duplicate divisional answers, ie
tempDivAns = [10,12,5,20,18]
The problem occurs when there are duplicates, ie
tempDivAns = [10,12,5,10,18]
Can anyone spot what I am doing wrong here?
Is it just a logic problem I can't see or is my whole approach wrong - or overly complicated?
Thanks for any help you can give me on this
:)
function process_debts()
{
if (mainArray.length == 0)
{
alert("You must have entered at least one debt");
return;
}
var tempArray1 = new Array(2);
var tempArray2 = new Array(0);
for (i=0; i<mainArray.length; i++)
{
tempArray1[0] = mainArray[i][0];
tempArray1[1] = mainArray[i][4];
tempArray2[i] = [tempArray1[0] , tempArray1[1]];
}
alert(tempArray2);
// create list of just the division answers
var tempDivAns = new Array(0);
var tempDebtIDs = new Array(0);
for (i=0; i<tempArray2.length; i++)
{
tempDebtIDs[i] = tempArray2[i][0];
tempDivAns[i] = tempArray2[i][1];
}
alert(tempDivAns);
// assign debt payoff priority values
alert("start of pay off priority calc "+mainArray);
// check that all the items are not equal to each other
var flag = 0;
var po = 1;
var testItem;
var testItemPos;
var i = 0;
while (tempDivAns.length != 0)
{ //start i loop
testItem = tempDivAns[i];
testItemPos = i;
for (n=0; n<tempDivAns.length; n++) // start n loop
{
if (testItem < tempDivAns[n])
{
flag = 1;
}
if (testItem == tempDivAns[n])
{
if (n == testItemPos)
{
continue;
}
}
if (testItem > tempDivAns[n])
{
flag = 0;
}
} // end n loop
if (flag == 1)
{
mainArray[i][5] = po;
//alert(mainArray[i][5]);
po++;
//alert("pre splice: tempDivAns.length = "+tempDivAns.length);
tempDivAns.splice(i,1); // CHECK THIS
//alert("pre splice: tempDivAns.length = "+tempDivAns.length);
flag = 0;
i = -1;
}
i++;
if (tempDivAns.length == 1)
{
i = 0;
mainArray[i][5] = po;
tempDivAns.splice(i,1); // CHECK THIS
}
} // end i loop
alert("end of pay off priority calc "+mainArray);
}
I have an array (mainArray) that stores sub-arrays of debt data
Each sub-array or 'record' has an ID integer
Each debt 'record' is assigned a 'division answer' by dividing the total debt by the minimum monthly payment - thus each debt record has an ID integer (mainArray[n][0]) and a divisional answer (mainArray[n][4])
What I have to do is assign each debt record a debt priority integer (starting at 1) by finding the lowest divisional answer (assigning 1 to it), then finding the next lowest answer (assigning 2 to it) and so on...
The function below works, so long as there are no duplicate divisional answers, ie
tempDivAns = [10,12,5,20,18]
The problem occurs when there are duplicates, ie
tempDivAns = [10,12,5,10,18]
Can anyone spot what I am doing wrong here?
Is it just a logic problem I can't see or is my whole approach wrong - or overly complicated?
Thanks for any help you can give me on this
:)
function process_debts()
{
if (mainArray.length == 0)
{
alert("You must have entered at least one debt");
return;
}
var tempArray1 = new Array(2);
var tempArray2 = new Array(0);
for (i=0; i<mainArray.length; i++)
{
tempArray1[0] = mainArray[i][0];
tempArray1[1] = mainArray[i][4];
tempArray2[i] = [tempArray1[0] , tempArray1[1]];
}
alert(tempArray2);
// create list of just the division answers
var tempDivAns = new Array(0);
var tempDebtIDs = new Array(0);
for (i=0; i<tempArray2.length; i++)
{
tempDebtIDs[i] = tempArray2[i][0];
tempDivAns[i] = tempArray2[i][1];
}
alert(tempDivAns);
// assign debt payoff priority values
alert("start of pay off priority calc "+mainArray);
// check that all the items are not equal to each other
var flag = 0;
var po = 1;
var testItem;
var testItemPos;
var i = 0;
while (tempDivAns.length != 0)
{ //start i loop
testItem = tempDivAns[i];
testItemPos = i;
for (n=0; n<tempDivAns.length; n++) // start n loop
{
if (testItem < tempDivAns[n])
{
flag = 1;
}
if (testItem == tempDivAns[n])
{
if (n == testItemPos)
{
continue;
}
}
if (testItem > tempDivAns[n])
{
flag = 0;
}
} // end n loop
if (flag == 1)
{
mainArray[i][5] = po;
//alert(mainArray[i][5]);
po++;
//alert("pre splice: tempDivAns.length = "+tempDivAns.length);
tempDivAns.splice(i,1); // CHECK THIS
//alert("pre splice: tempDivAns.length = "+tempDivAns.length);
flag = 0;
i = -1;
}
i++;
if (tempDivAns.length == 1)
{
i = 0;
mainArray[i][5] = po;
tempDivAns.splice(i,1); // CHECK THIS
}
} // end i loop
alert("end of pay off priority calc "+mainArray);
}