lundi 22 juillet 2019

How do I include arrays within an if/else statement?

I am fairly new to this and would like to include my arrays within an if else statement and another if else. When my program asks a question about whether the user likes fiction or non fiction books, if fiction is true it takes them to a prompt of different genres and then is meant to randomly select one and then go to that array.

do {
  var strng = prompt(
    "Do you prefer fiction or non fiction books? From here I can choose a genre of book for you to read"
  );
  var fiction = strng.includes("fiction");
  var nonfiction = strng.includes("non fiction");

  if (fiction == true) {
    var strng = prompt(
      "What genre do you prefer from the following? Fantasy, Science Fiction, Rommance, Action, Mystery"
    );
    var fantasy = strng.includes("Fantasy");
    var sciencefiction = strng.includes("Science Fiction");
    var romance = strng.includes("Romance");
    var action = strng.includes("Action");
    var mystery = strng.includes("Mystery");
    if (fantasy == true) {
      alert("I think you will enjoy " + getRndFromArray(FantasyArray));
    } else if (sciencefiction == true) {
      alert("I think you will enjoy " + getRndFromArray(ScienceFictionArray));
    } else if (romance == true) {
      alert("I think you will enjoy " + getRndFromArray(RomanceArray));
    } else if (action == true) {
      alert("I think you will enjoy " + getRndFromArray(ActionArray));
    } else mystery == true;
    {
      alert("I think you will enjoy " + getRndFromArray(MysteryArray));
    }

    if (nonfiction == true) {
      var strng = prompt(
        "What genre do you prefer from the following? Self Help, Cooking, Health, Business"
      );
      var selfhelp = strng.includes("Self Help");
      var cooking = strng.includes("Cooking");
      var health = strng.includes("Health");
      var business = strng.includes("Business");
      if (selfhelp == true) {
        alert("I think you will enjoy " + getRndFromArray(SelfHelpArray));
      } else if (cooking == true) {
        alert("I think you will enjoy " + getRndFromArray(CookingArray));
      } else if (health == true) {
        alert("I think you will enjoy " + getRndFromArray(HealthArray));
      } else if (business == true) {
        alert("I think you will enjoy " + getRndFromArray(BusinessArray));
      } //if no option is inputed this set of code will run
      else {
        var again = prompt(
          "You have not told me whether you prefer fiction or non fiction. Would you like to try again? Yes or No?"
        );
      }
    } // prompt relating to do while loop
  }
} while (again === "Yes");

Aucun commentaire:

Enregistrer un commentaire