Connecting Tech Pros Worldwide Help | Site Map
View Poll Results: Macs and javascript: to be or not to be, that is the question.
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. 1 100.00%
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%
Voters: 1. You may not vote on this poll

Javascript in Mac: Number.toFixed challenge

  #1  
Old February 7th, 2006, 11:19 AM
Newbie
 
Join Date: Feb 2006
Location: Live in California, yea sun!
Posts: 2
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:

Expand|Select|Wrap|Line Numbers
  1.      function round_2(Number) 
  2.     {
  3.     var temp_var = (Number.toFixed(2))
  4.     return temp_var
  5.     }
  6.  
  7.  
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:

Expand|Select|Wrap|Line Numbers
  1.  
  2. <HTML><HEAD><TITLE>Simple Mortgage Calculator</TITLE>
  3. <script language="JavaScript"> 
  4. function round_2(Number)
  5. {
  6. var temp_var = (Number.toFixed(2))
  7. return temp_var
  8. }
  9.  
  10. function find_payment(A, B, C)
  11. {
  12. var temp_var = (A * B) / (1 - Math.pow(1 + B, -C))
  13. return temp_var
  14. }
  15.  
  16. function dosum()
  17. {
  18. if(document.temps.PP.value>0)
  19. {
  20. if((document.temps.DP.value / document.temps.PP.value) <=0)
  21. {document.temps.DP.value=document.temps.PP.value/10}
  22. else if((document.temps.DP.value/document.temps.PP.value)<.1)
  23. {
  24. alert("A 10% Miminimum Down Payment Is Required.")
  25. document.temps.DP.value=document.temps.PP.value/10
  26. }
  27. else if((document.temps.DP.value/document.temps.PP.value)>=1)
  28. {
  29. alert("Come on. If you pay for the property in full at time of
  30. purchase, you won't need a loan. Why are you looking at terms? If you
  31. are going to pay in full, call me at (800) 760-3001 and I'll give you a
  32. 5% discount on the purchase of any property on our inventory.")
  33. document.temps.DP.value=document.temps.PP.value/10
  34. }
  35. else
  36. {document.temps.DP.value=document.temps.DP.value}
  37.  
  38. if(document.temps.YR.value<=0)
  39. {document.temps.YR.value=15}
  40. else if(document.temps.YR.value>30)
  41. {
  42. alert("30 Year Is The Maximum Repayment Period.")
  43. document.temps.YR.value=30
  44. }
  45. else
  46. {document.temps.YR.value=document.temps.YR.value}
  47.  
  48. document.temps.LA.value=document.temps.PP.value-document.temps.DP.value
  49. if((document.temps.DP.value/document.temps.PP.value)<.2)
  50. {document.temps.IR.value=.09}
  51. else if((document.temps.DP.value/document.temps.PP.value)<.3)
  52. {document.temps.IR.value=.085}
  53. else
  54. {document.temps.IR.value=.08}
  55. var MP = document.temps.MP.value
  56. var LA = document.temps.LA.value
  57. var IR = document.temps.IR.value
  58. var YR = document.temps.YR.value
  59. MP=find_payment(LA,IR/12,YR*12)
  60.  
  61. document.temps.AT.value=(document.temps.PP.value*.0125)
  62.  
  63. document.temps.MP.value=round_2(MP)
  64. }
  65. else
  66. {alert("Please Enter A Purchase Price")}
  67. }
  68. function clearboxes()
  69. {
  70. document.temps.PP.value = "";
  71. document.temps.DP.value = "";
  72. document.temps.LA.value = "";
  73. document.temps.YR.value = "";
  74. document.temps.IR.value = "";
  75. document.temps.MP.value = "";
  76. document.temps.AT.value = "";
  77. }
  78. // -->
  79. </script>
  80. </HEAD><b>Simple Mortgage Calculator</b><br>
  81. <br>
  82. <form name="temps">
  83. Loan Details...<br><br>
  84. <u>variable price</u>
  85. <br>Purchase Price:<input NAME="PP" onfiltered="dosum()" tabindex="1"
  86. size=15 style="font-size:9pt;">
  87. <hr width=95%><br><br>
  88. <u>terms are flexible</u>
  89. <br>Down Payment:<input NAME="DP" onfiltered="dosum()" tabindex="1"
  90. size=15 style="font-size:9pt;">
  91. <br>Repayment Period: (Years)<input NAME="YR" onfiltered="dosum()"
  92. tabindex="2" size=15 style="font-size:9pt;">
  93. <br>
  94. <br>
  95. Results...<br><br>
  96. <u>fixed numbers</u>
  97. <br>Monthly Payment:<input size=13 NAME="MP" style="font-size:9pt;"
  98. tabindex="8">
  99. <br>Loan Amount:<input NAME="LA" onfiltered="dosum()" tabindex="1"
  100. size=15 style="font-size:9pt;">
  101. <br>Interest Rate:<input size=15 style="font-size:9pt;" NAME="IR"
  102. "onfiltered="dosum()" tabindex="3">
  103. <br>Annual County Tax:<input size=13 NAME="AT" style="font-size:9pt;"
  104. tabindex="11">
  105. <br>
  106.  
  107. <br><br><blockquote><a href="javascript:clearboxes()" width=58
  108. height=16 border=0>Clear</a>
  109. <br><br><a href="javascript:dosum()" width=100 height=16
  110. border=0>CALCULATE</a>
  111. </blockquote>
  112. <div align=center><font size=1>Calculations made by this tool are
  113. believed to be accurate but are not guaranteed.</font></div>
  114. </BODY></HTML>
  115.  
Thanks for your help

Last edited by KUB365; February 7th, 2006 at 02:14 PM.
  #2  
Old April 16th, 2008, 12:18 PM
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,517
Provided Answers: 12

re: Javascript in Mac: Number.toFixed challenge


You can add toFixed if it doesn't exist. Either use a function or add it directly using prototype, e.g.
Expand|Select|Wrap|Line Numbers
  1. if (!num.toFixed) {
  2.     Number.prototype.toFixed = function(precision) {
  3.         var num = (Math.round(this*Math.pow(10,precision))).toString();
  4.         return num.substring(0,num.length-precision) + "." + num.substring(num.length-precision, num.length);
  5.     }
  6. }
  #3  
Old April 16th, 2008, 01:01 PM
gits's Avatar
Moderator
 
Join Date: May 2007
Location: Munich, Germany
Posts: 4,102
Provided Answers: 1

re: Javascript in Mac: Number.toFixed challenge


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
  #4  
Old April 16th, 2008, 01:57 PM
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,517
Provided Answers: 12

re: Javascript in Mac: Number.toFixed challenge


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.
  #5  
Old April 16th, 2008, 02:52 PM
Needs Regular Fix
 
Join Date: Jun 2006
Posts: 425

re: Javascript in Mac: Number.toFixed challenge


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,

Quote:
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?
  #6  
Old April 17th, 2008, 10:09 AM
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,517
Provided Answers: 12

re: Javascript in Mac: Number.toFixed challenge


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