我是编码的新手,我还不了解所有词汇。这是我坚持的练习。 使用一系列三元运算符将类别设置为以下之一: *-“草食动物”(如果动物食用植物) *-“食肉动物”(如果动物食用动物) *-“杂食动物”(如果动物食用植物和动物) *-如果动物不吃植物或动物,则未定义
我的代码是这样开始的。
var eatsPlants = true;
var eatsAnimals = false;
var category = eatsPlants ? "herbivore" : "carnivore" /* this is where I'm stuck (lol), when my first condition is true the code runs automatically. How do I get to my "omnivore" (true, true) "undefined" (false, false) condition? */
console.log(category);
任何提示将不胜感激!
谢谢
答案 0 :(得分:0)
您可以尝试一下。
var category = (eatsPlants && eatsAnimals) ? "omnivore" : (eatsPlants && !eatsAnimals) ? "herbivore" : (!eatsPlants && eatsAnimals) ? "carnivore" : undefined