DCrawshawJr Posted August 11, 2015 Share Posted August 11, 2015 Hello everyone! I need some help with a program I wrote in JavaScript. It is designed to tell you how many months you have to pay off your loans, given your balance, interest rate, and monthly payment of up to ten loans. It shows you the number of months it will take to pay off the loans with regular payments, and with the debt snowball method. I'm trying to convert this from a program I wrote in FreeBasic, but I'm having some trouble getting it to work. Some code was borrowed from another guy, such as the beginning tags, but the actual program idea is mine. Thanks! Here's the code (warning: long): <!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”><html xmlns=”http://www.w3.org/1999/xhtml”><head><meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″ /><title>Loan Calculator</title></head><body><div><form><table border="1" align="center" cellspacing="1" cellpadding="1"><tr><td class="label">Loan Name</td><td class="label">Balance</td><td class="label">Interest</td><td class="label">Payment</td></tr><tr><td class="field"><input id="loan0" class="field" name="loan0" type="text" /></td><td class="field"><input id="balance0" class="field" name="balance0" type="text" /></td><td class="field"><input id="interest0" class="field" name="interest0" type="text" /></td><td class="field"><input id="payment0" class="field" name="payment0" type="text" /></td></tr><tr><td class="field"><input id="loan1" class="field" name="loan1" type="text" /></td><td class="field"><input id="balance1" class="field" name="balance1" type="text" /></td><td class="field"><input id="interest1" class="field" name="interest1" type="text" /></td><td class="field"><input id="payment1" class="field" name="payment1" type="text" /></td></tr><tr><td class="field"><input id="loan2" class="field" name="loan2" type="text" /></td><td class="field"><input id="balance2" class="field" name="balance2" type="text" /></td><td class="field"><input id="interest2" class="field" name="interest2" type="text" /></td><td class="field"><input id="payment2" class="field" name="payment2" type="text" /></td></tr><tr><td class="field"><input id="loan3" class="field" name="loan3" type="text" /></td><td class="field"><input id="balance3" class="field" name="balance3" type="text" /></td><td class="field"><input id="interest3" class="field" name="interest3" type="text" /></td><td class="field"><input id="payment3" class="field" name="payment3" type="text" /></td></tr><tr><td class="field"><input id="loan4" class="field" name="loan4" type="text" /></td><td class="field"><input id="balance4" class="field" name="balance4" type="text" /></td><td class="field"><input id="interest4" class="field" name="interest4" type="text" /></td><td class="field"><input id="payment4" class="field" name="payment4" type="text" /></td></tr><tr><td class="field"><input id="loan5" class="field" name="loan5" type="text" /></td><td class="field"><input id="balance5" class="field" name="balance5" type="text" /></td><td class="field"><input id="interest5" class="field" name="interest5" type="text" /></td><td class="field"><input id="payment5" class="field" name="payment5" type="text" /></td></tr><tr><td class="field"><input id="loan6" class="field" name="loan6" type="text" /></td><td class="field"><input id="balance6" class="field" name="balance6" type="text" /></td><td class="field"><input id="interest6" class="field" name="interest6" type="text" /></td><td class="field"><input id="payment6" class="field" name="payment6" type="text" /></td></tr><tr><td class="field"><input id="loan7" class="field" name="loan7" type="text" /></td><td class="field"><input id="balance7" class="field" name="balance7" type="text" /></td><td class="field"><input id="interest7" class="field" name="interest7" type="text" /></td><td class="field"><input id="payment7" class="field" name="payment7" type="text" /></td></tr><tr><td class="field"><input id="loan8" class="field" name="loan8" type="text" /></td><td class="field"><input id="balance8" class="field" name="balance8" type="text" /></td><td class="field"><input id="interest8" class="field" name="interest8" type="text" /></td><td class="field"><input id="payment8" class="field" name="payment8" type="text" /></td></tr><tr><td class="field"><input id="loan9" class="field" name="loan9" type="text" /></td><td class="field"><input id="balance9" class="field" name="balance9" type="text" /></td><td class="field"><input id="interest9" class="field" name="interest9" type="text" /></td><td class="field"><input id="payment9" class="field" name="payment9" type="text" /></td></tr><tr> <td colspan="2" class="calculate"><button id="calc" class="btn" onclick="return false">Calculate</button></td><td class="result_r"><input id="result_r" class="result" name="result_r" type="text" value="Normal"/><td class="result_s"><input id="result_s" class="result" name="result_s" type="text" value="Snowball"/></tr></table></form><script type="text/javascript">$("#calc").click(function()) //If the Calculate button is clicked...{//Establish variables.var balance = [0,0,0,0,0,0,0,0,0,0];var interest = [0,0,0,0,0,0,0,0,0,0];var payment = [0,0,0,0,0,0,0,0,0,0];var sbalance = [0,0,0,0,0,0,0,0,0,0];var sinterest = [0,0,0,0,0,0,0,0,0,0];var spayment = [0,0,0,0,0,0,0,0,0,0];var rbalance = [0,0,0,0,0,0,0,0,0,0];var rinterest = [0,0,0,0,0,0,0,0,0,0];var rpayment = [0,0,0,0,0,0,0,0,0,0];var loantotal, oldtotal,difference,loancounter;var snowball,snowballamt,snowballbal;var s_total,s_months;var r_total,r_months;//Turn the number fields into numbers.for (a = 0;a <= 9; a++){balance[a] = parseFloat($("#balance"+a.toSring()).val());interest[a] = parseFloat($("#interest"+a.toString()).val());payment[a] = parseFloat($("#payment"+a.toString()).val());}//Establish a loan total for calculation.loantotal = 0;for (a = 0;a <= 9; a++){loantotal += balance[a];}do{for (a = 0;a <=9; a++){//Plug in the balances, interests, and payments for snowball and regular calculation.sbalance[a] = balance[a];sinterest[a] = interest[a];spayment[a] = payment[a];rbalance[a] = balance[a];rinterest[a] = interest[a];rpayment[a] = payment[a];}//Zero the snowball acct. #, and # of months indicators.snowball = 0;s_months = 0; r_months = 0;do{snowballamt = 0; //Reset current snowball payment amount.snowballbal = 999999; //Reset target balance.for (a = 0;a <=9; a++) //Check to see if any loans are paid off.{spayment[a] = payment[a]; //Reset the regular payment for a loan.if (sbalance[a] <= 0) //If there's no balance, {snowballamt += spayment[a]; //Transfer what you would have paid to the snowball payment,spayment[a] = 0; //, and clear the regular payment.}} if (snowballamt > 0) //If there's money in the snowball...{for (a = 0;a<=9;a++){if (sbalance[a] <= snowballbal && sbalance[a] > 0) //Find the loan with the lowest balance...{snowballbal = sbalance[a]; //Make that the new target balance...snowball = a; //And make that loan subject to the snowball.}}}spayment[snowball] += snowballamt; //Add the snowball amount to the regular payments of the snowball loan.for (a = 0;a<= 9; a++) {sbalance[a] -= spayment[a]; //Now, make a payment.sbalance[a] *= 1+(sinterest[a] /12); //Charge interest.if (sbalance[a] < 0 ) //Zero any loans that have a credit balance.{sbalance[a] = 0; }}s_months += 1; //Add a month to the snowball month counter.s_total = 0; //Reset the total balance.for (a = 0;a <= 9;a++){s_total += sbalance[a]; //Check for a balance.}}while (s_total > 0 && s_months < 999); //Keep going while there's a balance and 999 months haven't passed.do //Now, the regular calculation.{for (a = 0;a<=9:a++) //Make a payment and charge interest as before.{rbalance[a] -= rpayment[a];rbalance[a] *= 1+(rinterest[a]/12);if (rbalance[a] < 0){rbalance[a] = 0;}}r_months += 1; //Add a month as before.r_total = 0; //Reset that balance too.for (a = 0;a <= 9; a++){r_total += rbalance[a]; //Check for a balance, as before.}}while (r_total > 0 && r_months < 999); //Keep going while there's a balance and 999 months haven't passed.}if(!isNaN(result_r)) {$("#result_r").val(r_months);} //Display the results.if(!isNaN(result_s)) {$("#result_s").val(s_months);}}</script></body></html> Link to comment Share on other sites More sharing options...
justsomeguy Posted August 11, 2015 Share Posted August 11, 2015 What's the actual problem? Make sure you're checking the error console for error messages. It looks like you're trying to use jQuery to attach a click event, but I don't see jQuery being included on the page. Since that's the only place you try to use jQuery it's probably better to attach that click event normally. Link to comment Share on other sites More sharing options...
davej Posted August 11, 2015 Share Posted August 11, 2015 He only uses jQuery in a few places. He has syntax errors scattered here and there and is missing the while condition for the main do loop.. Link to comment Share on other sites More sharing options...
DCrawshawJr Posted August 12, 2015 Author Share Posted August 12, 2015 I used Notepad++ in part of it, and I'm not sure how to check for errors. I did get rid of the main do loop. Now, I was mainly guessing with this code: balance[a] = parseFloat($("#balance"+a.toSring()).val());interest[a] = parseFloat($("#interest"+a.toString()).val());payment[a] = parseFloat($("#payment"+a.toString()).val()); I'm thinking that's where some of the syntax errors are. What I was trying to do was, within the for loop, plug in the number version of the string texts onto the variables. Anything different I can do? Also, how would I return the r_months variable and s_months variable on the actual HTML upon clicking "Calculate" without JQuery? Thanks again. Link to comment Share on other sites More sharing options...
davej Posted August 12, 2015 Share Posted August 12, 2015 If you use the browser debugging tools the syntax errors are displayed one by one. Which browser are you using? I have looked at the code some, but I don't really understand your logical approach. Also you do not seem to declare the variable 'a'. I did play with some code to generate a somewhat similar program... <!DOCTYPE><html><head><meta charset='utf-8'><title>Loan Calculator</title><script>window.onerror = function(a,b,c,d){alert('Javascript Error: '+a+'nURL: '+b+'nLine Number: '+c+' Col Number: '+d);return true;}</script><style>table,th,td {border:1px solid #aaa}#wrap{width:500px;margin:0 auto}</style><script>'use strict';window.onload = function(){ document.getElementById('calc').onclick = calc; document.getElementById('add').onclick = addrow;}function calc(){ document.getElementById('out').innerHTML = ''; var paidoff = []; var name = []; var balance = []; var rate = []; var payment = []; var j; var bal0; var rate0; var pay0; var cnt; var table = document.getElementsByTagName('TABLE')[0]; // get first table var list = table.getElementsByTagName('INPUT'); j = 0; for (var i=3 ; i<list.length ; i+=4){ if (list[i].type = 'text'){ if (list[i-3].value == '***INVALID***'){ list[i-3].value = ''; } if (list[i-3].value.trim().length > 0){ name[j] = list[i-3].value.trim(); }else{ name[j] = 'Loan' + j; list[i-3].value = name[j]; } bal0 = Number(list[i-2].value); rate0 = Number(list[i-1].value);//alert('balance='+bal0+' rate='+rate0); if (rate0<1.0 && rate0>0){ // fix rate formats 0.015 = 1.5% rate0 = rate0*100; list[i-1].value = rate0; } pay0 = Number(list[i].value);//alert('payment='+pay0); if (bal0>0 && rate0>=0 && pay0>=0){ balance[j] = bal0; rate[j] = rate0; payment[j] = pay0; paidoff[j] = false; j++; }else{ list[i-3].value = '***INVALID***'; } } } cnt = j; var mo = 1; var remaining = cnt; do{ for (var i=0 ; i<cnt ; i++){ if(paidoff[i] == false){ balance[i] = balance[i] + balance[i] * (rate[i]/1200) - payment[i]; //alert('balance['+i+']='+balance[i]); if (balance[i] <= 0){ document.getElementById('out').innerHTML += '<br/>' + name[i] + ' was paid off in ' + mo + ' months.'; paidoff[i] = true; remaining--; } } } mo++; }while(mo<12*100 && remaining>0); for(var i=0; i<cnt ; i++){ if(paidoff[i] == false){ document.getElementById('out').innerHTML += '<br/>' + name[i] + ' was never paid off.'; } }}// end of calcfunction addrow(){ var table = document.getElementsByTagName('TABLE')[0]; // get first table var tr = document.createElement('TR'); var td = document.createElement('TD'); var inp = document.createElement('INPUT'); inp.setAttribute('type', 'text'); td.appendChild(inp); tr.appendChild(td); td = document.createElement('TD'); inp = document.createElement('INPUT'); inp.setAttribute('type', 'text'); td.appendChild(inp); tr.appendChild(td); td = document.createElement('TD'); inp = document.createElement('INPUT'); inp.setAttribute('type', 'text'); td.appendChild(inp); tr.appendChild(td); td = document.createElement('TD'); inp = document.createElement('INPUT'); inp.setAttribute('type', 'text'); td.appendChild(inp); tr.appendChild(td); table.appendChild(tr);} // end of addrow</script></head><body><div id="wrap"><table><tr><th>Loan Name</th><th>Balance</th><th>Interest</th><th>Payment</th></tr><tr><td><input type="text" /></td><td><input type="text" /></td><td><input type="text" /></td><td><input type="text" /></td></tr></table><input type="button" id="add" value="Add Row"/><input type="button" id="calc" value="Calculate"/><div id="out"></div></div></body></html> Link to comment Share on other sites More sharing options...
DCrawshawJr Posted August 12, 2015 Author Share Posted August 12, 2015 (edited) I'm using Chrome...and I've got some studying to do. The regular debt payment is self-explanatory as shown in your version. The logical approach I was taking in the debt snowball method was such: 1. Clear the snowball variable, and reset the payments. 2. Find any loans that are paid off in the previous loop. 3. Transfer each of those payments to the snowball variable. 4. Transfer the totals to the next lowest loan. 5. Make a payment. 6. Charge interest. 7. Repeat until all loans are paid off or this loop repeated 999 times. As for the "a" variable, I wasn't aware that I had to declare that in a for loop. Like I said, I've got some studying to do with your version. Thanks! Edited August 12, 2015 by DCrawshawJr Link to comment Share on other sites More sharing options...
davej Posted August 12, 2015 Share Posted August 12, 2015 All variables should be declared. I do not see 'a' declared in your code. There are several possible approaches. You could pay off the loans that have the highest interest rates, or the loans that have the highest total interest paid, or some other scheme. Link to comment Share on other sites More sharing options...
DCrawshawJr Posted August 13, 2015 Author Share Posted August 13, 2015 (edited) Here's my latest attempt: <!DOCTYPE><html><head> <meta charset='utf-8'> <title>Loan Calculator Attempt 2</title> <style> table,th,td {border:1px solid #aaa} #wrap{width:500px;margin:0 auto} </style> <script> 'use strict'; window.onload = function() { document.getElementById('calc').onclick = calc; //Whenever you click the id 'calc' button, call the calc function. } function calc() { var loan = ['','','','','','','','','','']; //Loan names (for console) var balance = [0,0,0,0,0,0,0,0,0,0]; //Original balance. var interest = [0,0,0,0,0,0,0,0,0,0]; //Original interest rate. var payment = [0,0,0,0,0,0,0,0,0,0]; //Original monthly payment. var s_loan; //Loan # that gets the snowball money. var s_amount; //Amount of snowball money. var s_refbalance; //Lowest balance of loan in determining who gets the snowball money. var s_balance = [0,0,0,0,0,0,0,0,0,0]; //Balance used in snowball calculation. var s_interest = [0,0,0,0,0,0,0,0,0,0]; //Interest rate used in snowball calculation. var s_payment = [0,0,0,0,0,0,0,0,0,0]; //Monthly payment used in snowball calculation. var s_months = 0; //# of months in the snowball calculation. var s_total; //Total current balance in snowball calculation. var r_balance = [0,0,0,0,0,0,0,0,0,0]; //Balance used in regular calculation. var r_interest = [0,0,0,0,0,0,0,0,0,0]; //Interest rate used in regular calculation. var r_payment = [0,0,0,0,0,0,0,0,0,0]; //Monthly payment used in regular calculation. var r_months = 0; //# of months in the regular calculation. var r_total; //Total current balance in regular calculation. var table = document.getElementsByTagName('TABLE')[0]; var list = table.getElementsByTagName('INPUT'); for (var a = 0; a <= 39; a+= 4) { loan[a] = list[a].value; if (loan[a].value = '') { loan[a].value = 'Loan '+ a; } balance[a] = Number(list[a+1].value); interest[a] = Number(list[a+2].value) / 100; payment[a] = Number(list[a+3].value); s_balance[a] = balance[a]; r_balance[a] = balance[a]; s_interest[a] = interest[a]; r_interest[a] = balance[a]; s_payment[a] = payment[a]; r_payment[a] = balance[a]; } //Snowball payment calculation console.log ('SNOWBALL PAYMENT CALCULATION n'); do { console.log('Months elapsed:' + s_months + 'n'); s_amount = 0; //Reset current snowball amount. s_refbalance = 999999; //Reset reference balance. (I hope you don't owe millions...) //Build the snowball. for (var a = 0; a <= 9; a++) { s_payment[a] = payment[a]; //Reset the regular payment for a loan. if (s_balance[a] <= 0) //If there's no balance... { s_amount += s_payment[a]; //...Transfer what you would have paid to the snowball payment, s_payment[a] = 0; //and clear the payment. } } console.log('Snowball amount: $'+s_amount.toFixed(2)+ 'n'); //Find the lowest balance. if (s_amount > 0) //If there's money in the snowball... { for (var a = 0; a <= 9; a++) { if (s_balance[a] <= s_refbalance && s_balance[a] > 0) //If Loan a is lower than the reference balance... { s_refbalance = s_balance[a]; //...it becomes the new reference balance... s_loan = a; //and the loan # is marked. } } } console.log('Lowest balance: $'+s_refbalance.toFixed(2) + 'n'); console.log(loan[s_loan] + ' gets the snowball money. n'); s_payment[s_loan] = s_refbalance; //Add the snowball money to the 'winning' loan at this time. //Make a payment and charge interest. for (var a = 0; a <= 9; a++) { s_balance[a] -= s_payment[a]; //Make a payment. s_balance[a] *= 1 + (s_interest[a] / 12); //Charge interest. if (s_balance[a] <= 0) {s_balance = 0;} //Zero any credit balances. } s_months++; //Add a month to the counter. //Recheck the total balance. s_total = 0; for (var a = 0; a <= 9; a++) {s_total += s_balance[a];} console.log('Total balance after ' + s_months+ ' month(s) : $' + s_total.toFixed(2)+ 'n'); } while (s_total > 0 && s_months < 1200) document.getElementById('snowball').innerHTML = s_months; //Regular payment calculation. console.log('REGULAR PAYMENT CALCULATION n'); do { for (var a = 0; a <= 9; a++) { r_balance[a] -= r_payment[a]; r_balance[a] *= 1 + (r_interest[a] / 12); if (r_balance[a] <= 0) {r_balance = 0;} } r_months++; //Recheck the total balance. r_total = 0; for (var a = 0; a <= 9; a++) {r_total += r_balance[a];} console.log('Total balance after ' + r_months+ ' month(s) : $' + r_total.toFixed(2)+ 'n'); } while (r_total > 0 && r_months < 1200) document.getElementById('regular').innerHTML = r_months; } </script></head><body> <table> <tr> <th>Loan Name</th> <th>Balance</th> <th>Interest</th> <th>Payment</th> </tr> <tr> <td><input type="text" /></td> <td><input type="text" /></td> <td><input type="text" /></td> <td><input type="text" /></td> </tr> <tr> <td><input type="text" /></td> <td><input type="text" /></td> <td><input type="text" /></td> <td><input type="text" /></td> </tr> <tr> <td><input type="text" /></td> <td><input type="text" /></td> <td><input type="text" /></td> <td><input type="text" /></td> </tr> <tr> <td><input type="text" /></td> <td><input type="text" /></td> <td><input type="text" /></td> <td><input type="text" /></td> </tr> <tr> <td><input type="text" /></td> <td><input type="text" /></td> <td><input type="text" /></td> <td><input type="text" /></td> </tr> <tr> <td><input type="text" /></td> <td><input type="text" /></td> <td><input type="text" /></td> <td><input type="text" /></td> </tr> <tr> <td><input type="text" /></td> <td><input type="text" /></td> <td><input type="text" /></td> <td><input type="text" /></td> </tr> <tr> <td><input type="text" /></td> <td><input type="text" /></td> <td><input type="text" /></td> <td><input type="text" /></td> </tr> <tr> <td><input type="text" /></td> <td><input type="text" /></td> <td><input type="text" /></td> <td><input type="text" /></td> </tr> <tr> <td><input type="text" /></td> <td><input type="text" /></td> <td><input type="text" /></td> <td><input type="text" /></td> </tr> <tr> <p align="center"><td colspan = "2"><input type="button" id="calc" value="Calculate" /></td></p> <td><div id="regular"></div> <td><div id="snowball"></div> </tr> <tr> </table> <p align="center"><div id="out"></div></p> </body></html> I've included some markers for the console, and I found out that I'm not even getting the form text plugged into the variables. I think it's coming from this piece of code: for (var a = 0; a <= 39; a+= 4) { loan[a] = list[a].value; if (loan[a].value = '') { loan[a].value = 'Loan '+ a; } balance[a] = Number(list[a+1].value); interest[a] = Number(list[a+2].value) / 100; payment[a] = Number(list[a+3].value); s_balance[a] = balance[a]; r_balance[a] = balance[a]; s_interest[a] = interest[a]; r_interest[a] = balance[a]; s_payment[a] = payment[a]; r_payment[a] = balance[a]; } The snowball method I'm using is based on lowest balance. Plus, while I know it's not good programming style, I'm having a fixed number of accounts for the sake of simplicity, so I could just get the bare bones code to work. Edited August 13, 2015 by DCrawshawJr Link to comment Share on other sites More sharing options...
DCrawshawJr Posted August 13, 2015 Author Share Posted August 13, 2015 Wait...scratch that previous post. It was the wrong snippet of code. And the getElementsbyTagName does work, I found out. Still working on it... Link to comment Share on other sites More sharing options...
DCrawshawJr Posted August 13, 2015 Author Share Posted August 13, 2015 Oh...now I see the problem. Every fourth account is being read. Continuing to debug... Link to comment Share on other sites More sharing options...
DCrawshawJr Posted August 13, 2015 Author Share Posted August 13, 2015 I think I got it now: <!-- Loan Calculator by Douglas Crawshaw Jr. 8-12-2015 9:38PM--><!DOCTYPE><html><head> <meta charset='utf-8'> <title>Loan Calculator Attempt 2</title> <style> table,th,td {border:1px solid #aaa} #wrap{width:500px;margin:0 auto} </style> <script> 'use strict'; window.onload = function() { document.getElementById('calc').onclick = calc; //Whenever you click the id 'calc' button, call the calc function. document.getElementById('clear').onclick = clear; } function clear() { var g0 = document.getElementById("f0"); var g1 = document.getElementById("f1"); var g2 = document.getElementById("f2"); var g3 = document.getElementById("f3"); var g4 = document.getElementById("f4"); var g5 = document.getElementById("f5"); var g6 = document.getElementById("f6"); var g7 = document.getElementById("f7"); var g8 = document.getElementById("f8"); var g9 = document.getElementById("f9"); var g10 = document.getElementById("f10"); var g11 = document.getElementById("f11"); var g12 = document.getElementById("f12"); var g13 = document.getElementById("f13"); var g14 = document.getElementById("f14"); var g15 = document.getElementById("f15"); var g16 = document.getElementById("f16"); var g17 = document.getElementById("f17"); var g18 = document.getElementById("f18"); var g19 = document.getElementById("f19"); var g20 = document.getElementById("f20"); var g21 = document.getElementById("f21"); var g22 = document.getElementById("f22"); var g23 = document.getElementById("f23"); var g24 = document.getElementById("f24"); var g25 = document.getElementById("f25"); var g26 = document.getElementById("f26"); var g27 = document.getElementById("f27"); var g28 = document.getElementById("f28"); var g29 = document.getElementById("f29"); var g30 = document.getElementById("f30"); var g31 = document.getElementById("f31"); var g32 = document.getElementById("f32"); var g33 = document.getElementById("f33"); var g34 = document.getElementById("f34"); var g35 = document.getElementById("f35"); var g36 = document.getElementById("f36"); var g37 = document.getElementById("f37"); var g38 = document.getElementById("f38"); var g39 = document.getElementById("f39"); g0.value = ''; g10.value = ''; g20.value = ''; g30.value = ''; g1.value = ''; g11.value = ''; g21.value = ''; g31.value = ''; g2.value = ''; g12.value = ''; g22.value = ''; g32.value = ''; g3.value = ''; g13.value = ''; g23.value = ''; g33.value = ''; g4.value = ''; g14.value = ''; g24.value = ''; g34.value = ''; g5.value = ''; g15.value = ''; g25.value = ''; g35.value = ''; g6.value = ''; g16.value = ''; g26.value = ''; g36.value = ''; g7.value = ''; g17.value = ''; g27.value = ''; g37.value = ''; g8.value = ''; g18.value = ''; g28.value = ''; g38.value = ''; g9.value = ''; g19.value = ''; g29.value = ''; g39.value = ''; } function calc() { var loan = ['','','','','','','','','','']; //Loan names (for console) var balance = [0,0,0,0,0,0,0,0,0,0]; //Original balance. var interest = [0,0,0,0,0,0,0,0,0,0]; //Original interest rate. var payment = [0,0,0,0,0,0,0,0,0,0]; //Original monthly payment. var s_loan; //Loan # that gets the snowball money. var s_amount; //Amount of snowball money. var s_refbalance; //Lowest balance of loan in determining who gets the snowball money. var s_balance = [0,0,0,0,0,0,0,0,0,0]; //Balance used in snowball calculation. var s_interest = [0,0,0,0,0,0,0,0,0,0]; //Interest rate used in snowball calculation. var s_payment = [0,0,0,0,0,0,0,0,0,0]; //Monthly payment used in snowball calculation. var s_months = 0; //# of months in the snowball calculation. var s_total; //Total current balance in snowball calculation. var r_balance = [0,0,0,0,0,0,0,0,0,0]; //Balance used in regular calculation. var r_interest = [0,0,0,0,0,0,0,0,0,0]; //Interest rate used in regular calculation. var r_payment = [0,0,0,0,0,0,0,0,0,0]; //Monthly payment used in regular calculation. var r_months = 0; //# of months in the regular calculation. var r_total; //Total current balance in regular calculation. var table = document.getElementsByTagName('TABLE')[0]; var list = table.getElementsByTagName('INPUT'); for (var a = 0; a <= 9; a++) { //loan[a] = list[a].value; //if (loan[a].value = '') //{ // loan[a].value = 'Loan '+ a; //} balance[a] = Number(list[(a*4)+1].value); console.log(balance[a]); interest[a] = Number(list[(a*4)+2].value) / 100; console.log(interest[a]); payment[a] = Number(list[(a*4)+3].value); console.log(payment[a]); s_balance[a] = balance[a]; r_balance[a] = balance[a]; s_interest[a] = interest[a]; r_interest[a] = interest[a]; s_payment[a] = payment[a]; r_payment[a] = payment[a]; } //Snowball payment calculation console.log ('SNOWBALL PAYMENT CALCULATION n'); do { console.log('Months elapsed:' + s_months + 'n'); s_amount = 0; //Reset current snowball amount. s_refbalance = 999999; //Reset reference balance. (I hope you don't owe millions...) //Build the snowball. for (var a = 0; a <= 9; a++) { s_payment[a] = payment[a]; //Reset the regular payment for a loan. console.log('S Balance '+ a+ "= "+s_balance[a]+"n"); if (s_balance[a] <= 0) //If there's no balance... { s_amount += s_payment[a]; //...Transfer what you would have paid to the snowball payment, s_payment[a] = 0; //and clear the payment. } } console.log('Snowball amount: $'+s_amount.toFixed(2)+ 'n'); //Find the lowest balance. if (s_amount > 0) //If there's money in the snowball... { for (var a = 0; a <= 9; a++) { if (s_balance[a] <= s_refbalance && s_balance[a] > 0) //If Loan a is lower than the reference balance... { s_refbalance = s_balance[a]; //...it becomes the new reference balance... s_loan = a; //and the loan # is marked. } } } console.log('Lowest balance: $'+s_refbalance.toFixed(2) + 'n'); console.log(loan[s_loan] + ' gets the snowball money. n'); s_payment[s_loan] += s_amount; //Add the snowball money to the 'winning' loan at this time. //Make a payment and charge interest. for (var a = 0; a <= 9; a++) { //if (s_balance[a] > 0) //{ s_balance[a] -= s_payment[a]; //Make a payment. console.log('New balance: $'+s_balance[a]+' n'); s_balance[a] *= 1 + (s_interest[a] / 12); //Charge interest. console.log('New balance: $'+s_balance[a]+' n'); if (s_balance[a] <= 0) {s_balance[a] = 0;} //Zero any credit balances. //} } s_months++; //Add a month to the counter. //Recheck the total balance. s_total = 0; for (var a = 0; a <= 9; a++) { s_total += s_balance[a]; console.log(s_total + 'n'); } console.log('Total balance after ' + s_months+ ' month(s) : $' + s_total.toFixed(2)+ 'n'); } while (s_total > 0 && s_months < 1200) document.getElementById('snowball').innerHTML = s_months; //Regular payment calculation. console.log('REGULAR PAYMENT CALCULATION n'); for (var a = 0; a <= 9; a++) { console.log (a + ' : ' + r_balance[a] + 'n'); } do { for (var a = 0; a <= 9; a++) { //if (r_balance[a] > 0) //{ r_balance[a] -= r_payment[a]; console.log('New balance: $'+r_balance[a]+' n'); r_balance[a] *= 1 + (r_interest[a] / 12); console.log('New balance: $'+r_balance[a]+' n'); if (r_balance[a] <= 0) {r_balance[a] = 0;} //} } r_months++; //Recheck the total balance. r_total = 0; for (var a = 0; a <= 9; a++) {r_total += r_balance[a];} console.log('Total balance after ' + r_months+ ' month(s) : $' + r_total.toFixed(2)+ 'n'); } while (r_total > 0 && r_months < 1200) document.getElementById('regular').innerHTML = r_months; } </script></head><body> <form name = "box"> <table> <tr> <th>Loan Name</th> <th>Balance</th> <th>Interest</th> <th>Payment</th> </tr> <tr> <td><input id="f0" type="text" /></td> <td><input id="f1" type="text" /></td> <td><input id="f2" type="text" /></td> <td><input id="f3" type="text" /></td> </tr> <tr> <td><input id="f4" type="text" /></td> <td><input id="f5" type="text" /></td> <td><input id="f6" type="text" /></td> <td><input id="f7" type="text" /></td> </tr> <tr> <td><input id="f8" type="text" /></td> <td><input id="f9" type="text" /></td> <td><input id="f10" type="text" /></td> <td><input id="f11"" type="text" /></td> </tr> <tr> <td><input id="f12" type="text" /></td> <td><input id="f13" type="text" /></td> <td><input id="f14" type="text" /></td> <td><input id="f15" type="text" /></td> </tr> <tr> <td><input id="f16" type="text" /></td> <td><input id="f17" type="text" /></td> <td><input id="f18" type="text" /></td> <td><input id="f19" type="text" /></td> </tr> <tr> <td><input id="f20" type="text" /></td> <td><input id="f21" type="text" /></td> <td><input id="f22" type="text" /></td> <td><input id="f23" type="text" /></td> </tr> <tr> <td><input id="f24" type="text" /></td> <td><input id="f25" type="text" /></td> <td><input id="f26" type="text" /></td> <td><input id="f27" type="text" /></td> </tr> <tr> <td><input id="f28" type="text" /></td> <td><input id="f29" type="text" /></td> <td><input id="f30" type="text" /></td> <td><input id="f31" type="text" /></td> </tr> <tr> <td><input id="f32" type="text" /></td> <td><input id="f33" type="text" /></td> <td><input id="f34" type="text" /></td> <td><input id="f35" type="text" /></td> </tr> <tr> <td><input id="f36" type="text" /></td> <td><input id="f37" type="text" /></td> <td><input id="f38" type="text" /></td> <td><input id="f39" type="text" /></td> </tr> <tr> <td><input type="button" id="calc" value="Calculate" /></td></p> <td><input type="button" id="clear" value="Clear" /></td></p> <td><div id="regular"></div></td> <td><div id="snowball"></div></td> </tr> <tr> </table> </form> <p align="center"><div id="out"></div></p> </body></html> Link to comment Share on other sites More sharing options...
davej Posted August 13, 2015 Share Posted August 13, 2015 OK, but I'm sure you can see that g0-g39 would be easier to work with as one or more arrays. Link to comment Share on other sites More sharing options...
DCrawshawJr Posted August 13, 2015 Author Share Posted August 13, 2015 Yeah, that was something I tried to figure out. So, if on my INPUT tags, I had the same id value, how would I be able to use the getElementbyId method to be able to clear the fields all at once? Link to comment Share on other sites More sharing options...
davej Posted August 13, 2015 Share Posted August 13, 2015 Every id on a page must be unique. But to clear them you could simply give the table an id. var tab = document.getElementById('mytableid');var list = tab.getElementsByTagName('INPUT');for (var i=0 ; i<list.length ; i++){ if (list[i].type=='text'){ list[i].value = ''; }} ---last edit 4:14pm cdt Aug 13--- 1 Link to comment Share on other sites More sharing options...
DCrawshawJr Posted August 16, 2015 Author Share Posted August 16, 2015 Hey, thanks a bunch for all your help! Link to comment Share on other sites More sharing options...
dsonesuk Posted August 16, 2015 Share Posted August 16, 2015 (edited) It would be better to use form collections .elements http://www.w3schools.com/js/js_htmldom_elements.asp you don't have to rely on specific form tagname each time to loop through, it will loop through all the form input elements, you just have to specify the type be it text, textarea, checkbox, radio whatever OR even other attributes like name to target and adjust how you want.http://www.w3schools.com/js/tryit.asp?filename=tryjs_dom_form_elements Edited August 16, 2015 by dsonesuk Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now