lundi 22 juillet 2019

How can I make these arrays function properly?

my +getRndFromArray are not grabbing from the correct array and are instead choosing any option from the arrays in the "what genre do you prefer from the following?" questions. The arrays for this are at the start of the code (not shown in the snippet) and I have a

function getRndFromArray(arrayName){                                
return arrayName[Math.floor(Math.random()*arrayName.length)]; 
}   

after the arrays at the start of the code

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, Romance, 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");
</script>
</body>
</html>

Aucun commentaire:

Enregistrer un commentaire