javascript - ternary operators with multiple statements - Stack Overflow

** Use a series of ternary operator to set the category to one of the following:* - "herbivore&

/* * Use a series of ternary operator to set the category to one of the following: * - "herbivore" if an animal eats plants * - "carnivore" if an animal eats animals * - "omnivore" if an animal eats plants and animals * - undefined if an animal doesn't eat plants or animals * * Notes * - use the variables eatsPlants and eatsAnimals in your ternary expressions * - if statements aren't allowed ;-) */

i used this code but it is not getting things right

  var eatsPlants = true;
var eatsAnimals = false;


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

any ideas to solve this problem

/* * Use a series of ternary operator to set the category to one of the following: * - "herbivore" if an animal eats plants * - "carnivore" if an animal eats animals * - "omnivore" if an animal eats plants and animals * - undefined if an animal doesn't eat plants or animals * * Notes * - use the variables eatsPlants and eatsAnimals in your ternary expressions * - if statements aren't allowed ;-) */

i used this code but it is not getting things right

  var eatsPlants = true;
var eatsAnimals = false;


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

any ideas to solve this problem

Share Improve this question edited Nov 11, 2017 at 4:47 Mohamed Turki asked Nov 11, 2017 at 4:26 Mohamed TurkiMohamed Turki 111 gold badge1 silver badge2 bronze badges 5
  • Hello and wele! Remember that StackOverflow isn't a discussion forum or a code writing service. It might be worth having a look at [this article][1] which has some tips on how to ask a great question that'll get you the answer you're looking for. [1]: stackoverflow./help/how-to-ask – Paul M Commented Nov 11, 2017 at 4:30
  • undefined if an animal doesn't eat plants or animals ... but you output undefined when an animal doesn't eat plants AND animals - you need to brush up on your boolean logic – Jaromanda X Commented Nov 11, 2017 at 4:33
  • also, it should be undefined for eats neither, not "undefined" – Jaromanda X Commented Nov 11, 2017 at 4:35
  • eatsPlants ? (eatsAnimals ? "omnivore" : "herbivore") : (eatsAnimals ? "carnivore" : undefined); – Jaromanda X Commented Nov 11, 2017 at 4:35
  • doesn't eat plants or animals means that it is not eating them both, not one of them so i thought i should use && because using OR means that this animal may eat plants but not animals and in this case it will be herbivore and vice versa. – Mohamed Turki Commented Nov 11, 2017 at 4:41
Add a ment  | 

3 Answers 3

Reset to default 2

This will work -

const category = (eatPlants && eatAnimals) ? 'omnivore' : eatAnimals ? 'carnivore' : eatPlants ? 'herbivore' : undefined;

You need to chain one ternary's else part to another ternary and so on if you wish to draw out an if-else branch using ternary operator.

But I suggest, you sinply use if else branches to maintain readability of your code. Ternaries just make a little mess with multiple conditions.

This is how much clear it would have been in if-else logic.

let category = undefined;
if (eatPlants && eatAnimals)
     category = 'omnivore';
else if (eatPlants)
     category = 'herbivore';
else if (eatAnimals)
     category = 'carnivore';

This should solve your problem.

var eatsPlants = false;
var eatsAnimals = true;

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

console.log(category);
var eatsPlants = false;
var eatsAnimals = false;

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

console.log(category);

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745280809a4620269.html

相关推荐

  • javascript - ternary operators with multiple statements - Stack Overflow

    ** Use a series of ternary operator to set the category to one of the following:* - "herbivore&

    4小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信