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));
@desinas
Copy link
Author

desinas commented Dec 15, 2017

What Went Well

  • Your code should have a variable eatsPlants
  • Your code should have a variable eatsAnimals
  • Your code should include ternary statements
  • Your code should produce the expected output

Feedback: Your answer passed all our tests! Awesome job!

@desinas
Copy link
Author

desinas commented Dec 16, 2017

The format of the conditionals I suggest is this:
var category = 🌿&🐮 ? "omnivore" : (🐮? "carnivore" : (🌿 ? "herbivore" : undefined ) );

So that, to check for the category in all options.

@bounab-gr
Copy link

/*
//Please help me
//In this exercise .. I am a beginner
//

  • 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 ;-)
      */

/*

  • QUIZ REQUIREMENTS
    • Your code should have the variables eatsPlants, eatsAnimals
    • Your code should include ternary statements. Do not use if....else statement.
    • Your code should produce the expected output
    • Your code should not be empty
    • BE CAREFUL ABOUT THE PUNCTUATION AND THE EXACT WORDS TO BE PRINTED.
      */

// change the values of eatsPlants and eatsAnimals to test your code
var eatsPlants = false;
var eatsAnimals = true;

/*

  • Test your code agaist the followig possible input/output combinations of (eatsPlants, eatsAnimals, expected output):
    • (true, true, omnivore)
    • (false, true, carnivore)
    • (true, false, herbivore)
    • (false, false, undefined)
      */

var category = /* your code goes here */

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