First time saw such ternary expressions:
var somevar = b1 ? b2 ? b3 : b4 : b5 ? b6 : b7 ? b8 : b9 ? b10 : b11
Having a hard time understanding and converting it to:
if (b1) {
} else if (bx) {
}
I've looked everywhere and can't seem to find the answer to this.
Just add parentheses.
var somevar = (b1 ? (b2 ? b3 : b4) : (b5 ? b6 : (b7 ? b8 : (b9 ? b10 : b11))))
I.e.
if (b1) {
if (b2) {
b3
} else {
b4
}
} else {
if (b5) {
b6
} else {
if (b7) {
b8
} else {
if (b9) {
b10
} else {
b11
}
}
}
}
Or to make it shorter.
if (b1) {
if (b2) {
b3
} else {
b4
}
} else if (b5) {
b6
} else if (b7) {
b8
} else if (b9) {
b10
} else {
b11
}
Start from the far right. whenever you find a ? b : c
add parentheses around it.
For example:
a ? b ? c : d : e
a ? (b ? c : d) : e
(a ? (b ? c : d) : e)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With