Forum Moderators: open

Message Too Old, No Replies

Checking and changing

I need a quick solution for this ASAP please!

         

adni18

11:24 pm on Nov 3, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Is there a way in JScript to:

1) Check if a random number is already located in a string, with "¦" on either side of it
2) If it's not located there, then choose another random number
3) If there is no random number other than the ones listed, then execute the alert function.

I need help with this really badly, so please respond ASAP!

Bernard Marx

12:26 am on Nov 4, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



How about choosing a number from the string at random?
-I say this because I don't understand #3

[pre]
Array.prototype.getRandom = function(start, stop)
{
start = start¦¦0; stop = stop¦¦this.length;
return this[ start + Math.floor( Math.random()*(stop-start) ) ];
}

numbers = "14¦45¦78";
rand = numbers.split('¦').getRandom()
[/pre]

adni18

1:23 am on Nov 4, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



nonono. It's like this:

It gets a random number, and if the random number has already been chosen and placed in the string by a previous script, then it chooses another random number.

Rambo Tribble

1:38 am on Nov 4, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Will there be any other instances within the string of the pipe symbol, "¦"? If not you could simply split on ¦ and check the second array value (var_arr[1]) for its numericity, uhh, numericality, unh, would you believe . . . unh, numberness?

Obviously, more devious versions of the above could also be applied if there were more than one numeric value, but each was in a fixed relation to the pipe symbols and other string content.

You might wish to investigate parseInt() and parseFloat() in conjunction with isNaN() if you wish to test strings for numeric value or quality contained therein.

I'll leave the randomness to Bernard.

Emperor

2:24 am on Nov 4, 2004 (gmt 0)

10+ Year Member



Just put it in a while loop until you get a fresh number. I would code it for you right now but I still don't understand what you are having problems with or even trying to do (like rule 3.)

Take care.
Cyrus

adni18

12:28 pm on Nov 4, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Number three means that if I have specified a random number as any number between -1 and 5 [Math.floor(Math.random()*5)], and 0,1,2,3, and 4 have already been taken, then alert()