Bonjour,,
Je faisais un exercice JS sur freecodecamp.org sur les nombres al�atoires entiers avec les fonctions Math.random() et Math.floor(� et je suis tomb� sur cette erreur..
Je me demande bien � quoi �a correspond, et est-ce qu'en fait cela cr�e des it�rations infinies qui font crasher le programma??(d�passement de valeurs)?
MON
INSTRUCTIONS DE L'EXERCICE:
Code : S�lectionner tout - Visualiser dans une fen�tre � part
1
2
3
4
5
6
7
8
9
10
11
12 var randomNumberBetween0and19 = Math.floor(Math.random() * 20); function randomWholeNum() { // Only change code below this line. for (var i=0;i<10;i++) { return Math.random[i](randomWholeNum(i)); } }
Generate Random Whole Numbers with JavaScript
It's great that we can generate random decimal numbers, but it's even more useful if we use it to generate random whole numbers.
Use Math.random() to generate a random decimal.
Multiply that random decimal by 20.
Use another function, Math.floor() to round the number down to its nearest whole number.
Remember that Math.random() can never quite return a 1 and, because we're rounding down, it's impossible to actually get 20. This technique will give us a whole number between 0 and 19.
Putting everything together, this is what our code looks like:
Math.floor(Math.random() * 20);
We are calling Math.random(), multiplying the result by 20, then passing the value to Math.floor() function to round the value down to the nearest whole number.
Instructions
Use this technique to generate and return a random whole number between 0 and 9.
Partager