2018-10-10 18:03:03 -04:00
|
|
|
---
|
|
|
|
id: 56533eb9ac21ba0edf2244ad
|
|
|
|
challengeType: 1
|
2020-04-29 18:29:13 +08:00
|
|
|
videoUrl: 'https://scrimba.com/c/cM2KeS2'
|
|
|
|
forumTopicId: 17558
|
2020-10-01 17:54:21 +02:00
|
|
|
title: 数字递减
|
2018-10-10 18:03:03 -04:00
|
|
|
---
|
|
|
|
|
|
|
|
## Description
|
2020-04-29 18:29:13 +08:00
|
|
|
<section id='description'>
|
|
|
|
使用自减符号<code>--</code>,你可以很方便地对一个变量执行<dfn>自减</dfn>或者<code>-1</code>运算。
|
|
|
|
<code>i--;</code>
|
|
|
|
等效于
|
|
|
|
<code>i = i - 1;</code>
|
|
|
|
<strong>提示</strong><br><code>i--;</code>这种写法,省去了书写<code>=</code>符号的必要。
|
|
|
|
</section>
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
## Instructions
|
2020-04-29 18:29:13 +08:00
|
|
|
<section id='instructions'>
|
|
|
|
重写代码,使用<code>--</code>符号对<code>myVar</code>执行自减操作。
|
|
|
|
</section>
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
## Tests
|
|
|
|
<section id='tests'>
|
|
|
|
|
|
|
|
```yml
|
|
|
|
tests:
|
2020-04-29 18:29:13 +08:00
|
|
|
- text: <code>myVar</code>应该等于<code>10</code>。
|
2020-02-18 01:40:55 +09:00
|
|
|
testString: assert(myVar === 10);
|
2020-04-29 18:29:13 +08:00
|
|
|
- text: <code>myVar = myVar - 1;</code>语句应该被修改。
|
2020-02-18 01:40:55 +09:00
|
|
|
testString: assert(/var\s*myVar\s*=\s*11;\s*\/*.*\s*([-]{2}\s*myVar|myVar\s*[-]{2});/.test(code));
|
2020-04-29 18:29:13 +08:00
|
|
|
- text: 对<code>myVar</code>使用<code>--</code>运算符。
|
2020-02-18 01:40:55 +09:00
|
|
|
testString: assert(/[-]{2}\s*myVar|myVar\s*[-]{2}/.test(code));
|
2020-04-29 18:29:13 +08:00
|
|
|
- text: 不要修改注释上面的代码。
|
2020-02-18 01:40:55 +09:00
|
|
|
testString: assert(/var myVar = 11;/.test(code));
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
</section>
|
|
|
|
|
|
|
|
## Challenge Seed
|
|
|
|
<section id='challengeSeed'>
|
|
|
|
|
|
|
|
<div id='js-seed'>
|
|
|
|
|
|
|
|
```js
|
|
|
|
var myVar = 11;
|
|
|
|
|
|
|
|
// Only change code below this line
|
|
|
|
myVar = myVar - 1;
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
|
### After Test
|
|
|
|
<div id='js-teardown'>
|
|
|
|
|
|
|
|
```js
|
2020-04-29 18:29:13 +08:00
|
|
|
(function(z){return 'myVar = ' + z;})(myVar);
|
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 myVar = 11;
|
|
|
|
myVar--;
|
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>
|