vendredi 7 avril 2017

JavaScript (google script) function sometimes works, sometimes not : what's wrong?

I have an array containing values (values from a google sheet), and two functions that generate random values to pick values from the array :

the random function #1 :

function hasardSingPlur(){
  var values = [0,1];
  return (values[Math.floor(Math.random()*2)]);
}

random generator function #2 :

function hasardNombreMax(){
   return (Math.floor((Math.random() * nombreMax) + 1))
}

the function to get the random elements :

function generate() {
  var nom = noms[hasardNombreMax(nombreMax)][hasardSingPlur()];
  Logger.log(nom);
}

If I test the generate() function a hundred times, it sometimes goes to the end and outputs 100 values, but it sometimes stops after random number of values...

function test(){
  for(var i = 0 ; i < 100 ; i++){
    Logger.log(generate());
      }
}

I get the error

TypeError: Impossible to read property "1.0" from undefined. 

Strange behavior, and I really don't understand why.




Aucun commentaire:

Enregistrer un commentaire