Important alert: (current site time 7/15/2013 1:26:56 PM EDT)
 

VB icon

Add commas to whole numbers (using simple class and methods/instances)

Email
Submitted on: 2/28/2003 11:05:41 PM
By: Steven Jacobs 
Level: Intermediate
User Rating: By 2 Users
Compatibility: JavaScript
Views: 24043
author picture
(About the author)
 
     I don't know if this already exists here at PSC, but I had some time on my hand and I wanted to submit this for beginners and intermediates who dislike the idea of "OOP" programming in javascript. When you first look at the code, you may get the impression that all I am doing is using functions and ask yourself why I did what I did. My answer is this, I can pull that class and related prototyped functions/methods out and without any changes to the code, use it else- where. (WOW, sounds abit like C++ OOP and JAVA). Anyway, its more of a learning tool and yes, I did not do a complete validation on the input..just basic. Anyway, hope someone has the need for it and uses it.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
'**************************************
' Name: Add commas to whole numbers (using simple class and methods/instances)
' Description:I don't know if this already exists here at PSC, but I had some time on my hand and I wanted to submit this for beginners and intermediates who dislike the idea of "OOP" programming in javascript. When you first look at the code, you may get the impression that all I am doing is using functions and ask yourself why I did what I did. My answer is this, I can pull that class and related prototyped functions/methods out and without any changes to the code, use it else- where. (WOW, sounds abit like C++ OOP and JAVA). Anyway, its more of a learning tool and yes, I did not do a complete validation on the input..just basic. Anyway, hope someone has the need for it and uses it.
' By: Steven Jacobs
'
' Assumes:Need to be familiar with prototypes/classes/class methods and instances..
********************************
<!-Body Section of HTML Page-->
<form name = "testNum">
<input type = "text" name = "num" value = "">
<input type = "button" value = "Execute" onclick = "javascript:var jString = '';init(jString);">
</form>
'
'This code is copyrighted and has' limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=3457&lngWId=14'for details.'**************************************

<!-Head Section-->
function recurseMe(xPrompt)
{
this.x = xPrompt;
this.y = 3;
this.z = ",";
if (isNaN(xPrompt) || (xPrompt.indexOf(".") > 0))
{
alert("This is not a numerical string or a whole number.\n" +
"Please try again.");
return false;
}else{
recurseMe.prototype.rString(this.x,this.y,this.z);
}
delete this.x;
delete this.y;
delete this.z;
}
recurseMe.prototype.rString = function(x,y,z)
{
var b = "";
var gArray = new Array(x.length);
for (var i = x.length - 1; i >= 0; i--)
{
if (y == 0) 
{
gArray[i] = x.charAt(i) + z;
y = 2;
}else{
gArray[i] = x.charAt(i);
y -= 1;
}
}
recurseMe.prototype.setRight(gArray,i,b);
}
recurseMe.prototype.setRight = function(gArray,i,b)
{
for (var i = 0; i < gArray.length; i++)
{b += gArray[i];}
init(b);
}
function init(jString)
{
if (jString == '')
{
var fPrompt = prompt("Enter a whole number","0");
var x = new recurseMe(fPrompt);
}else{
document.testNum.num.value = "$" + jString + ".00";
}
}


Other 4 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.