Math.random()
para generar un decimal aleatorio. 20
. Math.floor()
para redondear el número a su número entero más cercano. Math.random()
nunca puede devolver un 1
y, como estamos redondeando, es imposible obtener 20
. Esta técnica nos dará un número entero entre 0
y 19
. Poniendo todo junto, así es como se ve nuestro código: Math.floor(Math.random() * 20);
Llamamos a Math.random()
, multiplicamos el resultado por 20 y luego Math.floor()
el valor a la función Math.floor()
para redondear el valor al número entero más cercano. 0
y 9
. randomWholeNum
debe ser un número entero.
testString: 'assert(typeof randomWholeNum() === "number" && (function(){var r = randomWholeNum();return Math.floor(r) === r;})(), "The result of randomWholeNum
should be a whole number.");'
- text: Debería estar usando Math.random
para generar un número aleatorio.
testString: 'assert(code.match(/Math.random/g).length > 1, "You should be using Math.random
to generate a random number.");'
- text: Debería haber multiplicado el resultado de Math.random
por 10 para que sea un número entre cero y nueve.
testString: 'assert(code.match(/\s*?Math.random\s*?\(\s*?\)\s*?\*\s*?10[\D]\s*?/g) || code.match(/\s*?10\s*?\*\s*?Math.random\s*?\(\s*?\)\s*?/g), "You should have multiplied the result of Math.random
by 10 to make it a number that is between zero and nine.");'
- text: Debe usar Math.floor
para eliminar la parte decimal del número.
testString: 'assert(code.match(/Math.floor/g).length > 1, "You should use Math.floor
to remove the decimal part of the number.");'
```