Adrian Skar 82459dad13 [Guide] Basic JS: Ternary operator. Fixes and enhancements (#22633)
1. Fix code solution (it used the assignment operator to compare _a_ and _b_ instead of the comparison one; which wouldn't pass the test at the exercise page).
2. Add problem explanation, hint, code explanation, run example and resources
2018-11-22 00:36:11 +07:00

1.0 KiB

title
title
Use the Conditional (Ternary) Operator

Use the Conditional (Ternary) Operator

Problem explanation:

Use the conditional operator in the checkEqual function to check if two numbers are equal or not. The function should return either true or false.

Hint 1

Remember that the "traditional" if...else syntax can be re-written using the conditional operator (condition ? statement if true : statement if false;)

try to solve the problem now

Spoiler alert!

Solution ahead!

Code solution:

function checkEqual(a, b) {
  return a === b ? true : false;
}

· Run code at repl.it.

Code explanation

  • The function checks if the condition before the interrogation sign (?) is true, and if so, executes the true statement. Otherwise, it returns false.

Resources