![]() |
Implement JS NumberFormat Function
Hi guys I am brand new to JS and building a small calculator project just to learn:
Calculator is working and I a now trying to implement a Format function I got by googling. I am obviously doing something wrong because all my attempts fail and in fact stop the rest of my JS from running Here is what I have: The line in red is my attempt at implementing the formatDollar( ) function Can someone show me where I am going wrong Thanks Code:
<script type="text/javascript"> |
Did you take a look at the formatDollar function? It's pretty obvious that it cannot work if you name the parameter "value" in the head, but the function tries to work on a variable named "num". It just doesn't fit. There might be more wrong, but I didn't look further into it as this is the first mistake. Once you fixed this, please use the console to check what error you are getting – maybe you can fix it yourself.
Also, when you post source code, please use the [ code ] tags to that it looks better and is readable. |
This is crazy:
Code:
function validInt(value)On top of that, as written that code would *ALLOW* input such as ".........." or "3........9" or "3.999999999999", none of which make a lot of sense. If you want to check for a valid number--and here I will assume it is to be no more than 2 digits after the decimal point (i.e., dollars and cents) then: Code:
function validNum( value )Notice that this also avoids the use of alert( ), which is obsolete. But if you aren't so fussy--if you *WILL* allow numbers such as 3.9999999 or 13E2--then let JS test for you: Code:
if ( isNaN( value ) ) { ... not a valid number ... } else { ... valid ... } |
Thanks Airblader I changed the parameter to num still not working and i have know Idea how to use "console" but will google and look into that
Thanks Old Pedant , that was also a piece of code I found googling, I actually do want to allow for decimals even though the function says "validInt" but thanks for explaing the other problems, once i figure out the problem with the question in my original post I will move onto that Figured out how to get debugger in IE and there is an error SCRIPT438: Object doesn't support property or method 'reduce' |
Found a work around to the error message with this piece of code:
Code:
if (!Array.prototype.reduce) { |
See the answer you were given in the thread you have hijacked.
http://www.codingforums.com/showthread.php?t=286939 |
Quote:
Wasn't aware I hijacked a thread, I asked a question that was relevant to the OP question in that thread. My apologies if that was not appropriate "Supreme Master Coder" :) |
Quote:
|
Quote:
Anyway, again my apologies, I certainly didnt intend to "hijack" anyones post, in the future I will start a new post Thanks |
Quote:
|
| All times are GMT +1. The time now is 11:04 PM. |
Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.