Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 268 Vote(s) - 3.59 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Clarify this Javascript ternary

#1
Can someone split this nested ternary into multiple ternaries or other code or explain it in English? I've never come across a nested ternary like this so I'm not sure the order of operations:

var EqualizeCheck = (IIO.ReadDigitalInput(IO_GROUP_CHARGERS, subGroupName, IO_ITEM_ENABLEEQUALIZE) == IO_INPUT_ON) ? (EqualizeCompleteTime != "") ? (EqualizeCompleteTime <= CurrentDateTime) ? true : false : false : true;



Reply

#2
There you go:

```
let EqualizeCheck;

if (IIO.ReadDigitalInput(IO_GROUP_CHARGERS, subGroupName, IO_ITEM_ENABLEEQUALIZE) == IO_INPUT_ON) {
if (EqualizeCompleteTime != "") {
if (EqualizeCompleteTime <= CurrentDateTime) {
EqualizeCheck = true;
} else {
EqualizeCheck = false;
}
} else {
EqualizeCheck = false;
}
} else {
EqualizeCheck = true;
}
```

I understand that it wasn't you who wrote this, but I should point out that nesting so many ternary expressions is a bad practice, and one that leads to unreadable code.
Reply

#3
To get better idea just break it and try to apply `if-else` then you can understand easily. And also the thing mentioned by @Sagi Rika is correct, that nesting of many ternary expressions reduce the code readbilty which leads to difficult in understanding.

First lets break your ternary expressions as below:

var EqualizeCheck = (IIO.ReadDigitalInput(IO_GROUP_CHARGERS, subGroupName, IO_ITEM_ENABLEEQUALIZE) == IO_INPUT_ON)
? (EqualizeCompleteTime != "")
? (EqualizeCompleteTime <= CurrentDateTime)
? true
: false
: false
: true;


If you break it using `if-else` then it will be like as below:

var EqualizeCheck;

if (IIO.ReadDigitalInput(IO_GROUP_CHARGERS, subGroupName, IO_ITEM_ENABLEEQUALIZE) == IO_INPUT_ON) {
if (EqualizeCompleteTime != "") {
if (EqualizeCompleteTime <= CurrentDateTime) {
EqualizeCheck = true;
} else {
EqualizeCheck = false;
}
} else {
EqualizeCheck = false;
}
} else {
EqualizeCheck = true;
}


To understand better let's take an example using `if-else`. Its like if the age is greater than than 18 then `canVote` will be `true` otherwise `canVote` will be `false`

var age = 19;
var canVote;

if (age > 18) {
canVote = 'yes';
} else {
canVote = 'no';
}

If we write the above code using ternary expressions then that will be like:

var age = 19;
var canVote = age > 18 ? 'yes' : 'no';
Reply

#4
To work through it step by step, first separate the most nested condition (Look for a complete `condition ? value : value` statement) In this case that's this bit:

> var EqualizeCheck = (IIO.ReadDigitalInput(IO_GROUP_CHARGERS, subGroupName, IO_ITEM_ENABLEEQUALIZE) == IO_INPUT_ON) ? (EqualizeCompleteTime != "") ? **(EqualizeCompleteTime <= CurrentDateTime) ? true : false** : false : true;

Separate that out and repeat for each ternary condition, until you have something like this:

```
// Condition 1:
(IIO.ReadDigitalInput(IO_GROUP_CHARGERS, subGroupName, IO_ITEM_ENABLEEQUALIZE) == IO_INPUT_ON) ?
// Condition 1 == true, check the next condition
// Condition 2:
(EqualizeCompleteTime != "") ?
// Condition 2 == true, check the next condition
// Condition 3:
(EqualizeCompleteTime <= CurrentDateTime) ?
// Condition 3 == true, return true
true :
// Condition 3 == false, return false
false :
// Condition 2 == false, return false
false :
// Condition 1 == false, return true
true;
```

Since this essentially boils down to two success conditions (Either where condition 1 is false, or where condition 2 and 3 are both true), you can vastly simplify it to this:

```
var EqualizeCheck =
!IIO.ReadDigitalInput(IO_GROUP_CHARGERS, subGroupName, IO_ITEM_ENABLEEQUALIZE) == IO_INPUT_ON) ||
(
EqualizeCompleteTime != "" &&
EqualizeCompleteTime <= CurrentDateTime
)
```
Reply

#5
This is the more readable friendly version of the above ternary oparator.

const EqualizeCheck = (IIO.ReadDigitalInput(IO_GROUP_CHARGERS, subGroupName, IO_ITEM_ENABLEEQUALIZE) == IO_INPUT_ON) ?
((EqualizeCompleteTime != "") ? ((EqualizeCompleteTime <= CurrentDateTime) ? true : false) : false) : true;

If you divide that into a several ternary operator statements, it will looks as follows. I hope this format is easy to understand.

const EqualizeValueCheck = (IIO.ReadDigitalInput(IO_GROUP_CHARGERS, subGroupName, IO_ITEM_ENABLEEQUALIZE) == IO_INPUT_ON);
const EqualizeCompleteTimeCheck = (EqualizeCompleteTime <= CurrentDateTime) ? true : false;
const EqualizeCompleteTimeEmptyValueCheck = (EqualizeCompleteTime != "") ? EqualizeCompleteTimeCheck : false;
const EqualizeCheck = (EqualizeValueCheck) ? EqualizeCompleteTimeEmptyValueCheck : false;
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through