Click to See Complete Forum and Search --> : Out of stack space error.


planetsaturn
12-18-2002, 04:21 AM
I am a super newbie to Javascript. I have only written a couple poorly coded scripts, but hey they work atleast. Anyhow I wrote a script tonight that calculates the amount of remaining payments on a loan or credit card balance then writes it to the screen in a nice format giving you a statement of what if payment history. I don't know how to do a loop in javascript so I just used an IF/ELSE statement that looped between functions until the principal fell under 0. This worked but I find that on larger loans the browser reports that it is out of stack space. This appears to be based of system memory. For instance in my system in 384MB I can get through 234 months of payments before "out of stack space" on a smaller laptop with 64MB I can only get to 160 months. I am sure there is nothing I can do to correct it but was wondering if anyone could maybe look at the script and see a better way to write it. Is there some other way to do a loop in Java? Keep in mind im still new so take it easy on me. anyways the script/webpage is available here:

http://www.digiclipse.com/home/calc.htm

Thanks in advance,
Eric

fluffycow
01-14-2003, 05:15 PM
Stack overflows happen when functions call one another consecutively without returning.

I turned your if statements into a loop, added a bunch of comments, changed document.write into a variable that continuosly gets larger until it writes at the end(becuase document.write is slow), and made a little peice of code that should stop people from entering numbers where the interest goes up faster than their payments can bring it down.

NOTE:(web page may kill any & nbsp you had in this, so you might have to re-insert them)

loops in javascript are the for() while() dowhile() and maybe some other I forgot

while is like this

while(math statement goes here)
{
do this stuff
}
for is

for(declare a variable;while this is true;do this stuff to eventually make it false each time it loops)
{
do stuff here
}

the two most used are while and for loops, dowhile is hardly ever seen anymore.
Look them up on google or some othe search engine, im sure you'll find some info about them

start of script:

<html>
<head>
<!-- Original Author : Eric Freeman -->
<!-- Website: www.digiclipse.com -->
<!-- Please leave this tag intact and provide link digiclipse.com -->
<SCRIPT LANGUAGE="JavaScript">
var highmax=""
var i_principal = 0
var i_payment = 0
var i_interest = 0
var i_month = 0
function setv()
{
i_principal = eval(document.myform.T1.value)
i_payment = eval(document.myform.T5.value)
i_interest = eval(document.myform.T2.value)

//(should)make it so you can't enter something where interest is higher than payment(throwing program into infinite loop)
if((((i_interest/12) /100) * i_principal)>= i_payment)
{
alert("your interest goes up faster than you pay off your loan")
return
}

i_month = 1
highmax = highmax+("<body bgcolor='#C0C0C0'>Generated: " + new Date())
add()
}

function add()
{
//used this so it would loop one extra time
//var lastrun = 0
while(i_principal >= i_payment)
{
//alert(i_principal)
//rounds pricipal to cents
i_principal = Math.round(i_principal*100)/100
//money left without this month's pay
sub_pay = i_principal - i_payment
//rounds pay to cents
sub_pay = Math.round(sub_pay*100)/100
//rounds interest to cents
i_interest = Math.round(i_interest*100) / 100
//gets interest percent per month
get_interest = sub_pay * ((i_interest / 100) / 12)
//rounds interest to cents
get_interest = Math.round(get_interest*100)/100

highmax=highmax+" <table border='1' cellpadding='0' cellspacing='0' style='border-collapse: collapse' bordercolor='#111111' width='295' id='AutoNumber1'><tr><td width='62' align='center'><b><font face='Trebuchet MS'>month</font></b></td><td width='230' align='center'><b><font face='Trebuchet MS'>schedule</font></b></td></tr><tr><td width='62' rowspan='4' align='center'><font size='7' face='Trebuchet MS'>" + i_month + "</font></td>"
highmax=highmax+"<td bgcolor='c0c0c0' width='230'><font face='Trebuchet MS'>$" + i_principal + " principal</font></td></tr>"
highmax=highmax+"<td width='230'><font face='Trebuchet MS'>-$" + i_payment + " monthly payment</font></td></tr>"
highmax=highmax+"<tr><td bgcolor='c0c0c0' width='230'><font face='Trebuchet MS'>$" + sub_pay +" new principal</font></td></tr>"
highmax=highmax+"<tr><td width='230'><font face='Trebuchet MS'>$" + (Math.round((sub_pay + get_interest)*100)/100) + " interest added</font></td></tr>"
//new cost with interest
i_principal = sub_pay + get_interest
//next month
i_month = i_month + 1
//original while loop statement
//if(i_principal < i_payment) lastrun++
}
i_principal = Math.round(i_principal*100)/100
get_interest = i_principal * (((i_interest / 100) / 12))
highmax=highmax+" <table border='1' cellpadding='0' cellspacing='0' style='border-collapse: collapse' bordercolor='#111111' width='295' id='AutoNumber1'><tr><td width='62' align='center'><b><font face='Trebuchet MS'>month</font></b></td><td width='230' align='center'><b><font face='Trebuchet MS'>schedule</font></b></td></tr><tr><td width='62' rowspan='4' align='center'><font size='7' face='Trebuchet MS'>" + i_month + "</font></td>"
highmax=highmax+"<td bgcolor='c0c0c0' width='230'><font face='Trebuchet MS'>$" + i_principal + " principal</font></td></tr>"
highmax=highmax+"<tr><td width='230'><font face='Trebuchet MS'>$" + (Math.round((i_principal + get_interest)*100)/100) + " interest added</font></td></tr>"
highmax=highmax+"<td bgcolor='c0c0c0' width='230'><font face='Trebuchet MS'>-$" + (Math.round((i_principal + get_interest)*100)/100) + " monthly payment</font></td></tr>"
highmax=highmax+"<td width='230'><font face='Trebuchet MS'>$0.00 balance</font></td></tr>"
highmax=highmax+"</table><table border='1' bordercolor='#111111' width='295'><tr><td align='center' width='230'><font face='Trebuchet MS'>COMPLETED!</font></td></tr></table></BODY>"
document.write(highmax)
}
</script>
</head>
<body bgcolor="#C0C0C0">
<form name="myform">
<table border="2" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="350" id="AutoNumber1">
<tr>
<td width="100%" bgcolor="#000000" align="center"><b>
<font color="#FFFFFF" face="Trebuchet MS">Debt
Calculator/Planner</font></b></td>
</tr>
<tr>
<td width="100%"><font face="Trebuchet MS"><b>principal<br>
<input type="text" name="T1" size="20" style="color: #000000; border: 2px solid #000000; background-color: #808080"><br>
payment amount<br>
<input type="text" name="T5" size="20" style="color: #000000; border: 2px solid #000000; background-color: #808080"><br>
interest rate</b></font><br>
<input type="text" name="T2" size="20" style="color: #000000; border: 2px solid #000000; background-color: #808080"><br>
<input type="button" value="calculate" onclick="setv()" name="B1" style="font-family: Trebuchet MS; font-size: 10pt; color: #000000; font-weight: bold; border: 2px solid #000000; background-color: #C0C0C0"></td>
</tr>
<tr>
<td width="100%">
<p align="center"><br>
<font face="Trebuchet MS" size="2">- Have you ever wondered how
long it would take you to pay off a credit card making the
minimum payments? <br>
- Do you know how much of an impact paying a few extra dollars
each payment can really make?<br>
<br>
Simply plug in the following information above and an in-depth
statement of &quot;What-if&quot; payment plans will be calculated.</font></p>
<p align="center"><font face="Trebuchet MS" size="2">** This
script is limited to the amount of System Ram you have in your
computer. Some large or high interest loans will not be possible
to completely calculate. ***</font></td>
</tr>
</table>
<p>&nbsp;</p>
</p>
</form>
</body>
</html>

planetsaturn
01-15-2003, 12:45 PM
I'll have to try this out later. Thanks for the help. Do you have a website/name? I'd like to add you to the script comments.

Eric

fluffycow
01-15-2003, 05:46 PM
awww thanks, I don't have a website though, You can just leave me in there as fluffycow.

The script should work, if it doesn't go ahead and tell me about it.