codice:
Array.prototype.randNdelete = function () {
var rnd = Math.random()
, lng = this.length
, ret = this[Math.floor(rnd * lng)];
delete this[Math.floor(rnd * lng)];
return ret;
}
dove sbaglio?
Se ciclo un array con 4 elementi, spesso uno me lo ritorna "undefined" perchè lo cancella.
EDIT:
Ho risolto così, secondo voi funziona? (Teoricamente ho provato e va...)
codice:
Array.prototype.randNdelete = function (undefined) {
do {
var rnd = Math.random()
, lng = this.length
, ret = this[Math.floor(rnd * lng)];
} while(ret === undefined);
delete this[Math.floor(rnd * lng)];
return ret;
}