Can anyone help me with this Javascript coding quiz?

Can anyone help me with this Javascript coding quiz?
0.0 0

#1

Hi I just begin learning JavaScript and had a problem with getting this coding quiz right. Can someone show me what’s the right way to do it? Thanks in advance!

/*

  • 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 :wink:
      */

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

console.log(category);


#2

Firstly, welcome to the forums.

While we are primarily here to help people with their Free Code Camp progress, we are open to people on other paths, too. Some of what you are asking is pretty trivial in the Free Code Camp context, so you might find that if you’re not getting the instruction and material you need in your current studies, the FCC curriculum will really help you get started. At a modest guess I’d say investing a 4-5 hours working through the curriculum here will really pay off. You can find the curriculum at https://freecodecamp.org.

With your current questions, we don’t have enough context to know what you already know or don’t know, so it is impossible to guide you without just telling you the answer (which we won’t do).

It is pretty typical on here for people to share a codepen / jsfiddle example of what they have tried so that anyone helping has more of an idea of what help is actually helpful.

Please provide some example of what you’ve tried and I’m sure you’ll get more help.

Happy coding :slight_smile:


#3

Sometimes it helps to do a flow chart kinda thing of the question?



var a = false;
var b = true;

var check =
if a true
{     if b true
      {then a & b  }
      else
      { just a }  
}
else
if b true
  { then just b }
else
  undef
 
( substituting.... if?true then  ... for "?"  , and ...else: ... for " : " )
var check = a if?true then (b if?true then "a & b true" else: "just a true") else: (b if?true then"b true" else: undefined);


#4

This should see you through

var eatsPlants = true;
var eatsAnimals = false;

var category = eatsPlants && eatsAnimals ? “omnivore”:
eatsPlants ? “herbivore” : eatsAnimals ? “carnivore” : undefined;

console.log(category);