Using Nested Ternary Operators

A somewhat painful triple-nested ternary operator

I think you can have this to avoid the deep nesting:

var H

if(C == 0){
H = null;
}
else if(V == r){
H = (g - b) / C;
}
else if (V == g){
H = (b - r) / C + 2;
}
else {
H = (r - g) / C + 4;
}

How does this nested ternary operator work using javascript?

but when does allColumns is assigned to columns?

In the remaining case, i.e. where both isUser1Readable and isUser2Readable are true.

The chained ternary expression can be interpreted as an if ... else if ... else sequence:

let columns;
if (!isUser1Readable) {
columns = allColumns.filter(column => !columnIdsUser1.includes(column.id));
} else if (!isUser2Readable) {
columns = allColumns.filter(column => !columnIdsUser2.includes(column.id));
} else {
columns = allColumns;
}

How to write nested ternary operator in react?

If you do complicated calculation do not put it in your render , put it before , it's way simpler, that's why you can't see your mistake.

Try something like this (check how it's written though, not sure it's 100% ok)

const getStyle = (item) => {
if (item.settings && 'backdropSettings' in item.settings) {
const { backdropSettings } = item.settings;

if (backdropSettings.value.includes('#')) {
return `backgroundImage:${item.settings.backdropSettings.value}`
} else {
return `backgroundImage:url(${item.settings.backdropSettings.value})`
}
}
return null
}

return(
<div style={getStyle(item)}>
</div>
);

This is way more readable and you can find your mistake easily. Hope it helps.

How To Use Nested Ternary Operator In C#?

The ternary operator is not a good fit for your problem. It is used to set the value of one variable to one of two values, based on a predicate:

var thingToSet = predicateA ? 
ifPredicateAIsTrue :
ifPredicateAIsFalse;

This is the same as:

if (predicateA)
thingToSet = ifPredicateAIsTrue;
else
thingToSet = ifPredicateAIsFalse;

To nest ternary expressions, place a new ternary expression in the value to set:

var otherThingToSet = predicateB ? (
predicateC ?
ifPredicateCIsTrue :
ifPredicateCIsFalse
) : (
predicateD ?
ifPredicateDIsTrue :
ifPredicateDIsFalse
);

This is equivalent to:

if (predicateB)
{
if (predicateC)
otherThingToSet = ifPredicateCIsTrue;
else
otherThingToSet = ifPredicateCIsFalse;
}
else
{
if (predicateD)
otherThingToSet = ifPredicateDIsTrue;
else
otherThingToSet = ifPredicateDIsFalse;
}

As you can see, this is not really a good fit for your problem, as you're trying to set the value of several variables, based on the exception message.

A better fit for your problem would be a switch statement:

switch (ex.Message)
{
case "Parent Menu Title Required":
metroLabel4.Visible = true;
metroLabel5.Visible = false;
metroLabel6.Visible = false;
metroLabel4.Text = ex.Message;
break;
case "Menu Title Required":
metroLabel4.Visible = false;
metroLabel5.Visible = true;
metroLabel6.Visible = false;
metroLabel5.Text = ex.Message;
break;
case "Form Name Required":
metroLabel4.Visible = false;
metroLabel5.Visible = false;
metroLabel6.Visible = true;
metroLabel6.Text = ex.Message;
break;
default:
metroLabel4.Visible = true;
metroLabel5.Visible = true;
metroLabel6.Visible = true;
metroLabel4.Text = "Parent Menu Title Required";
metroLabel5.Text = "Menu Title Required";
metroLabel6.Text = "Form Name Required";
break;
}

PHP nested ternary if condition

use below code

<?php
$var = 4;
echo $current = (($var > 2) ? "gr than 2" : (($var > 6) ? "gr than 6" : "not gr than 2 or 6") );
?>

JS Nested Ternary with Multiple conditions

    const myThing = (conditionA && conditionB) ? thingA : (conditionA) ? thingB : thingC;

the same as:

if(conditionA && conditionB){
thingA
}
else if(conditionA){
thingB
} else {
thingC
}

Using nested ternary operators

Wrap it in parentheses:

$selectedTemplate = isset($_POST['selectedTemplate'])
? $_POST['selectedTemplate']
: (
isset($_GET['selectedTemplate'])
? $_GET['selectedTemplate']
: 0
);

Or even better, use a proper if/else statement (for maintainability):

$selectTemplate = 0;

if (isset($_POST['selectedTemplate'])) {
$selectTemplate = $_POST['selectedTemplate'];
} elseif (isset($_GET['selectedTemplate'])) {
$selectTemplate = $_GET['selectedTemplate'];
}

However, as others have pointed out: it would simply be easier for you to use $_REQUEST:

$selectedTemplate = isset($_REQUEST['selectedTemplate'])
? $_REQUEST['selectedTemplate']
: 0;


Related Topics



Leave a reply



Submit