[英]How can I make these arrays function properly?
我的+ getRndFromArray沒有從正確的數組中獲取,而是從“您希望從以下哪種流派?”中從數組中選擇任何選項。 問題。 用於此的數組位於代碼的開頭(代碼段中未顯示),我有一個
function getRndFromArray(arrayName){
return arrayName[Math.floor(Math.random()*arrayName.length)];
}
在代碼開頭的數組之后
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發現了錯誤的位置,但mystery == true;
應該是if (mystery == true)
或簡單地if (mystery)
。
在這方面的細微變化應該可以幫助您前進。 它僅涉及四種類型。 沒有處理類型錯誤的流派。 但是基本知識就在那里。 主要遺漏的是“神秘案”中的else
書寫不正確。
您仍然需要解決一個重要問題。 請注意,如果字符串包含“ non fiction”,則它也包含“ fiction”,因此這兩個變量均為true。 因此,您仍然需要研究一些測試邏輯。 如果您對此感到困惑,請提出一個單獨的問題,說明您的工作,其他人將很樂意為您提供幫助。
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");
對於新來者來說,這是一個合理的問題。 但是代碼太多。 請訪問幫助中心 ,尤其是如何創建最小的,可復制的示例 。 您可能還想閱讀如何創建可運行的堆棧代碼段?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.