Oliver Eyton-Williams ee1e8abd87
feat(curriculum): restore seed + solution to Chinese (#40683)
* feat(tools): add seed/solution restore script

* chore(curriculum): remove empty sections' markers

* chore(curriculum): add seed + solution to Chinese

* chore: remove old formatter

* fix: update getChallenges

parse translated challenges separately, without reference to the source

* chore(curriculum): add dashedName to English

* chore(curriculum): add dashedName to Chinese

* refactor: remove unused challenge property 'name'

* fix: relax dashedName requirement

* fix: stray tag

Remove stray `pre` tag from challenge file.

Signed-off-by: nhcarrigan <nhcarrigan@gmail.com>

Co-authored-by: nhcarrigan <nhcarrigan@gmail.com>
2021-01-12 19:31:00 -07:00

71 lines
1.4 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
id: cf1111c1c11feddfaeb9bdef
title: 使用 JavaScript 生成随机分数
challengeType: 1
videoUrl: 'https://scrimba.com/c/cyWJJs3'
forumTopicId: 18185
dashedName: generate-random-fractions-with-javascript
---
# --description--
随机数非常适合用来创建随机行为。
`Math.random()`用来生成一个在`0`(包括 0`1`(不包括 1之间的随机小数因此`Math.random()`可能返回 0 但绝不会返回 1。
**提示**
[使用赋值运算符存储值](storing-values-with-the-assignment-operator)这一节讲过,所有函数调用将在`return`执行之前解析,因此我们可以返回`Math.random()`函数的值。
# --instructions--
更改`randomFraction`使其返回一个随机数而不是`0`
# --hints--
`randomFraction`应该返回一个随机数。
```js
assert(typeof randomFraction() === 'number');
```
`randomFraction`应该返回一个小数。
```js
assert((randomFraction() + '').match(/\./g));
```
需要使用`Math.random`生成随机的小数。
```js
assert(code.match(/Math\.random/g).length >= 0);
```
# --seed--
## --after-user-code--
```js
(function(){return randomFraction();})();
```
## --seed-contents--
```js
function randomFraction() {
// Only change code below this line
return 0;
// Only change code above this line
}
```
# --solutions--
```js
function randomFraction() {
return Math.random();
}
```