473,973 Members | 1,803 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Too much time on my hands today...

I had a very quiet schedule at work today, so I thought I'd finish this
script. In theory, it should generate a random time between two moments
specified, round it off to a conventional interval if desired, and
convert it into text if desired. The text conversion routine is a
separate function which could be used more generally if desired.

It mostly seems to work ok. Anyone want to either borrow it or show me
some horribly obvious flaw that I've missed, or show me how my code is
woefully inefficient? I'm just inordinately pleased that I managed to
write all this without blatantly copying any pre-existing code.

Yeah, this is going to be used for one of my language learning games on
my website, strange as it may seem.

--
Fabian
Visit my website often and for long periods!
http://www.lajzar.co.uk

function timr(s,e,r,c,t) {
/*
created and (c) by Fabian van-de-l'Isle 2003-10-24
Free for non-profit use
May be distributed freely
No fee may be charged for distribution

(s) earliest random time, 24 hour decimal notation
(e) latest random time, 24 hour decimal notation
For random times spanning midnight, use s=23 and e=25 (or whatever)

(r) rounding mode:
0 - 1 minute
1 - 5 minutes
2 - 10 minutes
3 - 15 minutes
4 - 30 minutes
5 - 60 minutes

(c) clock mode:
0 - 24 hour clock
1 - 12 hour clock, am/pm style
2 - 12 hour clock, verbose text style
3 - 12 hour clock, null style

(t) output text mode:
0 - numerals - 7:31
1 - text - seven thirty one
*/

//generate an output time in 24 hour decimal
var o, hh, mm;
while (e < s) { e = e + 24; }
o = (Math.random() * (e-s)) + s;
if (o > 24) { o = o - 24; }
// o should now be between 0 and 23.999

// round to the specified minutes value
var rav = [[60,12,6,4,2,1], [1,5,10,15,30,60]];
o = (o * rav[0][r]) + 0.5;
o = Math.floor(o);
o = o * rav[1][r];
// o is the rounded time in minutes, 0 to 1439

// force into 12 hour clock if mode is so set
var tt = "";
var oo = o;
if (c == 1) {
tt = " am";
if (o >= 720) { tt = " pm"; }
}
if (c == 2) {
tt = " at night";
if (o > 300) { tt = " in the morning"; }
if (o > 720) { tt = " in the afternoon"; }
if (o > 1080) { tt = " in the evening"; }
if (o > 1320) { tt = " at night"; }
}
if (c == 3) {
tt = "";
}
if (c>0) {
if (oo < 60) { oo = oo + 60; }
if (oo >= 780) { oo = oo - 720; }
}
// end 12 hour clock modes

// generate outputs
hh = Math.floor(oo / 60);
while (oo > 59) { oo = oo - 60; }
mm = oo;
var finl;

//numeric outputs
if (t == 0) {
if (mm < 10) { mm = "0" + mm; }
finl = hh + ":" + mm + tt;
}
// text mode outputs
if (t == 1) {
finl = numri(hh) + " ";
if (mm==0) { finl = finl + "o' clock" + tt; }
else {
if (mm<10) { finl = finl + "oh "; }
finl = finl + numri(mm) + tt;
}

if (o == 0) { finl = "midnight"; }
if (o == 720) { finl = "midday"; }
}
return finl;
}

function numri(i) {
// billions are US billions. replace "billion" string with "hundred
million" for traditional UK usage.
// comment out "and" lines for strict US usage.
var uu, tu, hu;
var ut, tt, ht;
var um, tm, hm;
var ub, tb, hb;
var leng
var txt = "";

i = Math.floor(i) + "";

leng = i.length;

hb = i.substring(len g -12,leng -11);
tb = i.substring(len g -11,leng -10);
ub = i.substring(len g -10,leng - 9);
hm = i.substring(len g - 9,leng - 8);
tm = i.substring(len g - 8,leng - 7);
um = i.substring(len g - 7,leng - 6);
ht = i.substring(len g - 6,leng - 5);
tt = i.substring(len g - 5,leng - 4);
ut = i.substring(len g - 4,leng - 3);
hu = i.substring(len g - 3,leng - 2);
tu = i.substring(len g - 2,leng - 1);
uu = i.substring(len g - 1,leng);

if ((hb + tb + ub) > 0) {
if (hb > 0) {
txt = txt + anmar(hb) + " hundred";
if ((tb + ub) > 0) { txt = txt + " and "; }
}
if (tb > 1) {
txt = txt + anmar(tb * 10);
if (ub > 0) { txt = txt + " " + anmar(ub); }
}
if (((tb + ub) > 0) && (tb<2)) { txt = txt + anmar(tb + ub); }
txt = txt + " billion";
}

if (txt != "") { txt = txt + " ";}

if ((hm + tm + um) > 0) {
if (hm > 0) {
txt = txt + anmar(hm) + " hundred";
if ((tm + um) > 0) { txt = txt + " and "; }
}
if (tm > 1) {
txt = txt + anmar(tm * 10);
if (um > 0) { txt = txt + " " + anmar(um); }
}
if (((tm + um) > 0) && (tm<2)) { txt = txt + anmar(tm + um); }
txt = txt + " million";
}

if (txt != "") { txt = txt + " ";}

if ((ht + tt + ut) > 0) {
if (ht > 0) {
txt = txt + anmar(ht) + " hundred";
if ((tt + ut) > 0) { txt = txt + " and "; }
}
if (tt > 1) {
txt = txt + anmar(tt * 10);
if (ut > 0) { txt = txt + " " + anmar(ut); }
}
if (((tt + ut) > 0) && (tt<2)) { txt = txt + anmar(tt + ut); }
txt = txt + " thousand";
}

if (txt != "") { txt = txt + " ";}

if ((hu + tu + uu) > 0) {
if (hu > 0) {
txt = txt + anmar(hu) + " hundred";
if ((tu + uu) > 0) { txt = txt + " and "; }
}
if (tu > 1) {
txt = txt + anmar(tu * 10);
if (uu > 0) { txt = txt + " " + anmar(uu); }
}
if (((tu + uu) > 0) && (tu<2)) { txt = txt + anmar(tu + uu); }
}

return txt;
}
function anmar(i) {
if (i == 1) { return "one"; }
if (i == 2) { return "two"; }
if (i == 3) { return "three"; }
if (i == 4) { return "four"; }
if (i == 5) { return "five"; }
if (i == 6) { return "six"; }
if (i == 7) { return "seven"; }
if (i == 8) { return "eight"; }
if (i == 9) { return "nine"; }
if (i == 10) { return "ten"; }
if (i == 11) { return "eleven"; }
if (i == 12) { return "twelve"; }
if (i == 13) { return "thirteen"; }
if (i == 14) { return "fourteen"; }
if (i == 15) { return "fifteen"; }
if (i == 16) { return "sixteen"; }
if (i == 17) { return "seventeen" ; }
if (i == 18) { return "eighteen"; }
if (i == 19) { return "nineteen"; }
if (i == 20) { return "twenty"; }
if (i == 30) { return "thirty"; }
if (i == 40) { return "forty"; }
if (i == 50) { return "fifty"; }
if (i == 60) { return "sixty"; }
if (i == 70) { return "seventy"; }
if (i == 80) { return "eighty"; }
if (i == 90) { return "ninety"; }
return null;
}

Jul 20 '05 #1
9 2033
Fabian wrote:
function anmar(i) {
if (i == 1) { return "one"; }
if (i == 2) { return "two"; }
if (i == 3) { return "three"; }
if (i == 4) { return "four"; }
if (i == 5) { return "five"; }
if (i == 6) { return "six"; }
if (i == 7) { return "seven"; }
if (i == 8) { return "eight"; }
if (i == 9) { return "nine"; }
if (i == 10) { return "ten"; }
if (i == 11) { return "eleven"; }
if (i == 12) { return "twelve"; }
if (i == 13) { return "thirteen"; }
if (i == 14) { return "fourteen"; }
if (i == 15) { return "fifteen"; }
if (i == 16) { return "sixteen"; }
if (i == 17) { return "seventeen" ; }
if (i == 18) { return "eighteen"; }
if (i == 19) { return "nineteen"; }
if (i == 20) { return "twenty"; }
if (i == 30) { return "thirty"; }
if (i == 40) { return "forty"; }
if (i == 50) { return "fifty"; }
if (i == 60) { return "sixty"; }
if (i == 70) { return "seventy"; }
if (i == 80) { return "eighty"; }
if (i == 90) { return "ninety"; }
return null;
}


function anmar(i)
{
var a =
new Array(
null, "one", "two", "three", "four", "five", "six",
"seven", "eight", "nine", "ten", "eleven", "twelve",
"thirteen", "fourteen", "fifteen", "sixteen", "seventeen" ,
"eighteen", "nineteen", "twenty");

for (var j = 21; j < 90; j++)
a[j] = null;

a[30] = "thirty";
a[40] = "fourty";
a[50] = "fifty";
a[60] = "sixty";
a[70] = "seventy";
a[80] = "eighty";
a[90] = "ninety";

return (a[i] ? a[i] : null);
}

And that's not even splitted to atoms of language.

BTW: It is counterproducti ve to post scripts below the signature,
since a correctly separated signature is automagically cut on reply
by most news clients (I had to copypaste it in order to reply). And
it is counterproducti ve to post hundreds of lines of code instead of
posting the URL of a file containing that code. Bandwidth is precious.
PointedEars

Jul 20 '05 #2
Fabian hu kiteb:
I had a very quiet schedule at work today, so I thought I'd finish
this script. In theory, it should generate a random time between two
moments specified, round it off to a conventional interval if
desired, and convert it into text if desired. The text conversion
routine is a separate function which could be used more generally if
desired.


I just polished up my numbers script. It now handles numbers as English
text up to 33 digits, in your choice of US/UK English. And it does that
with half the amount of code I had in the original. I made js think the
number string is text in order to avoid those floating point errors.

There is, unfortunately, a limit on how many digits that can be done for
the Japanese version. One of the Japanese equivalents to X-llions isn't
in the Unicode character set.
I am feeling pleased with myself today.
--
--
Fabian
Visit my website often and for long periods!
http://www.lajzar.co.uk

Jul 20 '05 #3
Fabian wrote:
I am feeling pleased with myself today.


Great that you have a good time.

But what does it have to do with *coding* JavaScript?
PointedEars
Jul 20 '05 #4
On Sat, 1 Nov 2003 10:41:50 +0900
"Fabian" <la****@hotmail .com> wrote:
<snip>

I am feeling pleased with myself today.


That's great. If it isn't fun you've missed your calling.

--
Life is an offensive, directed against the repetitious mechanism of the
Universe.
--Alfred North Whitehead (1861-1947)
Jul 20 '05 #5
Thomas 'PointedEars' Lahn hu kiteb:
Fabian wrote:
I am feeling pleased with myself today.


Great that you have a good time.

But what does it have to do with *coding* JavaScript?


That paragraph, nothing. The ones you snipped, plenty. Halving the code
length whiel tripling the range of numbers the script can handle is, by
my personal benchmark, non-trivial.
--
--
Fabian
Visit my website often and for long periods!
http://www.lajzar.co.uk

Jul 20 '05 #6
In article <3F************ **@PointedEars. de>, Thomas 'PointedEars' Lahn
<Po*********@we b.de> writes:
Fabian wrote:
I am feeling pleased with myself today.


Great that you have a good time.

But what does it have to do with *coding* JavaScript?


Does that imply that this newsgroup is solely about the *coding* of JavaScript?
Because its not. Its a dicsussion group about the langauge, which covers the
coding of it, not vice versa. And trimming the size of a script in half while
increasing its productivity is *very* much on-topic here.

And IIRC, it should be javascript, not JavaScript.
--
Randy
Jul 20 '05 #7
Fabian wrote:
Thomas 'PointedEars' Lahn hu kiteb:
Fabian wrote:
I am feeling pleased with myself today.


Great that you have a good time.

But what does it have to do with *coding* JavaScript?


That paragraph, nothing. The ones you snipped, plenty. Halving the code
length whiel tripling the range of numbers the script can handle is, by
my personal benchmark, non-trivial.


Do you want congratulations or stuff like that here, or do you want to
contribute to this newsgroup? If the latter, you could have at least posted
the code that pleased you so much or an URL where one can look at it. If
the former, why don't you keep that uninteresting piece of information for
yourselves?

This is a *news*group, not a hall of fame.
PointedEars

Jul 20 '05 #8
On Sun, 02 Nov 2003 19:08:56 +0100
Thomas 'PointedEars' Lahn <Po*********@we b.de> wrote:
<snip>
Do you want congratulations or stuff like that here, or do you want to
contribute to this newsgroup? If the latter, you could have at least
posted the code that pleased you so much or an URL where one can look
at it. If the former, why don't you keep that uninteresting piece of
information for yourselves?

This is a *news*group, not a hall of fame.


Nor is it your private pulpit. Have you achieved anything of note
recently? Or does your sense of self-esteem depend on harranging
others?

--
Life is an offensive, directed against the repetitious mechanism of the
Universe.
--Alfred North Whitehead (1861-1947)
Jul 20 '05 #9
Thomas 'PointedEars' Lahn hu kiteb:
Do you want congratulations or stuff like that here, or do you want to
contribute to this newsgroup? If the latter, you could have at least
posted the code that pleased you so much or an URL where one can look
at it. If the former, why don't you keep that uninteresting piece of
information for yourselves?


I would hope that anyone sufficiently interested would ask for the code,
in order to save bandwidth. In any case, I have, technically, posted an
url where the code can be seen. It is used on my website below, albeit
one click away and then you'd need to check the source to find the exact
location. But people are free to ask me for it.
--
--
Fabian
Visit my website often and for long periods!
http://www.lajzar.co.uk

Jul 20 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
19651
by: Don | last post by:
Hi all, With regards to the following, how do I append the datetimestamp to the filenames in the form? The files are processed using the PHP script that follows below. Thanks in advance, Don Following running on client-side:
0
1155
by: Marcel - IDUG Europe 2005 | last post by:
Register today for IDUG 2005 - Europe, the premier European IDUG educational event(24-28 October, Berlin, Germany): http://conferences.idug.org/europe/2005/ Online registration ends Friday, 14 October: https://conferences.idug.org/secure/europe/2005/regform.cfm
25
2688
by: Neil Ginsberg | last post by:
A while back I posted a message re. using an ADP file with a SQL Server back end as opposed to MDB file with linked tables, thinking that the ADP file would be less problematic. The input I got was that the ADP would be just as, if not more, problematic; that ADO is far more limited than DAO, requiring a lot of workarounds; and that it would be better to stay with a seasoned MDB file than to switch to an ADP. The database in question was...
21
2012
by: salad | last post by:
Thanks for reading this post and wasting your time. I was thinking about writing about the PCDatasheet vs The Pussyfarts war. The pussyfarts, as near as I can tell, have little to offer this group but pussyfart noise. But I realize if you are going waste your time...the most precious commodity you have, you really shouldn't be reading pussyfart messages. You might want to read a book. There's a good kid book but actually it is a...
0
1331
by: Scott Nonnenberg [MSFT] | last post by:
The Visual Studio Debugger "Do you have some burning questions or comments about SQL, C#, VB, C++, or script debugging support in Visual Studio? Want to know more about Visual Studio 2005's enhanced datatips, improved interop debugging, Managed Debugging Assistants, debugger visualizers or Just My Code? Join the Visual Studio Debugger team to discuss the past, speculate on the future, and debate the present of the tool you work with and...
0
1464
by: Marcel - IDUG Europe 2006 | last post by:
Hi DB2 user, Register Before the Wednesday, 27 September Deadline http://conferences.idug.org/europe Don't miss your chance to attend the premier European IDUG educational event - IDUG 2006 - Europe at the Hilton Vienna, in Vienna Austria. Act fast - Online registration ends Wednesday, 27 September.* http://conferences.idug.org/eu/IDUG2006EuropeConferenceRegistrationForm/tabid/307/Default.aspx
3
1352
by: si_owen | last post by:
Hi all, I have a db that records time in the minutes that have passed midnight. I need to pull this back and put it into correct time hh:mm Can anyone help me with setting up the initial time to midnight?? Thanks in Advance,
9
2312
by: LayneMitch via WebmasterKB.com | last post by:
Hello. Got another one for you folks. I'm working on this problem that wants me to 1. Prompt for name 2. Use pop-up box with name 3. Display current date on page in format "October 30, 2000." 4. Display last modified date of doc. Here is my attempt. What a headache :-(
25
4575
by: Brian | last post by:
I have a datetimepicker formated for just time, the user selects the time. I want to compare if that time is between midnight and 8 am dtmTime #11:59:59 PM# and dtmTime < #08:00:00 AM# this evaluates to true when the time is not greater than dtmTime #9:32:34 PM# Why is that? and How can I get this to work? Brian
0
10347
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10160
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11811
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
11399
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10901
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
10071
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
7600
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
6410
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6542
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.