2018-09-30 23:01:58 +01:00
|
|
|
---
|
|
|
|
id: 56533eb9ac21ba0edf2244ac
|
|
|
|
title: Increment a Number with JavaScript
|
|
|
|
challengeType: 1
|
2019-02-14 12:24:02 -05:00
|
|
|
videoUrl: 'https://scrimba.com/c/ca8GLT9'
|
2019-07-31 11:32:23 -07:00
|
|
|
forumTopicId: 18201
|
2021-01-13 03:31:00 +01:00
|
|
|
dashedName: increment-a-number-with-javascript
|
2018-09-30 23:01:58 +01:00
|
|
|
---
|
|
|
|
|
2020-11-27 19:02:05 +01:00
|
|
|
# --description--
|
|
|
|
|
|
|
|
You can easily <dfn>increment</dfn> or add one to a variable with the `++` operator.
|
|
|
|
|
|
|
|
`i++;`
|
|
|
|
|
2018-09-30 23:01:58 +01:00
|
|
|
is the equivalent of
|
|
|
|
|
2020-11-27 19:02:05 +01:00
|
|
|
`i = i + 1;`
|
|
|
|
|
|
|
|
**Note**
|
|
|
|
The entire line becomes `i++;`, eliminating the need for the equal sign.
|
|
|
|
|
|
|
|
# --instructions--
|
2018-09-30 23:01:58 +01:00
|
|
|
|
2020-11-27 19:02:05 +01:00
|
|
|
Change the code to use the `++` operator on `myVar`.
|
2018-09-30 23:01:58 +01:00
|
|
|
|
2020-11-27 19:02:05 +01:00
|
|
|
# --hints--
|
2018-09-30 23:01:58 +01:00
|
|
|
|
2020-11-27 19:02:05 +01:00
|
|
|
`myVar` should equal `88`.
|
2018-09-30 23:01:58 +01:00
|
|
|
|
|
|
|
```js
|
2020-11-27 19:02:05 +01:00
|
|
|
assert(myVar === 88);
|
|
|
|
```
|
2018-09-30 23:01:58 +01:00
|
|
|
|
2020-11-27 19:02:05 +01:00
|
|
|
You should not use the assignment operator.
|
|
|
|
|
|
|
|
```js
|
|
|
|
assert(
|
|
|
|
/var\s*myVar\s*=\s*87;\s*\/*.*\s*([+]{2}\s*myVar|myVar\s*[+]{2});/.test(code)
|
|
|
|
);
|
|
|
|
```
|
|
|
|
|
|
|
|
You should use the `++` operator.
|
2018-09-30 23:01:58 +01:00
|
|
|
|
2020-11-27 19:02:05 +01:00
|
|
|
```js
|
|
|
|
assert(/[+]{2}\s*myVar|myVar\s*[+]{2}/.test(code));
|
2018-09-30 23:01:58 +01:00
|
|
|
```
|
|
|
|
|
2020-11-27 19:02:05 +01:00
|
|
|
You should not change code above the specified comment.
|
|
|
|
|
|
|
|
```js
|
|
|
|
assert(/var myVar = 87;/.test(code));
|
|
|
|
```
|
2018-09-30 23:01:58 +01:00
|
|
|
|
2020-11-27 19:02:05 +01:00
|
|
|
# --seed--
|
2018-09-30 23:01:58 +01:00
|
|
|
|
2020-11-27 19:02:05 +01:00
|
|
|
## --after-user-code--
|
2018-09-30 23:01:58 +01:00
|
|
|
|
|
|
|
```js
|
2018-10-20 21:02:47 +03:00
|
|
|
(function(z){return 'myVar = ' + z;})(myVar);
|
2018-09-30 23:01:58 +01:00
|
|
|
```
|
|
|
|
|
2020-11-27 19:02:05 +01:00
|
|
|
## --seed-contents--
|
2018-09-30 23:01:58 +01:00
|
|
|
|
2020-11-27 19:02:05 +01:00
|
|
|
```js
|
|
|
|
var myVar = 87;
|
2018-09-30 23:01:58 +01:00
|
|
|
|
2020-11-27 19:02:05 +01:00
|
|
|
// Only change code below this line
|
|
|
|
myVar = myVar + 1;
|
|
|
|
```
|
2018-09-30 23:01:58 +01:00
|
|
|
|
2020-11-27 19:02:05 +01:00
|
|
|
# --solutions--
|
2018-09-30 23:01:58 +01:00
|
|
|
|
|
|
|
```js
|
|
|
|
var myVar = 87;
|
|
|
|
myVar++;
|
|
|
|
```
|