0

I'm trying to build a minimum payment calculator and am having a problem with the minimum payment not being able to calculate based on the current monthly value. What I think should work...locks up the browser. I've commented out the line that is giving me a problem in the code below. I would appreciate if someone could lend a hand.

<?xml version="1.0" encoding="utf-8"?>
<s:Application
xmlns:fx="http://ns.adobe.com/mxml/2009" 
xmlns:s="library://ns.adobe.com/flex/spark" 
xmlns:mx="library://ns.adobe.com/flex/mx"
minWidth="1296" minHeight="768"
width="100%" height="408"  
backgroundColor="#B9ADFF" >

<fx:Declarations>
    <mx:CurrencyFormatter 
    id="Price" precision="2"
    rounding="nearest"
    decimalSeparatorTo="."
    thousandsSeparatorTo=","
    useThousandsSeparator="false"
    useNegativeSign="true"
    currencySymbol="$"
    alignSymbol="left"/>
</fx:Declarations>

<fx:Script>
<![CDATA[

    import mx.collections.ArrayCollection;

    [Bindable]
    private var myDataProvider:ArrayCollection = new ArrayCollection();

    protected function clickHandler(event:MouseEvent):void { 
        myDataProvider.removeAll();

        //Creditor Constants
        var c:Number = Number(1); //start counting at
        var b1:Number = Number(bal1.text); //initial balance
        var r1:Number = Number(apr.text) / 100 / 12;//convert apr to decimal
        var m1:Number = Number(mpp.text) / 100; //convert mpp to decimal

        var mpp:Number = Number(b1 * m1); //minimum payment by percentage
        var ipd:Number = Number(b1 * r1); //interest paid
        var ppd:Number = Number(mpp - ipd); //principle paid

        while(b1 >= 0) {
            myDataProvider.addItem({
                "months" : c, 
                "intBal" : Price.format(b1),  //balance
                "pPaid" : Price.format(ppd),  //principle paid
                "intPd" : Price.format(ipd),  //interest paid
                "minmopmt" : Price.format(mpp) //minimum payment
            });

            c   = (c + 1); //count rows
            b1 -= (ppd);  // Balance minus Principle Paid

            ///////// THE PROBLEM LINE IS BELOW /////////////
                //mpp = (b1 * m1); //minimum payment by percentage

            ipd = (b1 * r1); //Interest Paid
            ppd = (mpp - ipd); // Principle Paid
        }  
    }
]]>
</fx:Script>

<s:Button label="Calculate" x="26" y="238"
          click="clickHandler(event)" />

<s:TextInput x="22" y="277" id="bal1" restrict="[0-9.\-]" textAlign="right" text="1500"/>   
<s:Label x="158" y="287" text="Initial Balance&#xd;"/>      
<s:TextInput x="22" y="307" id="apr" restrict="[0-9.\-]" textAlign="right" text="15"/>
<s:Label x="158" y="317" text="Annual Percentage Rate (APR)"/>      
<s:TextInput x="22" y="337" id="mpp" restrict="[0-9.\-]" textAlign="right" text="2"/>
<s:Label x="158" y="347" text="Minimum Payment Percentage"/>

<mx:DataGrid dataProvider="{myDataProvider}" y="10" id="dg" height="184" x="22">
    <mx:columns>
        <mx:DataGridColumn dataField="months" headerText="Mo" width="30"/>
        <mx:DataGridColumn dataField="intBal" headerText="Balance" width="120"/>
        <mx:DataGridColumn dataField="pPaid" headerText="Principle Paid"  width="120"/>
        <mx:DataGridColumn dataField="intPd" headerText="Interest Paid"  width="120"/>
        <mx:DataGridColumn dataField="minmopmt" headerText="Min Monthly Pmt"  width="120"/>             
    </mx:columns>
</mx:DataGrid>

</s:Application>
dmschenk
  • 379
  • 1
  • 5
  • 19

3 Answers3

1

My guess is that you're finding yourself in an endless loop. This can happen if your calculations above either make ppd negative (since you're subtracting it), or make it get small so fast that you essentially never get b1 to zero (i.e., you have an asymptotic graph for the balance).

One way to troubleshoot this would be to count rows and break out of the loop if the number of rows gets large. Change "while(b1 >= 0)" to "while( b1 >= 0 && c < 50 )" or something.

cc.
  • 3,041
  • 1
  • 21
  • 24
0

There has to be a least threshold amount where the payment has to end. In your situation there is none and it is exactly at the line where you have pointed out. Consider defining a minimum payment amount. e.g.

var minimumPaymentAmount = 1;

....

if(b1 * m1 < minimumPaymentAmount) 
  mpp =  minimumPaymentAmount;     
else
  mpp =  b1 * m1; 

You should also consider limiting number of rows calculated to like 500 or so

Faheem
  • 3,429
  • 19
  • 26
0

You have discovered Zeno's paradox. If the rate (payment) is always proportional to the balance, the balance will never go completely to zero. Suppose you always paid half of what you owe:

$100

$50

$25

$12.50

...

$0.00009536 <- that may seem like you're done paying, but to your while loop, thats still satisfies b1 > 0.

You have two options: 1. you can change the rules so the minimum payment is 2% or $20, whichever is greater. I think that's what credit cards actually do, btw

  1. You can stop when your balance gets to be less than 1 cent.
pseudopeach
  • 1,475
  • 14
  • 29