|
| 1 | +# Concatenate conditions |
| 2 | + |
| 3 | +Furthermore you can concatenate different conditions with "or” or “and” statements, to test whether either statement is true, or both are true, respectively. |
| 4 | + |
| 5 | +In JavaScript “or” is written as `||` and “and” is written as `&&`. |
| 6 | + |
| 7 | +Say you want to test if the value of x is between 10 and 20—you could do that with a condition stating: |
| 8 | + |
| 9 | +``` |
| 10 | +if(x > 10 && x < 20) { |
| 11 | + ... |
| 12 | +} |
| 13 | +``` |
| 14 | + |
| 15 | +If you want to make sure that country is either “England” or “Germany” you use: |
| 16 | + |
| 17 | +``` |
| 18 | +if(country === 'England' || country === 'Germany') { |
| 19 | + ... |
| 20 | +} |
| 21 | +``` |
| 22 | + |
| 23 | +**Note**: Just like operations on numbers, Condtions can be grouped using parenthesis, ex: ```if ( (name == "John" || name == "Jennifer") && country == "France")```. |
| 24 | + |
| 25 | +--- |
| 26 | + |
| 27 | +Fill up the 2 conditions so that `primaryCategory` equals `"E/J"` only if name equals `"John"` and country is `"England"`, and so that `secondaryCategory` equals `"E|J"` only if name equals `"John"` or country is `"England"` |
| 28 | + |
| 29 | +```js |
| 30 | +var name = "John"; |
| 31 | +var country = "England"; |
| 32 | +var primaryCategory, secondaryCategory; |
| 33 | + |
| 34 | +if ( /* Fill here */ ) { |
| 35 | + primaryCategory = "E/J"; |
| 36 | +} |
| 37 | +if ( /* Fill here */ ) { |
| 38 | + secondaryCategory = "E|J"; |
| 39 | +} |
| 40 | +``` |
| 41 | + |
| 42 | +```js |
| 43 | +var name = "John"; |
| 44 | +var country = "England"; |
| 45 | +var primaryCategory, secondaryCategory; |
| 46 | + |
| 47 | +if (name == "John" && country == "England") { |
| 48 | + primaryCategory = "E/J"; |
| 49 | +} |
| 50 | +if (name == "John" || country == "England") { |
| 51 | + secondaryCategory = "E|J"; |
| 52 | +} |
| 53 | +``` |
| 54 | + |
| 55 | +```js |
| 56 | +assert(primaryCategory == "E/J" && secondaryCategory == "E|J"); |
| 57 | +``` |
| 58 | + |
| 59 | +--- |
0 commit comments