Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Too much time on my hands today...

Reply
Thread Tools

Too much time on my hands today...

 
 
Fabian
Guest
Posts: n/a
 
      10-24-2003
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 - ;
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 == { 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 == 1 { 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;
}

 
Reply With Quote
 
 
 
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      10-24-2003
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 == { 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 == 1 { 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

 
Reply With Quote
 
 
 
 
Fabian
Guest
Posts: n/a
 
      11-01-2003
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

 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      11-01-2003
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
 
Reply With Quote
 
Fabian
Guest
Posts: n/a
 
      11-01-2003
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

 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      11-02-2003
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

 
Reply With Quote
 
Fabian
Guest
Posts: n/a
 
      11-02-2003
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

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
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Too Much Talent + Too Much Demand = ? Lawrence D'Oliveiro NZ Computing 5 02-18-2011 10:14 PM
Way too much time on their hands. ian field Computer Support 1 08-22-2010 02:41 PM
OT: too much time on their hands kpg MCSE 2 11-13-2006 04:15 PM
Simulation questions...how much is too much? =?Utf-8?B?VGlwcHk=?= Microsoft Certification 0 04-16-2005 04:47 AM
[OT] Too much time on their hands JaR MCSE 1 11-18-2004 12:10 AM



Advertisments