By Arvindh


2019-01-11 11:50:45 8 Comments

Is it possible to perform ternary for 3 conditions but with one default condition ?

Here is my code :

<Button
  rounded={true}
  title='Add Address'
  backgroundColor='#2980b9'
  rightIcon={{name: 'arrow-forward'}}
  disabled={this.state.timeSlotItemSelected === null || this.state.quantityItemSelected === null ? true : false}
/>

In this, this.state.timeSlotItemSelected is default. What I need, is whenever this.state.timeSlotItemSelected and this.state.quantityItemSelected are null, the result should be true, and whenever this.state.timeSlotItemSelected and this.state.deliveryOptionSelected are null, then it should also be true. Otherwise it should be false.

How do I perform this for both conditions?

3 comments

@Daniel Budick 2019-01-11 12:18:34

This is some Boolean algebra (I will probably use the wrong words, I learned math in german, so edits or comments are appreciated):

Let's start with your verbal conditions translated into JavaScript (I deleted this.state for better readability):

(timeSlotItemSelected === null && quantityItemSelected === null) || (timeSlotItemSelected === null && deliveryOptionSelected === null)

We can factor out timeSlotItemSelected, so it looks like this:

timeSlotItemSelected === null && (quantityItemSelected === null || deliveryOptionSelected === null)

Now you can use it in an if-clause or a ternary operator.

Concerning your question for a "default condition":

There is literally no way to have a default when working with boolean, they are either true or false.

This means: If an if-clause or a ternary operator have a boolean as input (which they have in JavaScript and most other languages), they cannot have a third case for a default, because a boolean can only be true or false.

A variable with the type boolean can have a default value (e.g. false in Java), but not in JavaScript.

@Billy Brown 2019-01-11 12:40:10

I wonder whether by "default condition", they mean that one condition applies to both test cases. So for D = default, we have D ∧ A => X ; D ∧ B => Y ; else => Z, where somehow the default case is factored out as; something like D ∧ (A => X ; B => Y) ; else => Z. In Lisp/Scheme, you could have (cond-default D (A X ...) (B Y ...) (else Z ...)).

@Arvindh 2019-01-11 13:17:37

Thanks for the response and glad that it worked perfectly @Daniel

@Arvindh 2019-01-11 13:18:08

ya your right @BillyBrown, I mentioned default in the sense, that first condition applies for the both cases !

@Rasoul Salehi 2019-01-11 12:00:00

(this.state.timeSlotItemSelected === null && this.state.quantityItemSelected === null) || ( this.state.deliveryOptionSelected === null && this.state.timeSlotItemSelected === null) ? true : false

@Billy Brown 2019-01-11 11:59:20

You can do this with a simple combination of && and ||, and no need of the ternary operator at all.

this.state.timeSlotItemSelected === null &&
    (this.state.quantityItemSelected === null || this.state.deliveryOptionSelected === null)

This will be true if this.state.timeSlotItemSelected is null, and if either one of this.state.quantityItemSelected or this.state.deliveryOptionSelected is null, and false otherwise.

Truth table:

timeSlotItemSelected | quantityItemSelected | deliveryOptionSelected | Result
---------------------+----------------------+------------------------+-------
null                 | null                 | null                   | true
null                 | <not null>           | null                   | true
null                 | null                 | <not null>             | true
null                 | <not null>           | <not null>             | false
<not null>           | <anything>           | <anything>             | false

You do not need the ternary operator in this case, because the expression itself will return true or false: <Expression> ? true : false = <Expression>.

@Arvindh 2019-01-11 13:18:52

Perfectly working , cheers !

Related Questions

Sponsored Content

58 Answered Questions

[SOLVED] How do I redirect to another webpage?

52 Answered Questions

[SOLVED] How do I include a JavaScript file in another JavaScript file?

72 Answered Questions

[SOLVED] How do I remove a particular element from an array in JavaScript?

  • 2011-04-23 22:17:18
  • Walker
  • 5393374 View
  • 6715 Score
  • 72 Answer
  • Tags:   javascript arrays

25 Answered Questions

53 Answered Questions

[SOLVED] How do I check if an element is hidden in jQuery?

33 Answered Questions

[SOLVED] How do I return the response from an asynchronous call?

37 Answered Questions

[SOLVED] How do I remove a property from a JavaScript object?

86 Answered Questions

[SOLVED] How do JavaScript closures work?

21 Answered Questions

[SOLVED] Set a default parameter value for a JavaScript function

48 Answered Questions

Sponsored Content