2018-09-30 23:01:58 +01:00
|
|
|
---
|
|
|
|
id: 5690307fddb111c6084545d7
|
|
|
|
title: Logical Order in If Else Statements
|
|
|
|
challengeType: 1
|
2019-02-14 12:24:02 -05:00
|
|
|
videoUrl: 'https://scrimba.com/c/cwNvMUV'
|
2019-07-31 11:32:23 -07:00
|
|
|
forumTopicId: 18228
|
2018-09-30 23:01:58 +01:00
|
|
|
---
|
|
|
|
|
2020-11-27 19:02:05 +01:00
|
|
|
# --description--
|
|
|
|
|
|
|
|
Order is important in `if`, `else if` statements.
|
|
|
|
|
2018-09-30 23:01:58 +01:00
|
|
|
The function is executed from top to bottom so you will want to be careful of what statement comes first.
|
2020-11-27 19:02:05 +01:00
|
|
|
|
2018-09-30 23:01:58 +01:00
|
|
|
Take these two functions as an example.
|
2020-11-27 19:02:05 +01:00
|
|
|
|
2018-09-30 23:01:58 +01:00
|
|
|
Here's the first:
|
2019-05-17 06:20:30 -07:00
|
|
|
|
|
|
|
```js
|
|
|
|
function foo(x) {
|
|
|
|
if (x < 1) {
|
|
|
|
return "Less than one";
|
|
|
|
} else if (x < 2) {
|
|
|
|
return "Less than two";
|
|
|
|
} else {
|
|
|
|
return "Greater than or equal to two";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2018-09-30 23:01:58 +01:00
|
|
|
And the second just switches the order of the statements:
|
2019-05-17 06:20:30 -07:00
|
|
|
|
|
|
|
```js
|
|
|
|
function bar(x) {
|
|
|
|
if (x < 2) {
|
|
|
|
return "Less than two";
|
|
|
|
} else if (x < 1) {
|
|
|
|
return "Less than one";
|
|
|
|
} else {
|
|
|
|
return "Greater than or equal to two";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2018-09-30 23:01:58 +01:00
|
|
|
While these two functions look nearly identical if we pass a number to both we get different outputs.
|
2019-05-17 06:20:30 -07:00
|
|
|
|
|
|
|
```js
|
|
|
|
foo(0) // "Less than one"
|
|
|
|
bar(0) // "Less than two"
|
|
|
|
```
|
|
|
|
|
2020-11-27 19:02:05 +01:00
|
|
|
# --instructions--
|
2018-09-30 23:01:58 +01:00
|
|
|
|
|
|
|
Change the order of logic in the function so that it will return the correct statements in all cases.
|
|
|
|
|
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
|
|
|
`orderMyLogic(4)` should return "Less than 5"
|
2018-09-30 23:01:58 +01:00
|
|
|
|
2020-11-27 19:02:05 +01:00
|
|
|
```js
|
|
|
|
assert(orderMyLogic(4) === 'Less than 5');
|
|
|
|
```
|
|
|
|
|
|
|
|
`orderMyLogic(6)` should return "Less than 10"
|
|
|
|
|
|
|
|
```js
|
|
|
|
assert(orderMyLogic(6) === 'Less than 10');
|
2018-09-30 23:01:58 +01:00
|
|
|
```
|
|
|
|
|
2020-11-27 19:02:05 +01:00
|
|
|
`orderMyLogic(11)` should return "Greater than or equal to 10"
|
2018-09-30 23:01:58 +01:00
|
|
|
|
2020-11-27 19:02:05 +01:00
|
|
|
```js
|
|
|
|
assert(orderMyLogic(11) === 'Greater than or equal to 10');
|
|
|
|
```
|
|
|
|
|
|
|
|
# --seed--
|
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
|
|
|
|
|
|
|
```js
|
|
|
|
function orderMyLogic(val) {
|
|
|
|
if (val < 10) {
|
|
|
|
return "Less than 10";
|
|
|
|
} else if (val < 5) {
|
|
|
|
return "Less than 5";
|
|
|
|
} else {
|
|
|
|
return "Greater than or equal to 10";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
orderMyLogic(7);
|
|
|
|
```
|
|
|
|
|
2020-11-27 19:02:05 +01:00
|
|
|
# --solutions--
|
2018-09-30 23:01:58 +01:00
|
|
|
|
|
|
|
```js
|
|
|
|
function orderMyLogic(val) {
|
|
|
|
if(val < 5) {
|
2018-10-08 01:01:53 +01:00
|
|
|
return "Less than 5";
|
2018-09-30 23:01:58 +01:00
|
|
|
} else if (val < 10) {
|
|
|
|
return "Less than 10";
|
|
|
|
} else {
|
|
|
|
return "Greater than or equal to 10";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|