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