2018-10-10 18:03:03 -04:00
|
|
|
---
|
|
|
|
id: 56533eb9ac21ba0edf2244b0
|
|
|
|
challengeType: 1
|
2020-04-29 18:29:13 +08:00
|
|
|
videoUrl: 'https://scrimba.com/c/c2Qv7AV'
|
|
|
|
forumTopicId: 16660
|
|
|
|
localeTitle: 复合赋值之 -=
|
2018-10-10 18:03:03 -04:00
|
|
|
---
|
|
|
|
|
|
|
|
## Description
|
2020-04-29 18:29:13 +08:00
|
|
|
<section id='description'>
|
|
|
|
与<code>+=</code>操作符类似,<code>-=</code>操作符用来对一个变量进行减法赋值操作。
|
|
|
|
<code>myVar = myVar - 5;</code>
|
|
|
|
变量<code>myVar</code>等于自身减去<code>5</code>的值。也可以写成这种形式:
|
|
|
|
<code>myVar -= 5;</code>
|
|
|
|
</section>
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
## Instructions
|
2020-04-29 18:29:13 +08:00
|
|
|
<section id='instructions'>
|
|
|
|
使用<code>-=</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>a</code>应该等于<code>5</code>。
|
2020-02-18 01:40:55 +09:00
|
|
|
testString: assert(a === 5);
|
2020-04-29 18:29:13 +08:00
|
|
|
- text: <code>b</code>应该等于<code>-6</code>。
|
2020-02-18 01:40:55 +09:00
|
|
|
testString: assert(b === -6);
|
2020-04-29 18:29:13 +08:00
|
|
|
- text: <code>c</code>应该等于<code>2</code>。
|
2020-02-18 01:40:55 +09:00
|
|
|
testString: assert(c === 2);
|
2020-04-29 18:29:13 +08:00
|
|
|
- text: 应该对每个变量使用<code>-=</code>操作符。
|
2020-02-18 01:40:55 +09:00
|
|
|
testString: assert(code.match(/-=/g).length === 3);
|
2020-04-29 18:29:13 +08:00
|
|
|
- text: 不要修改注释上面的代码。
|
2020-02-18 01:40:55 +09:00
|
|
|
testString: assert(/var a = 11;/.test(code) && /var b = 9;/.test(code) && /var c = 3;/.test(code));
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
</section>
|
|
|
|
|
|
|
|
## Challenge Seed
|
|
|
|
<section id='challengeSeed'>
|
|
|
|
|
|
|
|
<div id='js-seed'>
|
|
|
|
|
|
|
|
```js
|
|
|
|
var a = 11;
|
|
|
|
var b = 9;
|
|
|
|
var c = 3;
|
|
|
|
|
|
|
|
// Only modify code below this line
|
|
|
|
|
|
|
|
a = a - 6;
|
|
|
|
b = b - 15;
|
|
|
|
c = c - 1;
|
|
|
|
|
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(a,b,c){ return "a = " + a + ", b = " + b + ", c = " + c; })(a,b,c);
|
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 a = 11;
|
|
|
|
var b = 9;
|
|
|
|
var c = 3;
|
|
|
|
|
|
|
|
a -= 6;
|
|
|
|
b -= 15;
|
|
|
|
c -= 1;
|
|
|
|
|
|
|
|
|
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>
|