Files
wehub-resource-sync dde272c4b8
i18n - Build Validation / Validate i18n Builds (24) (push) Has been cancelled
CI - Node.js / Lint (24) (push) Has been cancelled
CI - Node.js / Build (24) (push) Has been cancelled
CI - Node.js / Test (24) (push) Has been cancelled
CI - Node.js / Test - Upcoming Changes (24) (push) Has been cancelled
CI - Node.js / Test - i18n (italian, 24) (push) Has been cancelled
CI - Node.js / Test - i18n (portuguese, 24) (push) Has been cancelled
CD - Docker - GHCR Images / Build and Push Images (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 11:55:53 +08:00

109 lines
2.0 KiB
Markdown

---
id: 5690307fddb111c6084545d7
title: Logical Order in If Else Statements
challengeType: 1
forumTopicId: 18228
dashedName: logical-order-in-if-else-statements
---
# --description--
Order is important in `if`, `else if` statements.
The function is executed from top to bottom so you will want to be careful of what statement comes first.
Take these two functions as an example.
Here's the first:
```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";
}
}
```
And the second just switches the order of the statements:
```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";
}
}
```
While these two functions look nearly identical if we pass a number to both we get different outputs.
```js
foo(0)
bar(0)
```
`foo(0)` will return the string `Less than one`, and `bar(0)` will return the string `Less than two`.
# --instructions--
Change the order of logic in the function so that it will return the correct statements in all cases.
# --hints--
`orderMyLogic(4)` should return the string `Less than 5`
```js
assert(orderMyLogic(4) === 'Less than 5');
```
`orderMyLogic(6)` should return the string `Less than 10`
```js
assert(orderMyLogic(6) === 'Less than 10');
```
`orderMyLogic(11)` should return the string `Greater than or equal to 10`
```js
assert(orderMyLogic(11) === 'Greater than or equal to 10');
```
# --seed--
## --seed-contents--
```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);
```
# --solutions--
```js
function orderMyLogic(val) {
if(val < 5) {
return "Less than 5";
} else if (val < 10) {
return "Less than 10";
} else {
return "Greater than or equal to 10";
}
}
```