Math.random()
для генерации случайного десятичного знака. 20
. Math.floor()
чтобы округлить число до его ближайшего целого числа. Math.random()
никогда не может полностью вернуть 1
и, поскольку мы округливаем, на самом деле получить 20
невозможно. Этот метод даст нам целое число от 0
до 19
. Соединяя все вместе, это выглядит как наш код: Math.floor(Math.random() * 20);
Мы вызываем Math.random()
, умножая результат на 20, затем передавая значение функции Math.floor()
чтобы округлить значение до ближайшего целого числа.
0
до 9
.
randomWholeNum
should be a whole number.
testString: assert(typeof randomWholeNum() === "number" && (function(){var r = randomWholeNum();return Math.floor(r) === r;})());
- text: You should be using Math.random
to generate a random number.
testString: assert(code.match(/Math.random/g).length > 1);
- text: You should have multiplied the result of Math.random
by 10 to make it a number that is between zero and nine.
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));
- text: You should use Math.floor
to remove the decimal part of the number.
testString: assert(code.match(/Math.floor/g).length > 1);
```