2018-10-12 15:37:13 -04:00
|
|
|
---
|
|
|
|
title: Use the Conditional (Ternary) Operator
|
|
|
|
---
|
2019-03-02 05:10:29 +05:30
|
|
|
|
2019-07-24 00:59:27 -07:00
|
|
|
# Use the Conditional (Ternary) Operator
|
2019-03-02 05:10:29 +05:30
|
|
|
|
2019-07-24 00:59:27 -07:00
|
|
|
---
|
|
|
|
## Problem Explanation
|
2019-03-02 05:10:29 +05:30
|
|
|
|
|
|
|
* You need to write a function named `checkEqual`, which checks if the two parameters are equal.
|
2019-03-03 22:43:12 +05:30
|
|
|
* If the parameters are equal, `Equal` is to be returned else `Not Equal` should be returned.
|
2019-03-02 05:10:29 +05:30
|
|
|
|
|
|
|
|
2019-07-24 00:59:27 -07:00
|
|
|
---
|
|
|
|
## Hints
|
2018-10-12 15:37:13 -04:00
|
|
|
|
2019-07-24 00:59:27 -07:00
|
|
|
### Hint 1
|
|
|
|
|
|
|
|
Use ternary operator to check for equality.
|
2018-11-21 18:36:11 +01:00
|
|
|
|
|
|
|
|
2019-07-24 00:59:27 -07:00
|
|
|
---
|
|
|
|
## Solutions
|
2018-11-21 18:36:11 +01:00
|
|
|
|
2019-07-24 00:59:27 -07:00
|
|
|
<details><summary>Solution 1 (Click to Show/Hide)</summary>
|
2018-11-21 18:36:11 +01:00
|
|
|
|
2018-10-12 15:37:13 -04:00
|
|
|
```javascript
|
|
|
|
function checkEqual(a, b) {
|
2019-03-02 13:39:15 +08:00
|
|
|
return a === b ? "Equal" : "Not Equal";
|
2018-10-12 15:37:13 -04:00
|
|
|
}
|
|
|
|
```
|
2018-11-21 18:36:11 +01:00
|
|
|
|
2019-07-24 00:59:27 -07:00
|
|
|
#### Code Explanation
|
2018-11-21 18:36:11 +01:00
|
|
|
|
2019-03-02 05:10:29 +05:30
|
|
|
* A function `checkEqual` is declared, it accepts two parameters in variables `a` and `b`.
|
|
|
|
* The `return` statement would return the value of the evaluated ternary expression.
|
2019-03-02 13:39:15 +08:00
|
|
|
* The ternary expression checks if `a` and `b` are equal or not and returns `Equal` or `Not Equal` respectively.
|
2019-07-24 00:59:27 -07:00
|
|
|
</details>
|