mercredi 16 septembre 2020

My code won't allow the user quit, my answers aren't working right, neither will my .toLowerCase() and it also goes into an infinite loop

//function i created to check my responses

function responseChecker(question) {

var valid = false;

//while loop created to refuse answers i havent specified in the code below

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

//this is to convert any input from the user to toLowerCase but it always shows an error message when i click cancel something about not being able to convert null to lowercase

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)
}


}
}

//quiz questions for the game in a list

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" ]

//answers i also pu in a list like the questions

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

// ask user how many questions they want to play

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

// main code, start game

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

//while loop so i can allow the user to quit

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

//if statement inside while loop to alert user if their answer is correct or wrong and allow the to cancel by typing q but thats not working either

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")

//end game mechanics

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

Aucun commentaire:

Enregistrer un commentaire