Use the Conditional (Ternary) Operator
Problem Explanation
- You need to write a function named
checkEqual
, which checks if the two parameters are equal. - If the parameters are equal,
Equal
is to be returned elseNot Equal
should be returned.
Hints
Hint 1
Use ternary operator to check for equality.
Solutions
Solution 1 (Click to Show/Hide)
function checkEqual(a, b) {
return a === b ? "Equal" : "Not Equal";
}
Code Explanation
- A function
checkEqual
is declared, it accepts two parameters in variablesa
andb
. - The
return
statement would return the value of the evaluated ternary expression. - The ternary expression checks if
a
andb
are equal or not and returnsEqual
orNot Equal
respectively.