javascript - Convert if statement to ternary operator -


i tried convert these if statement 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 solution:

var category = eatsplants && eatsanimals ? "omnivore" : "herbivore" : "carnivore" : undefined;  console.log(category); 

but doesnt work , returns error missing semicolon.

any idea how fix it?

you should add remaining if conditions too

var category = eatsplants && eatsanimals ? "omnivore" : eatsplants? "herbivore" : eatsanimals? "carnivore" : undefined;  console.log(category); 

Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -