2018-09-30 23:01:58 +01:00
---
id: 56533eb9ac21ba0edf2244da
title: Introducing Else Statements
challengeType: 1
2019-02-14 12:24:02 -05:00
videoUrl: 'https://scrimba.com/c/cek4Efq'
2018-09-30 23:01:58 +01:00
---
## Description
< section id = 'description' >
When a condition for an < code > if< / code > statement is true, the block of code following it is executed. What about when that condition is false? Normally nothing would happen. With an < code > else< / code > statement, an alternate block of code can be executed.
< blockquote > if (num > 10) {< br > return "Bigger than 10";< br > } else {< br > return "10 or Less";< br > }< / blockquote >
< / section >
## Instructions
< section id = 'instructions' >
Combine the < code > if< / code > statements into a single < code > if/else< / code > statement.
< / section >
## Tests
< section id = 'tests' >
```yml
2018-10-04 14:37:37 +01:00
tests:
- text: You should only have one < code > if</ code > statement in the editor
2018-10-20 21:02:47 +03:00
testString: assert(code.match(/if/g).length === 1, 'You should only have one < code > if< / code > statement in the editor');
2018-10-04 14:37:37 +01:00
- text: You should use an < code > else</ code > statement
2018-10-20 21:02:47 +03:00
testString: assert(/else/g.test(code), 'You should use an < code > else< / code > statement');
2018-10-04 14:37:37 +01:00
- text: < code > testElse(4)</ code > should return "5 or Smaller"
2018-10-20 21:02:47 +03:00
testString: assert(testElse(4) === "5 or Smaller", '< code > testElse(4)< / code > should return "5 or Smaller"');
2018-10-04 14:37:37 +01:00
- text: < code > testElse(5)</ code > should return "5 or Smaller"
2018-10-20 21:02:47 +03:00
testString: assert(testElse(5) === "5 or Smaller", '< code > testElse(5)< / code > should return "5 or Smaller"');
2018-10-04 14:37:37 +01:00
- text: < code > testElse(6)</ code > should return "Bigger than 5"
2018-10-20 21:02:47 +03:00
testString: assert(testElse(6) === "Bigger than 5", '< code > testElse(6)< / code > should return "Bigger than 5"');
2018-10-04 14:37:37 +01:00
- text: < code > testElse(10)</ code > should return "Bigger than 5"
2018-10-20 21:02:47 +03:00
testString: assert(testElse(10) === "Bigger than 5", '< code > testElse(10)< / code > should return "Bigger than 5"');
2018-10-04 14:37:37 +01:00
- text: Do not change the code above or below the lines.
2018-10-20 21:02:47 +03:00
testString: assert(/var result = "";/.test(code) & & /return result;/.test(code), 'Do not change the code above or below the lines.');
2018-09-30 23:01:58 +01:00
```
< / section >
## Challenge Seed
< section id = 'challengeSeed' >
< div id = 'js-seed' >
```js
function testElse(val) {
var result = "";
// Only change code below this line
2018-10-08 01:01:53 +01:00
2018-09-30 23:01:58 +01:00
if (val > 5) {
result = "Bigger than 5";
}
2018-10-08 01:01:53 +01:00
2018-09-30 23:01:58 +01:00
if (val < = 5) {
result = "5 or Smaller";
}
2018-10-08 01:01:53 +01:00
2018-09-30 23:01:58 +01:00
// Only change code above this line
return result;
}
// Change this value to test
testElse(4);
```
< / div >
< / section >
## Solution
< section id = 'solution' >
```js
function testElse(val) {
var result = "";
if(val > 5) {
result = "Bigger than 5";
} else {
result = "5 or Smaller";
}
return result;
}
```
< / section >