Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Jan 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    i have made my calculator but does not function

    could someone let me where i am going wrong?
    thank you
    Code:
    <script language="javascript" type="text/javascript">
    <!-- Begin
    function Loan() {
    var loanAmount;
    var IR;
    var years;
    var monthPay;
    var month;
    switch(document.calcform.startm.value) {
    case "january":
    document.calcform.startm.value="0";
    parseInt(document.calcform.startm.value)
    break;
    case "february":
    document.calcform.startm.value="1";
    parseInt(document.calcform.startm.value)
    break;
    case "march":
    document.calcform.startm.value="2";
    parseInt(document.calcform.startm.value);
    break;
    case "april":
    document.calcform.startm.value="3";
    parseInt(document.calcform.startm.value);
    break;
    case "may":
    document.calcform.startm.value="4";
    parseInt(document.calcform.startm.value);
    break;
    case "june":
    document.calcform.startm.value="5";
    parseInt(document.calcform.startm.value);
    break;
    case "july":
    document.calcform.startm.value="6";
    parseInt(document.calcform.startm.value);
    break;
    case "august":
    document.calcform.startm.value="7";
    parseInt(document.calcform.startm.value);
    break;
    case "september":
    document.calcform.startm.value="8";
    parseInt(document.calcform.startm.value);
    break;
    case "october":
    document.calcform.startm.value="9";
    parseInt(document.calcform.startm.value);
    break;
    case "november":
    document.calcform.startm.value="10";
    parseInt(document.calcform.startm.value);
    break;
    case "december":
    document.calcform.startm.value="11";
    parseInt(document.calcform.startm.value);
    break;
    default:
    document.calcform.startm.value="0";
    parseInt(document.calcform.startm.value);
    break;
    }
    loanAmount = parseFloat(document.calcform.price.value);
    IR = parseFloat(document.calcform.interest.value);
    years = parseFloat(document.calcform.time.value);
    loanAmount = loanAmount + int;
    month = years * 12;
    interestRate = IR / 12;
    negmonth =- 1 * month;
    var bottom = 1 - (Math.pow(interestRate+1,negmonth));
    var top = interestRate;
    var mid = top / bottom;
    var dec;
    document.calcform.monthPay.value = (loanAmount * mid);
    monthPay = document.calcform.monthPay.value;
    document.calcform.monthPay.value = eval(monthPay);
    loanAmount = loanAmount - document.calcform.monthPay.value;
    document.calcform.month.value = month;
    }
    function convert() {
    var startmonth = new String();
    startmonth = document.calcform.startm.value;
    document.calcform.startm.value = startmonth.toLowerCase();
    }
    function changenum() {
    document.calcform.startyear.value=parseInt(document.calcform.startyear.value);
    }
    var d = new Date;
    function getFieldValue (strFieldName) {
    var strFieldValue;
    var objRegExp = new RegExp(strFieldName + "=([^&]+)","gi");
    if (objRegExp.test(location.search))
    strFieldValue = unescape(RegExp.1);
    else strFieldValue="";
    return strFieldValue;
    }
    function Currency(money1) {
    var money = new String(money1);
    var decimal = money.indexOf(".",[0]);
    var money = money.substring(0,decimal+3);
    var money1 = parseFloat(money);
    return money;
    }
    var startmonth;
    startmonth = getFieldValue("startm");
    var monthofpayment = new String;
    month = getFieldValue("month");
    monthPay = getFieldValue("monthPay");
    loanAmount = getFieldValue("price");
    Ir = getFieldValue("interest");
    Ir = Ir/12;
    year = getFieldValue("startyear")
    function check() {
    var price = document.calcform.price.value;
    if(price.indexOf(",") >= 0) {
    alert("Please do not put commas in the Price number")
       }
    }
    //  End -->
    </script>
    <center>
    <form method="calc.loan">
    <table border=0>
    <tr>
    <td valign="top">
    Anticipated Amount of Loan
    <br>
    </td>
    <td valign="top">
    
    <input name=price onblur="check()" size=10>
    </td>
    </tr>
    <tr>
    </tr>
    <tr>
    <td>
    Anticipated Interest Rate(.00 format)
    <br>
    </td>
    <td>
    <input name=interest size=10>
    <br>
    </td>
    <tr>
    <td>
    Anticipated length of loan, in years.
    <br>
    </td>
    <td>
    <input name="time" size=10>
    <br>
    </tr>
    <tr>
    <td>
    Start Date of Loan (month and year)
    </td>
    <td>
    <input name=startm onblur="convert()" size=10>
    <input name=startyear onblur="changenum()" size=10>
    </td>
    </td>
    </tr>
    <input name=month type=hidden>
    <input name=monthPay type=hidden>
    <tr>
    <td>
    <input type=submit value="Calculate Loan" onmouseover="check()" onclick="Loan()">
    </td>
    </tr>
    </table>
    </form>
    <table border=0>
    <tr>
    <td>
    Loan Price
    </td>
    <td>
    
    <script language="javascript">
    <!--
    document.write(getFieldValue("price"));
    //-->
    </script>
    </td>
    </tr>
    <td>
    Monthly Payment
    </td>
    <td>
    
    <script language="javascript">
    <!--
    var payment = getFieldValue("monthPay");
    payment=Currency(payment);
    document.write(payment);
    //-->
    </script>
    </td>
    </tr>
    <tr>
    <td>
    # of years
    </td>
    <td>
    <script language="javascript">
    <!--
    document.write(month/12);
    -->
    </script>
    </td>
    </tr>
    </table>
    <table border="1" >
    <tr>
    <th width="125">
    Month/Year
    </th>
    <th width="125">
    Interest
    </th>
    </tr>
    <script language="JavaScript">
    <!--
    var halfmonth;
    halfmonth = month / 2;
    for(y = 0; y <= month; y++) {
    var interest = loanAmount * Ir;
    var principal = monthPay - interest;
    loanAmount = loanAmount - principal;
    interest = Currency(interest);
    principal = Currency(principal);
    if(startmonth > 11) {
    startmonth = 0;
    year++;
    }
    switch(startmonth.toString()) {
    case "0":monthofpayment="January";
    break;
    case "1":monthofpayment="February";
    break;
    case "2":monthofpayment="March";
    break;
    case "3":monthofpayment="April";
    break;
    case "4":monthofpayment="May";
    break;
    case "5":monthofpayment="June";
    break;
    case "6":monthofpayment="July";
    break;
    case "7":monthofpayment="August";
    break;
    case "8":monthofpayment="September";
    break;
    case "9":monthofpayment="October";
    break;
    case "10":monthofpayment="November";
    break;
    case "11":monthofpayment="December";
    }
    document.write("<tr><td width='125'>")
    document.write(monthofpayment+" "+year+"</td>"+"<td width='125'>"+""+interest+"</td><td width='125'>"+""+principal);
    document.write("</td></tr>");
    startmonth++;
    }
    //-->
    </script>
    </table>
    </center>

  • #2
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,027
    Thanks
    36
    Thanked 494 Times in 488 Posts

    Lightbulb

    You have numerous errors.
    Take a look a the error console for some starting help.

    Also some useless code, for example:
    You have ...
    Code:
     switch(document.calcform.startm.value) {
      case "january"   : document.calcform.startm.value="0"; parseInt(document.calcform.startm.value); break;
      case "february"  : document.calcform.startm.value="1"; parseInt(document.calcform.startm.value); break;
    ...
    Should look like this ...
    Code:
     switch(document.calcform.startm.value) {
      case "january"   : document.calcform.startm.value="0"; break;
      case "february"  : document.calcform.startm.value="1"; break;
     ...
    or better yet...
    Code:
      var tmp = document.calcform.startm.value;
      var months = ['january','february','march','april','may','june',
                    'july','august','september','october','november','december'];
      var fnd = 0;  // used as default value
      for (var i=0; i<months.length; i++) { if (months[i] == tmp) { fnd = i; } }
      document..calcform.startm.value = fnd;
     ...
    Note also, it is not clear to enter the month as a string rather than a number like 1...12 in your site.

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,732
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    Also very old code. <script language=javascript> is long deprecated. Use <script type = "text/javascript"> instead (in fact also deprecated but still necessary for IE<9).
    The <!-- and //--> comment (hiding) tags have not been necessary since IE3 (i.e. since September 1997). If you see these in some published script it is a warning that you are looking at ancient and perhaps unreliable code.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •