2018-10-10 18:03:03 -04:00
|
|
|
---
|
|
|
|
id: cf1391c1c11feddfaeb4bdef
|
|
|
|
challengeType: 1
|
2020-04-29 18:29:13 +08:00
|
|
|
videoUrl: 'https://scrimba.com/c/ca8GEuW'
|
|
|
|
forumTopicId: 16826
|
|
|
|
localeTitle: 创建一个小数
|
2018-10-10 18:03:03 -04:00
|
|
|
---
|
|
|
|
|
|
|
|
## Description
|
2020-04-29 18:29:13 +08:00
|
|
|
<section id='description'>
|
|
|
|
我们也可以把小数存储到变量中。小数也被称作<dfn>浮点数</dfn> 。
|
|
|
|
<strong>提示</strong><br>不是所有的实数都可以用 <dfn>浮点数</dfn> 来表示。因为可能存在四舍五入的错误,<a href="https://en.wikipedia.org/wiki/Floating_point#Accuracy_problems" target="_blank">详情查看</a>。
|
|
|
|
</section>
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
## Instructions
|
2020-04-29 18:29:13 +08:00
|
|
|
<section id='instructions'>
|
|
|
|
创建一个变量<code>myDecimal</code>并给它赋值一个浮点数。(例如<code>5.21</code>)。
|
|
|
|
</section>
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
## Tests
|
|
|
|
<section id='tests'>
|
|
|
|
|
|
|
|
```yml
|
|
|
|
tests:
|
|
|
|
- text: <code>myDecimal</code>应该是一个数字。
|
2020-02-18 01:40:55 +09:00
|
|
|
testString: assert(typeof myDecimal === "number");
|
2020-04-29 18:29:13 +08:00
|
|
|
- text: <code>myDecimal</code>应该包含小数点。
|
|
|
|
testString: assert(myDecimal % 1 != 0);
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
</section>
|
|
|
|
|
|
|
|
## Challenge Seed
|
|
|
|
<section id='challengeSeed'>
|
|
|
|
|
|
|
|
<div id='js-seed'>
|
|
|
|
|
|
|
|
```js
|
|
|
|
var ourDecimal = 5.7;
|
|
|
|
|
|
|
|
// Only change code below this line
|
|
|
|
|
2020-04-29 18:29:13 +08:00
|
|
|
|
2018-10-10 18:03:03 -04:00
|
|
|
```
|
|
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
|
### After Test
|
|
|
|
<div id='js-teardown'>
|
|
|
|
|
|
|
|
```js
|
2020-04-29 18:29:13 +08:00
|
|
|
(function(){if(typeof myDecimal !== "undefined"){return myDecimal;}})();
|
2018-10-10 18:03:03 -04:00
|
|
|
```
|
|
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
</section>
|
|
|
|
|
|
|
|
## Solution
|
|
|
|
<section id='solution'>
|
|
|
|
|
2020-04-29 18:29:13 +08:00
|
|
|
|
2018-10-10 18:03:03 -04:00
|
|
|
```js
|
2020-04-29 18:29:13 +08:00
|
|
|
var myDecimal = 9.9;
|
2018-10-10 18:03:03 -04:00
|
|
|
```
|
2020-04-29 18:29:13 +08:00
|
|
|
|
2018-10-10 18:03:03 -04:00
|
|
|
</section>
|