2018-10-10 18:03:03 -04:00
---
id: 56533eb9ac21ba0edf2244da
2021-02-06 04:42:36 +00:00
title: Introducing Else Statements
2018-10-10 18:03:03 -04:00
challengeType: 1
2020-04-29 18:29:13 +08:00
videoUrl: 'https://scrimba.com/c/cek4Efq'
forumTopicId: 18207
2021-01-13 03:31:00 +01:00
dashedName: introducing-else-statements
2018-10-10 18:03:03 -04:00
---
2020-12-16 00:37:30 -07:00
# --description--
2021-02-06 04:42:36 +00:00
When a condition for an `if` statement is true, the block of code following it is executed. What about when that condition is false? Normally nothing would happen. With an `else` statement, an alternate block of code can be executed.
2020-04-29 18:29:13 +08:00
```js
if (num > 10) {
return "Bigger than 10";
} else {
return "10 or Less";
}
```
2020-12-16 00:37:30 -07:00
# --instructions--
2018-10-10 18:03:03 -04:00
2021-02-06 04:42:36 +00:00
Combine the `if` statements into a single `if/else` statement.
2018-10-10 18:03:03 -04:00
2020-12-16 00:37:30 -07:00
# --hints--
2018-10-10 18:03:03 -04:00
2021-02-06 04:42:36 +00:00
You should only have one `if` statement in the editor
2018-10-10 18:03:03 -04:00
```js
2020-12-16 00:37:30 -07:00
assert(code.match(/if/g).length === 1);
```
2018-10-10 18:03:03 -04:00
2021-02-06 04:42:36 +00:00
You should use an `else` statement
2018-10-10 18:03:03 -04:00
2020-12-16 00:37:30 -07:00
```js
assert(/else/g.test(code));
```
2018-10-10 18:03:03 -04:00
2021-02-06 04:42:36 +00:00
`testElse(4)` should return "5 or Smaller"
2018-10-10 18:03:03 -04:00
2020-12-16 00:37:30 -07:00
```js
assert(testElse(4) === '5 or Smaller');
2018-10-10 18:03:03 -04:00
```
2021-02-06 04:42:36 +00:00
`testElse(5)` should return "5 or Smaller"
2018-10-10 18:03:03 -04:00
2020-12-16 00:37:30 -07:00
```js
assert(testElse(5) === '5 or Smaller');
```
2018-10-10 18:03:03 -04:00
2021-02-06 04:42:36 +00:00
`testElse(6)` should return "Bigger than 5"
2020-12-16 00:37:30 -07:00
```js
assert(testElse(6) === 'Bigger than 5');
```
2018-10-10 18:03:03 -04:00
2021-02-06 04:42:36 +00:00
`testElse(10)` should return "Bigger than 5".
2018-10-10 18:03:03 -04:00
2020-12-16 00:37:30 -07:00
```js
assert(testElse(10) === 'Bigger than 5');
```
2018-10-10 18:03:03 -04:00
2021-02-06 04:42:36 +00:00
You should not change the code above or below the specified comments.
2020-04-29 18:29:13 +08:00
2018-10-10 18:03:03 -04:00
```js
2020-12-16 00:37:30 -07:00
assert(/var result = "";/.test(code) & & /return result;/.test(code));
2018-10-10 18:03:03 -04:00
```
2020-04-29 18:29:13 +08:00
2021-01-13 03:31:00 +01:00
# --seed--
## --seed-contents--
```js
function testElse(val) {
var result = "";
// Only change code below this line
if (val > 5) {
result = "Bigger than 5";
}
if (val < = 5) {
result = "5 or Smaller";
}
// Only change code above this line
return result;
}
testElse(4);
```
2020-12-16 00:37:30 -07:00
# --solutions--
2021-01-13 03:31:00 +01:00
```js
function testElse(val) {
var result = "";
if(val > 5) {
result = "Bigger than 5";
} else {
result = "5 or Smaller";
}
return result;
}
```