Javascript in Mac: Number.toFixed challenge

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • glegipon
    New Member
    • Feb 2006
    • 2

    Javascript in Mac: Number.toFixed challenge

    As Tom Cahill would require, my challenge is (according to the "three
    R's")

    Reproducible: if run on OS 9.x, the monthly payment does not show up in
    the text box:

    Recognizable: I believe the Mac OS 9.x is not getting the temp_var
    value returned to the var MP to show in the form. My hypothesis is
    that the problems lies in this function:

    Code:
     	function round_2(Number) 
    	{
    	var temp_var = (Number.toFixed(2))
    	return temp_var
    	}
    Repairable:I believe I've located the error location, but this is my
    2nd day coding Javascript, so I don't know if it's repairable. Is
    there a fix to this? I heard if the Number.toFixed method isn't
    available on the operating system, it is possible to write that method
    as a function.

    Here's my code:

    Code:
     
    <HTML><HEAD><TITLE>Simple Mortgage Calculator</TITLE>
    <script language="JavaScript"> 
    function round_2(Number)
    {
    var temp_var = (Number.toFixed(2))
    return temp_var
    }
     
    function find_payment(A, B, C)
    {
    var temp_var = (A * B) / (1 - Math.pow(1 + B, -C))
    return temp_var
    }
     
    function dosum()
    {
    if(document.temps.PP.value>0)
    {
    if((document.temps.DP.value / document.temps.PP.value) <=0)
    {document.temps.DP.value=document.temps.PP.value/10}
    else if((document.temps.DP.value/document.temps.PP.value)<.1)
    {
    alert("A 10% Miminimum Down Payment Is Required.")
    document.temps.DP.value=document.temps.PP.value/10
    }
    else if((document.temps.DP.value/document.temps.PP.value)>=1)
    {
    alert("Come on. If you pay for the property in full at time of
    purchase, you won't need a loan. Why are you looking at terms? If you
    are going to pay in full, call me at (800) 760-3001 and I'll give you a
    5% discount on the purchase of any property on our inventory.")
    document.temps.DP.value=document.temps.PP.value/10
    }
    else
    {document.temps.DP.value=document.temps.DP.value}
     
    if(document.temps.YR.value<=0)
    {document.temps.YR.value=15}
    else if(document.temps.YR.value>30)
    {
    alert("30 Year Is The Maximum Repayment Period.")
    document.temps.YR.value=30
    }
    else
    {document.temps.YR.value=document.temps.YR.value}
     
    document.temps.LA.value=document.temps.PP.value-document.temps.DP.value
    if((document.temps.DP.value/document.temps.PP.value)<.2)
    {document.temps.IR.value=.09}
    else if((document.temps.DP.value/document.temps.PP.value)<.3)
    {document.temps.IR.value=.085}
    else
    {document.temps.IR.value=.08}
    var MP = document.temps.MP.value
    var LA = document.temps.LA.value
    var IR = document.temps.IR.value
    var YR = document.temps.YR.value
    MP=find_payment(LA,IR/12,YR*12)
     
    document.temps.AT.value=(document.temps.PP.value*.0125)
     
    document.temps.MP.value=round_2(MP)
    }
    else
    {alert("Please Enter A Purchase Price")}
    }
    function clearboxes()
    {
    document.temps.PP.value = "";
    document.temps.DP.value = "";
    document.temps.LA.value = "";
    document.temps.YR.value = "";
    document.temps.IR.value = "";
    document.temps.MP.value = "";
    document.temps.AT.value = "";
    }
    // -->
    </script>
    </HEAD><b>Simple Mortgage Calculator</b><br>
    <br>
    <form name="temps">
    Loan Details...<br><br>
    <u>variable price</u>
    <br>Purchase Price:<input NAME="PP" onfiltered="dosum()" tabindex="1"
    size=15 style="font-size:9pt;">
    <hr width=95%><br><br>
    <u>terms are flexible</u>
    <br>Down Payment:<input NAME="DP" onfiltered="dosum()" tabindex="1"
    size=15 style="font-size:9pt;">
    <br>Repayment Period: (Years)<input NAME="YR" onfiltered="dosum()"
    tabindex="2" size=15 style="font-size:9pt;">
    <br>
    <br>
    Results...<br><br>
    <u>fixed numbers</u>
    <br>Monthly Payment:<input size=13 NAME="MP" style="font-size:9pt;"
    tabindex="8">
    <br>Loan Amount:<input NAME="LA" onfiltered="dosum()" tabindex="1"
    size=15 style="font-size:9pt;">
    <br>Interest Rate:<input size=15 style="font-size:9pt;" NAME="IR"
    "onfiltered="dosum()" tabindex="3">
    <br>Annual County Tax:<input size=13 NAME="AT" style="font-size:9pt;"
    tabindex="11">
    <br>
     
    <br><br><blockquote><a href="javascript:clearboxes()" width=58
    height=16 border=0>Clear</a>
    <br><br><a href="javascript:dosum()" width=100 height=16
    border=0>CALCULATE</a>
    </blockquote>
    <div align=center><font size=1>Calculations made by this tool are
    believed to be accurate but are not guaranteed.</font></div>
    </BODY></HTML>
    Thanks for your help
    1
    A match made in heaven, like summer & ice cream!
    0%
    0
    Like McDonalds with healthy food, Mac's not there yet...but they are trying.
    0%
    1
    Umm, something's wrong,...and I don't think they're trying.
    0%
    0
    What's Mac got to do...(i said) got to do with it!?...
    0%
    0
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    You can add toFixed if it doesn't exist. Either use a function or add it directly using prototype, e.g.
    [code=javascript]if (!num.toFixed) {
    Number.prototyp e.toFixed = function(precis ion) {
    var num = (Math.round(thi s*Math.pow(10,p recision))).toS tring();
    return num.substring(0 ,num.length-precision) + "." + num.substring(n um.length-precision, num.length);
    }
    }[/code]

    Comment

    • gits
      Recognized Expert Moderator Expert
      • May 2007
      • 5388

      #3
      and one more thing to mention: the problem is not the MAC ... it is the browser you would use with it that has to have an implementation of ECMA-Script ... and so the problem is the browser that is used with a specific OS ... and from that point of view the OS could be a problem when it limits the use of an up to date browser or the user who wants to use a specific browser that you have to support - but not the MAC itself :) ...

      just this two cents from a MAC-user ... :)

      kind regards

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        Yes, of course, you're absolutely right!

        Forgot to mention that... thanks!

        It's probably either IE5 on the Mac or an old version of Safari that's the problem.

        Comment

        • mrhoo
          Contributor
          • Jun 2006
          • 428

          #5
          While acoder's method is flawless, and gits remarks apt, I am not sure that toFixed is the problem. I didn't remember the 'onfilter ' event, which is the one calling the method, untitl I googled it,

          onfilter is a non existent event handler that web based email services such as Yahoo mail uses in place of actual event handlers inside emails that contain a JavaScript. It's a security precaution, to disable any malicious script from executing in the user's browser.
          This doesn't mean he has the method toFixed, and anyone who doesn't can use acoder's replacement, but what event calls dosum?

          Comment

          • acoder
            Recognized Expert MVP
            • Nov 2006
            • 16032

            #6
            Good spot! Goes to show that three (sets of) eyes are better than one...
            Originally posted by mrhoo
            what event calls dosum?
            Probably via the CALCULATE link at the bottom, though onfiltered should probably be onchange.

            Comment

            Working...