Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rating: Thread Rating: 2 votes, 5.00 average.
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-26-2010, 02:55 PM   PM User | #31
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,033
Thanks: 197
Thanked 2,410 Times in 2,388 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
I still prefer

Code:
function numsuffix(n) {
return n + ["th","st","nd","rd"][!(n%10>3||Math.floor(n%100/10)==1)*n%10]; 
}
Philip M is offline   Reply With Quote
Old 03-26-2010, 04:02 PM   PM User | #32
bardonw
New Coder

 
Join Date: Dec 2009
Posts: 11
Thanks: 1
Thanked 0 Times in 0 Posts
bardonw is an unknown quantity at this point
Quote:
Originally Posted by RoyW View Post
I like the boolean trick and I like the idea of only having 4 strings in the array
Code:
function num_abbrev_str(n)
{
    return n + ["th","st","nd","rd"][!(n%10>3||Math.floor(n%100/10)==1)*n%10]; 
}
In this 60% of the time the
(n%10>3)
will be true so the
Math.floor(n%100/10)==1
will only be executed for 40% of the numbers.

Invert the boolean and do the multiply
NOTE: I have utilised operator precedence to reduce char count.
Here is a version with parentheses to show precedence
PHP Code:
function num_abbrev_str(n)
{
    return 
+ ["th","st","nd","rd"][(!( ((n%10) >3) || (Math.floor(n%100/10)==1)) ) * (n%10)]; 

I must admit, this is some impressive code! After seeing this I decided to optimize it just a bit by storing the array of suffixes privately. After doing that and modifying it to have the same footprint as my generic function, I ended up with the following code:
Code:
var num_abbrev_str = (function()
{
	var z = ["th","st","nd","rd"];
	return function(intNum, includeNumber)
	{
		return (includeNumber ? intNum : "") + z[!((intNum = Math.abs(intNum))
			% 10 > 3 || Math.floor(intNum % 100 / 10) == 1) * intNum % 10];
	};
})();
The plus side is, the function works very well. The only disadvantage is, on Internet Explorer and Firefox, this function takes longer to run, for a large number of integers, than the code below does. On the other hand, I doubt that too many people will be running this type of function for 100,000+ numbers.
Code:
function num_abbrev_str(intNum, includeNumber)
{
	return (includeNumber ? intNum : "") + (((intNum = Math.abs(intNum) % 100)
		% 10 == 1 && intNum != 11) ? "st" : (intNum % 10 == 2 && intNum != 12)
		? "nd" : (intNum % 10 == 3 && intNum != 13) ? "rd" : "th");
}
Still, your code is most impressive.

Last edited by bardonw; 03-26-2010 at 04:09 PM..
bardonw is offline   Reply With Quote
Old 03-26-2010, 04:42 PM   PM User | #33
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,033
Thanks: 197
Thanked 2,410 Times in 2,388 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
In this forum it is often said that there are several good ways to skin a cat, but it is also said that re-inventing the wheel (but making it oval this time) does not add very much.
Philip M is offline   Reply With Quote
Old 03-27-2010, 02:54 AM   PM User | #34
RoyW
Regular Coder

 
Join Date: Jun 2002
Location: Atlanta, GA.
Posts: 313
Thanks: 0
Thanked 0 Times in 0 Posts
RoyW is an unknown quantity at this point
Looks like I still have email notifications on for this thread. A 7 year old thread! That must be a record
__________________
The answer does not come from thinking outside the box, it comes from realizing the truth :-
"There Is No Box". [JavaScript Gadgets'n'Gizmos][JavaScript-FX]
RoyW is offline   Reply With Quote
Old 10-10-2012, 05:05 PM   PM User | #35
bardonw
New Coder

 
Join Date: Dec 2009
Posts: 11
Thanks: 1
Thanked 0 Times in 0 Posts
bardonw is an unknown quantity at this point
After looking at someone else's implementation, I improved mine and created the smallest version that I could think of with the first parameter being the number and the second being the boolean indicating whether or not to show the number. I also used a closure in order to prevent the array from being recreated every time the function ran (this improves the speed):
Code:
(function(b){Number.getOrdinalFor=function(a,c){return(c?a:"")+(b[((a=Math.abs(a%100))-20)%10]||b[a]||"th")}})([,"st","nd","rd"]);
This function is only 130 characters long and will work for all integers. I also came up with a pretty cool example of how to use this for a string full of numbers:
Code:
var str = "1, 2, 3, 4.5, 6, and 7 are all numbers.";
alert(str.replace(/(\d+)/g, Number.getOrdinalFor));
I wrote a blog post about this function definition here:
http://gotochriswest.com/blog/2012/0...getordinalfor/

Let me know if you can write the equivalent function in an even shorter way while also making sure that something like an array doesn't have to be generated every time the function is called. That last requirement is what makes my function a little longer than it seems it needs to be.

Last edited by bardonw; 10-10-2012 at 05:34 PM..
bardonw is offline   Reply With Quote
Old 10-10-2012, 09:50 PM   PM User | #36
AndrewGSW
Senior Coder

 
Join Date: Apr 2011
Location: London, England
Posts: 2,120
Thanks: 15
Thanked 354 Times in 353 Posts
AndrewGSW will become famous soon enough
You could use a string rather than an array, incorporating:

XX = "thstndrd".substr((D % 10) * 2, 2) || "th";

It may not be shorter but avoids "Index out of range" or "undefined".

Oops! This is not quite right?!
__________________
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
Validate your HTML and CSS

Last edited by AndrewGSW; 10-10-2012 at 09:58 PM..
AndrewGSW is offline   Reply With Quote
Old 10-10-2012, 10:02 PM   PM User | #37
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,033
Thanks: 197
Thanked 2,410 Times in 2,388 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Code:
<script type="text/javascript">

var D = 12;
var XX = "thstndrd".substr((D % 10) * 2, 2) || "th";
alert (XX);  // nd!!!

</script>
__________________

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.
Philip M is offline   Reply With Quote
Old 10-10-2012, 10:04 PM   PM User | #38
AndrewGSW
Senior Coder

 
Join Date: Apr 2011
Location: London, England
Posts: 2,120
Thanks: 15
Thanked 354 Times in 353 Posts
AndrewGSW will become famous soon enough
Quote:
Originally Posted by Philip M View Post
Code:
<script type="text/javascript">

var D = 12;
var XX = "thstndrd".substr((D % 10) * 2, 2) || "th";
alert (XX);  // nd!!!

</script>
Yes, shame though - it looked good! Perhaps we can change the suffixes to match my formula .
__________________
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
Validate your HTML and CSS
AndrewGSW is offline   Reply With Quote
Old 10-10-2012, 10:06 PM   PM User | #39
WolfShade
Regular Coder

 
Join Date: Apr 2012
Location: St. Louis, MO, USA
Posts: 941
Thanks: 7
Thanked 95 Times in 95 Posts
WolfShade is an unknown quantity at this point
http://ecommerce.shopify.com/c/ecomm...-3rd-4th-29259
__________________
^_^

If anyone knows of a website that can offer ColdFusion help that isn't controlled by neurotic, pedantic jerks* (stackoverflow.com), please PM me with a link.
*
The neurotic, pedantic jerks are not the owners; just the people who are in control of the "popularity contest".
WolfShade is offline   Reply With Quote
Old 10-10-2012, 10:38 PM   PM User | #40
AndrewGSW
Senior Coder

 
Join Date: Apr 2011
Location: London, England
Posts: 2,120
Thanks: 15
Thanked 354 Times in 353 Posts
AndrewGSW will become famous soon enough
Code:
XX = "thstndrd".substr((D % 10) * !(D % 14 > 9) * 2, 2) || "th";
I'm considering dates though (from the title of this post) so haven't gone beyond 31 Added: Yes, works beyond 31.
__________________
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
Validate your HTML and CSS

Last edited by AndrewGSW; 10-11-2012 at 12:01 AM..
AndrewGSW is offline   Reply With Quote
Old 10-11-2012, 03:09 AM   PM User | #41
AndrewGSW
Senior Coder

 
Join Date: Apr 2011
Location: London, England
Posts: 2,120
Thanks: 15
Thanked 354 Times in 353 Posts
AndrewGSW will become famous soon enough
Code:
XX = "thstndrd".substr((D % 10) * (D < 11 || D > 13) * 2, 2) || "th";
.. works with non-date numbers.
__________________
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
Validate your HTML and CSS
AndrewGSW is offline   Reply With Quote
Old 10-11-2012, 08:03 AM   PM User | #42
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,033
Thanks: 197
Thanked 2,410 Times in 2,388 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Quote:
Originally Posted by AndrewGSW View Post
Code:
XX = "thstndrd".substr((D % 10) * (D < 11 || D > 13) * 2, 2) || "th";
.. works with non-date numbers.
No it doesn't!

Code:
var D = 112;
var XX = "thstndrd".substr((D % 10) * (D < 11 || D > 13) * 2, 2) || "th";
alert (XX);   // nd!!!

Code:
var D = 112;
var XX = "thstndrd".substr((D % 10) * !(D % 14 > 9) * 2, 2) || "th";
alert (XX);  // nd!!!
See Post # 31 - that does work!
__________________

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.

Last edited by Philip M; 10-11-2012 at 08:07 AM..
Philip M is offline   Reply With Quote
Old 10-12-2012, 08:49 PM   PM User | #43
bardonw
New Coder

 
Join Date: Dec 2009
Posts: 11
Thanks: 1
Thanked 0 Times in 0 Posts
bardonw is an unknown quantity at this point
Quote:
Originally Posted by Philip M View Post
No it doesn't!

Code:
var D = 112;
var XX = "thstndrd".substr((D % 10) * (D < 11 || D > 13) * 2, 2) || "th";
alert (XX);   // nd!!!

Code:
var D = 112;
var XX = "thstndrd".substr((D % 10) * !(D % 14 > 9) * 2, 2) || "th";
alert (XX);  // nd!!!
See Post # 31 - that does work!
I think that is a good version and I definitely spent some time looking at how to make it work a bit differently. The only problem I have is the fact that in that version, the array of ordinals is created every time the function is run. That means that the function will run slower if run on many thousands of numbers. Still, for most cases, that function will do. I just know that people like John Resig or Douglas Crockford would not like me endorsing that as the best solution.
bardonw is offline   Reply With Quote
Old 10-13-2012, 02:55 AM   PM User | #44
AndrewGSW
Senior Coder

 
Join Date: Apr 2011
Location: London, England
Posts: 2,120
Thanks: 15
Thanked 354 Times in 353 Posts
AndrewGSW will become famous soon enough
I suppose..

Code:
var XX = "thstndrd".substr((D % 10) * (D % 100 < 11 || D  % 100 > 13) * 2, 2) || "th";
but my interest was really in working with days (from the OP title).
__________________
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
Validate your HTML and CSS
AndrewGSW is offline   Reply With Quote
Old 10-13-2012, 07:49 AM   PM User | #45
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,033
Thanks: 197
Thanked 2,410 Times in 2,388 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Quote:
Originally Posted by bardonw View Post
The only problem I have is the fact that in that version, the array of ordinals is created every time the function is run. That means that the function will run slower if run on many thousands of numbers. Still, for most cases, that function will do. I just know that people like John Resig or Douglas Crockford would not like me endorsing that as the best solution.
I would doubt if there was any perceptible slowness (milliseconds perhaps) even if run on "many thousand of numbers". And where in reality is that required? Does the need to generate 1st to 123456th ever really arise?
__________________

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.
Philip M is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 05:23 AM.


Advertisement
Log in to turn off these ads.