Register forum user name Search FAQ

Gammon Forum

Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the password reset link.

Due to spam on this forum, all posts now need moderator approval.

 Entire forum ➜ MUSHclient ➜ Jscript ➜ Help

Help

It is now over 60 days since the last post. This thread is closed.     Refresh page


Posted by JSteele1234   USA  (27 posts)  Bio
Date Mon 28 May 2001 11:13 PM (UTC)

Amended on Mon 28 May 2001 11:16 PM (UTC) by JSteele1234

Message
I wrote a script that will convert a number to english (1 => "one", 34 => "thirty-four", etc.) ($string_utils:english_number() for MOO people) but for some reason it breaks out of a loop like:

for (n = 1; n <= 10; n++) {
world.note(english_number(n));
}

It just does "one" and stops. I even put "world.note("Blah");" right after it and it displays:

one
Blah

what's going on?
-------------
function english_number(num) {
if (num == 0)
return "zero";

var labels = new Array("", " thousand", " million", " billion", " trillion", " quadrillion", " quintillion", " sextillion", " septillion", " octillion", " nonillion", " decillion");
var numstr = "";
var mod = Math.abs(num);
for (n = 0; n < labels.length; n++) {
var div = mod % 1000;
if (div != 0) {
var hun = Math.floor(div / 100);
var ten = Math.floor(div % 100);

var outstr = english_tens(ten) + labels[n];

if (hun != 0)
outstr = english_ones(hun) + " hundred" + (ten != 0 ? " " : "") + outstr;

if (numstr != "")
numstr = outstr + " " + numstr;
else
numstr = outstr;
}
mod = Math.floor(mod / 1000);
}
return (num < 0 ? "negative " : "") + numstr;
}

function english_tens(num) {
var teens = new Array("ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen");
var others = new Array("twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety");

if (num < 10)
return english_ones(num);
else if (num < 20)
return teens[num - 10];
else
return others[Math.floor(num / 10) - 2] + (num % 10 != 0 ? "-" : "") + english_ones(num % 10);
}

function english_ones(num) {
var ones = new Array("", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine");
return ones[num];
}

~ JSteele1234 ~
Top

Posted by JSteele1234   USA  (27 posts)  Bio
Date Reply #1 on Mon 28 May 2001 11:15 PM (UTC)
Message
arg... how do I indent these lines? They're intented in the textarea box.

~ JSteele1234 ~
Top

Posted by Nick Gammon   Australia  (23,169 posts)  Bio   Forum Administrator
Date Reply #2 on Tue 29 May 2001 12:47 AM (UTC)
Message
To indent them put:

[code]
your code here
[/code]

and check the "forum codes" box.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Nick Gammon   Australia  (23,169 posts)  Bio   Forum Administrator
Date Reply #3 on Tue 29 May 2001 12:52 AM (UTC)
Message
The answer to your problem is that you are using "n" in function english_number here:


for (n = 0; n < labels.length; n++)


Thus you are changing the value of n in the function, so that your calling function immediately hits 10.

Change it to:


for (i = 1; i <= 10; i++) {
world.note(english_number(i));
}


and it works.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by JSteele1234   USA  (27 posts)  Bio
Date Reply #4 on Tue 29 May 2001 03:56 AM (UTC)
Message
Arg! Stupid me! Thank you.

~ JSteele1234 ~
Top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


19,033 views.

It is now over 60 days since the last post. This thread is closed.     Refresh page

Go to topic:           Search the forum


[Go to top] top

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.