2018-10-10 22:03:03 +00:00
|
|
|
---
|
|
|
|
id: 56533eb9ac21ba0edf2244a9
|
2021-03-04 17:49:46 +00:00
|
|
|
title: 使用赋值运算符初始化变量
|
2018-10-10 22:03:03 +00:00
|
|
|
challengeType: 1
|
2020-04-29 10:29:13 +00:00
|
|
|
videoUrl: 'https://scrimba.com/c/cWJ4Bfb'
|
|
|
|
forumTopicId: 301171
|
2021-01-13 02:31:00 +00:00
|
|
|
dashedName: initializing-variables-with-the-assignment-operator
|
2018-10-10 22:03:03 +00:00
|
|
|
---
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
# --description--
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2021-03-04 17:49:46 +00:00
|
|
|
通常在声明变量的时候会给变量<dfn>初始化</dfn>一个初始值。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
`var myVar = 0;`
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2021-03-04 17:49:46 +00:00
|
|
|
创建一个名为 `myVar` 的变量,并指定其初始值 `0`。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
# --instructions--
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2021-03-04 17:49:46 +00:00
|
|
|
通过关键字 `var` 定义一个变量 `a`,并给它一个初始值 `9`。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
# --hints--
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2021-03-04 17:49:46 +00:00
|
|
|
你需要初始化 `a` 的值为 `9`。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
|
|
```js
|
2021-02-06 04:42:36 +00:00
|
|
|
assert(/var\s+a\s*=\s*9(\s*;?\s*)$/.test(code));
|
2018-10-10 22:03:03 +00:00
|
|
|
```
|
|
|
|
|
2021-01-13 02:31:00 +00:00
|
|
|
# --seed--
|
|
|
|
|
|
|
|
## --after-user-code--
|
|
|
|
|
|
|
|
```js
|
|
|
|
if(typeof a !== 'undefined') {(function(a){return "a = " + a;})(a);} else { (function() {return 'a is undefined';})(); }
|
|
|
|
```
|
|
|
|
|
|
|
|
## --seed-contents--
|
2021-03-05 02:55:32 +00:00
|
|
|
|
2021-01-13 02:31:00 +00:00
|
|
|
```js
|
2021-03-05 02:55:32 +00:00
|
|
|
|
2021-01-13 02:31:00 +00:00
|
|
|
```
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
# --solutions--
|
2020-04-29 10:29:13 +00:00
|
|
|
|
2021-01-13 02:31:00 +00:00
|
|
|
```js
|
|
|
|
var a = 9;
|
|
|
|
```
|