Skip to content

Instantly share code, notes, and snippets.

@desinas
Last active January 20, 2024 08:09
Show Gist options
  • Save desinas/907ac97e97223030721c248879c52e79 to your computer and use it in GitHub Desktop.
Save desinas/907ac97e97223030721c248879c52e79 to your computer and use it in GitHub Desktop.
Navigating the food chain Quiz from Udacity Front end developer
/*
* 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 = false;
var eatsAnimals = true;
var category = eatsPlants && eatsAnimals ? "omnivore" : ( eatsAnimals ? "carnivore" : ( eatsPlants ? "herbivore" : undefined ) );
console.log(category));
@KOMOL420
Copy link

KOMOL420 commented Dec 7, 2020

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

Copy link

ghost commented Dec 10, 2020

var eatsPlants = false;
var eatsAnimals = false;

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

@leota95
Copy link

leota95 commented Mar 24, 2021

var eatsPlants = false;
var eatsAnimals = false;

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

console.log(category);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment