Skip to content

Instantly share code, notes, and snippets.

@claudiainbytes
Created August 20, 2017 20:54
Show Gist options
  • Save claudiainbytes/52a3ab1e82c4f89effa5b8bc890bb9e9 to your computer and use it in GitHub Desktop.
Save claudiainbytes/52a3ab1e82c4f89effa5b8bc890bb9e9 to your computer and use it in GitHub Desktop.
The Food Chain - Ternary Operators
/*
* Programming Quiz - Navigating the Food Chain (3-8)
*
* 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 ;-)
*/
// change the values of `eatsPlants` and `eatsAnimals` to test your code
var eatsPlants = true;
var eatsAnimals = false;
var category = (eatsPlants && eatsAnimals) ? "omnivore" : ( eatsPlants ? "herbivore": ( eatsAnimals ? "carnivore" : undefined ) );
console.log(category);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment