简体   繁体   English

如何使这些数组正常运行?

[英]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?" 我的+ getRndFromArray没有从正确的数组中获取,而是从“您希望从以下哪种流派?”中从数组中选择任何选项。 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>

Scott Sauyet spotted the error location, but mystery == true; Scott Sauyet发现了错误的位置,但mystery == true; should be if (mystery == true) or simply if (mystery) . 应该是if (mystery == true)或简单地if (mystery)

The minor changes in this should get you going. 在这方面的细微变化应该可以帮助您前进。 It only deals with four genres. 它仅涉及四种类型。 There is no handling for mis-typed genres. 没有处理类型错误的流派。 But the basics are there. 但是基本知识就在那里。 The main missing bit was that the else for the Mysteries case was not written correctly. 主要遗漏的是“神秘案”中的else书写不正确。

You will still need to fix one important problem. 您仍然需要解决一个重要问题。 Note that if the string contains "non fiction" it also contains "fiction", so both of those variables would be true. 请注意,如果字符串包含“ non fiction”,则它也包含“ fiction”,因此这两个变量均为true。 So there is some testing logic you still need to investigate. 因此,您仍然需要研究一些测试逻辑。 If you get stuck on that, ask a separate question showing what you've done and people will be glad to help. 如果您对此感到困惑,请提出一个单独的问题,说明您的工作,其他人将很乐意为您提供帮助。

 const FantasyArray = ["The Name of the Wind", "A Wise Man's Fear", "A Game of Thrones", "The Hobbit"]; const MysteryArray = ["A Study in Scarlet", "The Sign of the Four", "The Hound of the Baskervilles"]; const CookingArray = ["The Joy of Cooking", "The Moosewood Cookbook", "The Enchanted Broccoli Forest"]; const SelfHelpArray = ["I Do It Myself!", "Freud for Dummies"] function getRndFromArray(arrayName){ return arrayName[Math.floor(Math.random()*arrayName.length)]; } var again; 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, Mystery" ); var fantasy = strng.includes("Fantasy"); var mystery = strng.includes("Mystery"); if (fantasy == true) { alert("I think you will enjoy " +getRndFromArray(FantasyArray)); } else if (mystery == true) { alert("I think you will enjoy " +getRndFromArray(MysteryArray)); } else { //if no option is inputed this set of code will run } } if (nonfiction == true) { var strng = prompt( "What genre do you prefer from the following? Self Help, Cooking" ); var selfhelp = strng.includes("Self Help"); var cooking = strng.includes("Cooking"); 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 no option is inputed this set of code will run } } else { again = prompt("You have not told me whether you prefer fiction or non fiction. Would you like to try again? Yes or No?") } } while (again === "Yes"); 

This is a reasonably well-asked question for a newcomer. 对于新来者来说,这是一个合理的问题。 But there is too much code. 但是代码太多。 Please visit the Help Center , and especially How to create a Minimal, Reproducible Example . 请访问帮助中心 ,尤其是如何创建最小的,可复制的示例 You might also want to read How do I create a runnable stack snippet? 您可能还想阅读如何创建可运行的堆栈代码段?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM