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
Post a Comment