mardi 8 octobre 2019

JavaScript Math.random() returns "Undefined"

I have a block of code that uses Math.random(). I noticed that occasionally the return value would be "Undefined". This is the code I used:

return data.map(val => {
    var r = Math.random();
    if (r < this.mutChance) {
        console.log(Math.random);
        debugger;
        return this.rDna(val);
    }
    return val;
});

When i set the mutChance variable to 0 and let the code run for a bit eventually debugger gets called and shows the value of r to be undefined. I tried to reproduce the problem by running in console

var test = Math.random();
while(test){
    test = Math.random();
}

However, this loop never ended. I have no idea why the function would act differently within my object, and the console.log(Math.random); Says that the function still has its native code. Nowhere do I override the random function, nor do I use the variable r anywhere else.

I am relatively new to JavaScript and couldn't find this problem anywhere else. The only other code I have imported is the p5.min.js package.




Aucun commentaire:

Enregistrer un commentaire