interessante, ho colto l' occasione per fare una proto che spero torni utile.
La soluzione, tramite proto, diventa di poche righe ed e' questa:
codice:
M = 10;
A = new Array();
while( A.length < M ) {
if( !A.in_array( myRand = Math.round(M*Math.random()) ) ) {
A.push( myRand );
}
}
trace(A);
la proto, peraltro semplice ma portabilissima, e' Array.in_array() e questo e' il codice:
codice:
Array.prototype.in_array = function( what ) {
// andr3a [ 25 / 03 / 2004 ]
// check if a value is inside an array
// EXAMPLE:
// var myArray = new Array( "hello", "world", Array("one", "two") );
// trace( myArray.in_array( "hello" ) ); // true
// trace( myArray.in_array( "hi" ) ); // false
// trace( myArray.in_array( "two" ) ); // true
for( var a = 0; a < this.length; a++ ) {
if( this[a] == what ) {
return true;
}
else if( this[a] instanceof Array ) {
return this[a].in_array( what );
}
}
return false;
}