473,407 Members | 2,676 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

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(leng -12,leng -11);
tb = i.substring(leng -11,leng -10);
ub = i.substring(leng -10,leng - 9);
hm = i.substring(leng - 9,leng - 8);
tm = i.substring(leng - 8,leng - 7);
um = i.substring(leng - 7,leng - 6);
ht = i.substring(leng - 6,leng - 5);
tt = i.substring(leng - 5,leng - 4);
ut = i.substring(leng - 4,leng - 3);
hu = i.substring(leng - 3,leng - 2);
tu = i.substring(leng - 2,leng - 1);
uu = i.substring(leng - 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 1984
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 counterproductive 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 counterproductive 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*********@web.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*********@web.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
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,...
0
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...
25
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...
21
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...
0
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...
0
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 -...
3
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...
9
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."...
25
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.