From 82459dad1365b94e2e25629e938411a1b28037e0 Mon Sep 17 00:00:00 2001 From: Adrian Skar Date: Wed, 21 Nov 2018 18:36:11 +0100 Subject: [PATCH] [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 --- .../index.md | 33 +++++++++++++++---- 1 file changed, 27 insertions(+), 6 deletions(-) diff --git a/guide/english/certifications/javascript-algorithms-and-data-structures/basic-javascript/use-the-conditional-ternary-operator/index.md b/guide/english/certifications/javascript-algorithms-and-data-structures/basic-javascript/use-the-conditional-ternary-operator/index.md index cd91ee7a63..3ccd4b70b2 100644 --- a/guide/english/certifications/javascript-algorithms-and-data-structures/basic-javascript/use-the-conditional-ternary-operator/index.md +++ b/guide/english/certifications/javascript-algorithms-and-data-structures/basic-javascript/use-the-conditional-ternary-operator/index.md @@ -3,15 +3,36 @@ title: Use the Conditional (Ternary) Operator --- ## Use the Conditional (Ternary) Operator -### Hint 1 -Use ternary operator to check for equality. +### 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._ -### Warning Solution Ahead!!! +#### 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: ```javascript function checkEqual(a, b) { - return (a == b ? true : false ); + return a === b ? true : false; } - -checkEqual(1, 2); ``` +ยท Run code at [repl.it](https://repl.it/@AdrianSkar/Basic-JS-Ternary-operator). + +### 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 + +- ["Conditional (ternary) operator" - *MDN JavaScript reference*](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator) + + + +