1.5 KiB
1.5 KiB
id, title, challengeType, videoUrl, forumTopicId, dashedName
id | title | challengeType | videoUrl | forumTopicId | dashedName |
---|---|---|---|---|---|
cf1111c1c11feddfaeb9bdef | 使用 JavaScript 生成隨機分數 | 1 | https://scrimba.com/c/cyWJJs3 | 18185 | generate-random-fractions-with-javascript |
--description--
隨機數非常適合用來創建隨機行爲。
在 JavaScript 中,可以用 Math.random()
生成一個在0
(包括 0)到 1
(不包括 1)之間的隨機小數。 因此 Math.random()
可能返回 0
,但絕不會返回 1
。
提示:使用賦值運算符存儲值這一節講過,所有函數調用將在 return
執行之前結束,因此我們可以 return
(返回)Math.random()
函數的值。
--instructions--
更改 randomFraction
,使其返回一個隨機數而不是 0
。
--hints--
randomFraction
應該返回一個隨機數。
assert(typeof randomFraction() === 'number');
randomFraction
應該返回一個小數。
assert((randomFraction() + '').match(/\./g));
需要使用 Math.random
生成隨機的小數。
assert(code.match(/Math\.random/g).length >= 0);
--seed--
--after-user-code--
(function(){return randomFraction();})();
--seed-contents--
function randomFraction() {
// Only change code below this line
return 0;
// Only change code above this line
}
--solutions--
function randomFraction() {
return Math.random();
}