4 Replies Latest reply on Dec 10, 2008 3:54 PM by pedro_poc

# Is this a Number?

Dear All,

I have created a couple of functions. The PickRandomBox function selects the
first item from a radomised array. The second function takes this value
along with a basic if statement to return a string. The problem seems to be
that the second function does not see the input value, boxcode, as a Number.
A couple of trace statements show me that the array returns an entry but the
if statements doesn't see the value as a number that it can evaluate. I have
tried using the int function to convert the first value from the array but
it always returns a 0.

I am totally confused. It seems as if an element of an array is not a
Number.

Please put me out of my misery.

Alastair MacFarlane

arr: 3,18,27,32,5,9,37,1,12,21
The colour number is: 3
MORAY FIRTH COAST - (Unsure - Box 3)

function PickRandomBox(arr:Array){
arr.randomize();
trace("arr: " + arr);
trace(_root.boxnames[arr[0]] + " - " + BoxCodeDescr(arr[0]));
}

function BoxCodeDescr(boxcode:Number) {
trace("The colour number is: " + boxcode);
//boxcode=27;
if (boxcode == 1 || boxcode == 3) {
return "(Brown - Box " + boxcode + ")";
break;
} else if (boxcode == 5 || boxcode == 15 || boxcode == 25 || boxcode == 35)
{
return "(Airport - Box " + boxcode + ")";
break;
} else {
return "(Unsure - Box " + boxcode + ")";
break;
}
}

• ###### 1. Re: Is this a Number?
How is your array declared in the code? I'm assuming what you show in the first line isn't it.

What is the randomize() function? It's a new one to me if it's a valid array method.

You don't need to have the break;'s, they are primarily for stopping a loop, which you don't appear to be using.
• ###### 2. Re: Is this a Number?
Thanks NedWebs,

I have declared the array as below. Nebwebs, I would say don't spend too
long on it. I have a workaround that will do but the inquisitive side of me
wants to know why it does not work. Thanks again very much for your help.
All the relevant code is here, I think! Alastair

On Frame1, Layer1
boxnames = new Array("GO", "MORAY FIRTH COAST", "Community Chest", ...)
boxmodes = new Array(0, 2, 0, 2, 0, 0, 3, 3, 3, ...) -// where the values 0,
1, 2 ,3 all mean different things

On Frame1, Layer2
function PickRandomUserBox(){
var myArray:Array = Array();
len = _root.boxmodes.length;
for (var i = 0; i<len; i++) {
if (_root.boxmodes == 2) {
myArray.push(
); // Creates an array of all the box nos with a 2
}
}
return _root.PickRandomBox(myArray);
break;
};

function PickRandomBox(arr:Array){
arr.randomize();
// Returns the first box no with the array[0]
trace("arr: " + arr); //==========> arr: 3,18,27,32,5,9,37,1,12,21
trace(_root.boxnames[arr[0]] + " - " + BoxCodeDescr(arr[0])); //==========>
MORAY FIRTH COAST - (Unsure - Box 3)
}

// This Random Array was created with the help of yourself and Rothrock
Array.prototype.randomize = function ()
{
var __mon2 = this.length;
if (__mon2 != 0)
{
while (--__mon2)
{
var __mon3 = Math.floor(Math.random() * (__mon2 + 1));
var __mon5 = this[__mon2];
var __mon4 = this[__mon3];
this[__mon2] = __mon4;
this[__mon3] = __mon5;
}
return this;
}
}
ASSetPropFlags(Array.prototype,["randomize"],1,1);

function BoxCodeDescr(boxcode:Number) {
trace("The colour number is: " + boxcode);
//boxcode=27;
if (boxcode == 1 || boxcode == 3) {
return "(Brown - Box " + boxcode + ")";
break;
} else if (boxcode == 5 || boxcode == 15 || boxcode == 25 || boxcode == 35)
{
return "(Airport - Box " + boxcode + ")";
break;
} else {
return "(Unsure - Box " + boxcode + ")";
break;
}
}
• ###### 3. Re: Is this a Number?
Do you get a valid number out of the trace:

trace("The colour number is: " + boxcode);

in the BoxCodeDescr function?

If not, my suspicions fall on the randomize end of things. I am not familiar with the prototype aspect of things, so I can't offer any help there. But one thing that I question is that the randomize function returns a value, so from my old world experience I would expect to see something of the nature of:

arr = arr.randomize();

• ###### 4. Re: Is this a Number?
Did you try parseInt() instead of int()?
just a quick guess, it might not work.