我的代码不允许用户退出,我的答案无法正常工作,我的.toLowerCase也不会进入无限循环

问题描述

我创建的用于检查响应的函数

function responseChecker(question) {

var valid = false;

我创建的while循环拒绝代码中未指定的答案:

while (valid == false) {
  
var input = prompt(question)

这是为了将用户的任何输入转换为小写。它总是显示一条错误消息。当我单击“取消”时,它说明无法将null转换为小写。

input = input.toLowerCase() 

try {
if(input < "a" || input > "d" && input == "q" ) 
throw "Type a letter between a and d";
if(input == '') throw 'that is empty,type something'
if(input % 1 == 0) throw 'type a letter'
alert("Thank you")
valid = true;
}


catch(err) {
alert('That is ' + err)
}


}
}

列表中的游戏测验问题:

var quizQuestions = ["Where does Simba live? \nA) Pride Rock \nB) Kenya \nC) Great Rift Valley \nD) Desert","What is the name of Simba's dad? \nA) Mable \nB) Mufasa \nC) Matata \nD) Mr Simba ","Who was Simba's childhood best friend? \nA) Nala \nB) Sarabi  \nC) Shenzi \nD) Food","What animals are Timoni and Pumba? \nA) Rat & Cat \nB) Wild dog & mouse \nC) Warthog & Meerkat \nD) Pig & squirrel ","Who is Rafiki? \nA) Baboon \nB) Monkey \nC) Master Oogway 2.0 \nD) Wise Monk","Who killed Sima's dad? \nA) Shenzi \nB) Scar \nC) Hyena's  \nD) Takaa","What type of birds did were going to eat Simba when he passed out in the dessert? \nA) Hawks \nB) Verdins \nC) Vultures \nD) Eagles","What is the name of the Butler of the king of Pride Rock? \nA) Zen \nB) Kian \nC) Black Butler \nD) Zazu","What poor animal was almost eaten by Simba's uncle? \nA) Rat \nB) Bird \nC) Zazu \nD) Mouse","Who said \"Asante sana,Squash banana\"? \nA) Timoni \nB) Nala \nC) Rafiki \nD) Pumba" ]

我也将答案放在列表中,例如问题:

var answers = ["a","b","a","c","d","c"]

用户询问他们要玩多少个问题:

var questionsNum = Number(prompt("How many questions?"))

主要代码,开始游戏

var counter = 0;
var answersCorrect = 0;
var answersWrong = 0;

在循环时可以允许用户退出

while (questionsNum != counter || userAnswer != "q")  {
var userAnswer = responseChecker(quizQuestions[counter])
counter ++
answers ++

while循环中的if语句警告用户答案是对还是错,并允许他们通过键入q来取消,但这也不起作用。

if (userAnswer == answers[counter]){
alert("correct")
answersCorrect ++
} else if (userAnswer != answers[counter]){
alert("wrong")
answersWrong ++
}else if (userAnswer == "q") {
 break;
}
} // end of while loop
alert("out of rounds")

最终游戏机制

alert("You answered " + counter + " questions \nYou got " + questionsWrong + " questions wrong \nYou got " + questionCorrect + " questions Correct \nThank you for playing!")

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)