"Code Your Own Adventure" logic


#1


https://www.codecademy.com/courses/javascript-beginner-en-ZA2rb/0/6?curriculum_id=506324b3a7dffd00020bf661


This game works and I understand the exercise just fine. It's bugging me that I can't figure out how to work in the && operator in a logical way. Any ideas?


var habitat = prompt("Do you like the ocean, mountains, or forest?").toLowerCase();
var diet = prompt("Do you eat meat or vegetables?").toLowerCase();

switch(habitat) {
    case "ocean":
      if (diet == "meat") {
        console.log("You are an orca!");
      }
      else {
        console.log("You are a manatee!");
      }
      break;
    case "mountains":
      if (diet == "meat" || "vegetables") {
        console.log("You are a bear!");
      }
      break;
    case "forest":
      if (diet == "meat" && "vegetables") {
        console.log("You are a badger!");
      }
      else {
        console.log("You are a deer!");
      }
      break;
    default:
      console.log("I don't know what you are.");
}


#2

Hello, this link should help you :slight_smile:


#3

Hi, not sure the link helped much except to remind me to use ===. But, I reworked the game design and now I'm pretty happy with it. See below:

var habitat = prompt("Do you live in the ocean, tundra, or temperate forest?").toLowerCase();

switch(habitat) {
    case "ocean":
      var fish = prompt("Do you eat fish?").toLowerCase();
      var seal = prompt("Do you eat seals?").toLowerCase();
      if (fish === "yes" || seal === "yes") {
        console.log("You are an orca!");
      }
      else {
        console.log("You are a dolphin!");
      }
      break;
    case "tundra":
      var lichen = prompt("Do you eat lichens?").toLowerCase();
      var lemming = prompt("Do you eat lemmings?").toLowerCase();
      if (lichen === "yes" && lemming === "no") {
        console.log("You are a caribou!");
      }
      else if (lichen === "no" && lemming === "yes") {
        console.log("You are an arctic fox!")
      }
      else {
        console.log("I don't know an animal with that diet.");
      }
      break;
    case "temperate forest":
      var fruit = prompt("Do you eat fruit?").toLowerCase();
      var salmon = prompt("Do you eat salmon?").toLowerCase();
      if (fruit === "yes" && salmon === "no") {
        console.log("You are a whitetail deer!")
      }
      else {
        console.log("You are a black bear!")
      }
      break;
    default:
      console.log("I don't know which animal you are.");
}

#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.