lundi 7 novembre 2016

I can't understand a piece of code [on hold]

I'm facing problems understanding a simple notion. Below the code:

var arr = [1, 3, 7, 9, 12, 5, 4, 6];
var randomArr = Math.floor(Math.random()*arr.length);
console.clear();
console.log(randomArr);

What i don't understand from this, is why Math.floor(Math.random()*arr.length) returns a random number each time while Math.floor(Math.random()) always returns 0 ? From my understanding, Math.floor(Math.random()) will always return 0 since he generated a value between 0 and 1 (1 not included), so shouldn't Math.floor(Math.random()*arr.length) always return 8 in my case?

This is what i don't understand at the moment and can't find anything on this matter.

Thanks.




Aucun commentaire:

Enregistrer un commentaire