Math.random()生成随机小数。 20 。 Math.floor()将数字向下舍入到最接近的整数。 Math.random()永远不会返回1 ,因为我们正在向下舍入,实际上不可能得到20 。这项技术将给我们一个0到19之间的整数。将所有内容放在一起,这就是我们的代码: Math.floor(Math.random() * 20);我们调用Math.random() ,将结果乘以20,然后将值传递给Math.floor()函数,将值向下舍入到最接近的整数。 0到9之间的随机整数。 randomWholeNum的结果应该是整数。
testString: assert(typeof randomWholeNum() === "number" && (function(){var r = randomWholeNum();return Math.floor(r) === r;})());
- text: 您应该使用Math.random来生成随机数。
testString: assert(code.match(/Math.random/g).length > 1);
- text: 您应该将Math.random的结果乘以10,使其成为介于0和9之间的数字。
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: 您应该使用Math.floor删除数字的小数部分。
testString: assert(code.match(/Math.floor/g).length > 1);
```