Convert if statement to ternary operator

user8492975

I tried to convert these if statement to ternary:

var eatsPlants = false;
var eatsAnimals = true;

    if(eatsPlants){
      return 'herbivore';
    }else if(eatsAnimals){
      return 'carnivore';
    }else if(eatsPlants && eatsAnimals){
    return 'omnivore';
    }else{
     return undefined;
    }

Here's my solution:

var category = eatsPlants && eatsAnimals ? "omnivore" : "herbivore" : "carnivore" : undefined;

console.log(category);

But this doesnt work and it returns the error missing semicolon.

Any idea how to fix it?

Vineesh

You should add the remaining if conditions too

var category = eatsPlants && eatsAnimals ? "omnivore" : eatsPlants? "herbivore" : eatsAnimals? "carnivore" : undefined;

console.log(category);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Difference between ternary (conditional) operator and if statement returning an Action

From Dev

Ternary operator evaluating conditional statement while condition not met

From Dev

Ternary operator in select statement

From Dev

IEnumerable Select statement with ternary operator

From Dev

SQL statement equivalent to ternary operator

From Dev

convert some lines of php statement into ternary operator

From Dev

Ternary Operator (in-line "if statement") conversion from MySQL to MySQLi?

From Dev

How to use AND in if statement using ternary operator

From Dev

Ternary operator is not a statement

From Dev

Ternary Operator in jQuery statement

From Dev

String Convert To Ternary Operator

From Dev

Ternary operator inside a return statement

From Dev

Java Ternary operator outputs different result than if else statement

From Dev

(How) is it possible to catch an exception in a Java ternary operator statement?

From Dev

Question Regarding Using Ternary Operator in Switch Statement

From Dev

Trying to convert if else statement into ternary in javascript

From Dev

How would I write this if else statement as ternary operator

From Dev

Max Ternary operator IF statement compare

From Dev

IEnumerable Select statement with ternary operator

From Dev

How to Convert this to a ternary operator

From Dev

convert some lines of php statement into ternary operator

From Dev

How to use AND in if statement using ternary operator

From Dev

How can I convert this if else statement into ternary operator?

From Dev

String Convert To Ternary Operator

From Dev

convert ternary operator to if else

From Dev

Cannot embed ternary operator into echo statement

From Dev

PHP inline statement using ternary logic operator "?:"

From Dev

ternary operator causes an error inside if statement

From Dev

Can we use ternary operator within an if statement?

Related Related

  1. 1

    Difference between ternary (conditional) operator and if statement returning an Action

  2. 2

    Ternary operator evaluating conditional statement while condition not met

  3. 3

    Ternary operator in select statement

  4. 4

    IEnumerable Select statement with ternary operator

  5. 5

    SQL statement equivalent to ternary operator

  6. 6

    convert some lines of php statement into ternary operator

  7. 7

    Ternary Operator (in-line "if statement") conversion from MySQL to MySQLi?

  8. 8

    How to use AND in if statement using ternary operator

  9. 9

    Ternary operator is not a statement

  10. 10

    Ternary Operator in jQuery statement

  11. 11

    String Convert To Ternary Operator

  12. 12

    Ternary operator inside a return statement

  13. 13

    Java Ternary operator outputs different result than if else statement

  14. 14

    (How) is it possible to catch an exception in a Java ternary operator statement?

  15. 15

    Question Regarding Using Ternary Operator in Switch Statement

  16. 16

    Trying to convert if else statement into ternary in javascript

  17. 17

    How would I write this if else statement as ternary operator

  18. 18

    Max Ternary operator IF statement compare

  19. 19

    IEnumerable Select statement with ternary operator

  20. 20

    How to Convert this to a ternary operator

  21. 21

    convert some lines of php statement into ternary operator

  22. 22

    How to use AND in if statement using ternary operator

  23. 23

    How can I convert this if else statement into ternary operator?

  24. 24

    String Convert To Ternary Operator

  25. 25

    convert ternary operator to if else

  26. 26

    Cannot embed ternary operator into echo statement

  27. 27

    PHP inline statement using ternary logic operator "?:"

  28. 28

    ternary operator causes an error inside if statement

  29. 29

    Can we use ternary operator within an if statement?

HotTag

Archive