The previous topic “Build a Quiz Game: getResults” has been locked. I am having an issue with the last two steps 13 and 14. The task does not agree with the comparison method.
let rand = Math.floor(Math.random() * 5 )
let randomized = Math.floor(Math.random() * 3 )
let questions = [
{
category : "Japanese",
question : "What does 'Gokiburi' means?",
choices : ["Cockroach", "Fly","Ant"],
answer : "Cockroach"
},
{
category : "Science",
question : "What is the name of the name of the coolest star?",
choices : ["Red Dwarf","Pixie","Bat"],
answer : "Red Dwarf"
},
{
category : "Math",
question : "What is the square root of 100?",
choices : ["10","0","2"],
answer : "10"
},
{
category : "History",
question : "When the second World War has ended?",
choices : ["1945","1949","1941"],
answer : "1945"
},
{
category : "Art",
question : "Who drew the Mona Lisa?",
choices : ["Leonardo DaVinci","Agatha Christie", "Salvador Dali"],
answer : "Leonardo DaVinci"
}
]
//Random question
const getRandomQuestion = (arry) => {
return arry[rand]
}
//Random Choice based on asked question
const getRandomComputerChoice = (arry) => {
return arry[randomized]
}
//Comparing select answer vs. correct answer
const getResults = (quiz,select) => {
let correct = quiz.choices[0] // Tried direct assignment, but no luck
console.log(typeof(quiz.question)) //String
console.log(typeof(select)) // String
if(correct == select){ //Conditional works for both scenarios
//The format is correct according to step 12
return "The computer's choice is correct!"
} else{
// The format is correct (copy/paste for double check) but step 13 says it is not
return `The computer's choice is wrong. The correct answer is: ${correct}`
}
}
let trivia = getRandomQuestion(questions)
let reply = getRandomComputerChoice(trivia.choices)
let result = getResults(trivia,reply)
//Checking function outputs
console.log(trivia)
console.log(reply)
console.log(result)
If you have a question about a specific challenge as it relates to your written code for that challenge and need some help, click the Get Help > Ask for Help button located on the challenge.
The Ask for Help button will create a new topic with all code you have written and include a link to the challenge also. You will still be able to ask any questions in the post before submitting it to the forum.
It didn’t work. After applying that code, steps 11,12 failed as well. It seems that comparison step should be interpreted differently. The values in the array of “choices” are string, the correct “answer” is also string. I tried typeof() to identify the type of variable, both correct answer value from “choices” array and the “answer” value are strings.
I managed to complete the task. Thanks for the help. Here is the breakdown how I did it, I compared the index of the correct answer and the random answer
let correct = quiz.answer
let options = quiz.choices.indexOf(correct)
if(quiz.choices.indexOf(correct) == quiz.choices.indexOf(select)){
return "The computer's choice is correct!"
} else{
return `The computer's choice is wrong. The correct answer is: ${quiz.choices[options]}`
}
Good point.
After fixing the conditional, I tested out the ${quiz.choices[options]} to confirm if the false conditional variable is accepted…I can reformatted the variable to $quiz.answer and it was accepted.
Besides, the task did not say that the failed message should contain the correct answer from the answer parameter, I only needed to supply the correct answer.
Anyhow, I fixed my code to quiz.answer and the task was finished with no issues. I will reinput my code again
"select" is the second parameter of the function getResult(quiz, select)
let correct = quiz.answer
let options = quiz.choices.indexOf(correct)
if(quiz.choices.indexOf(correct) == quiz.choices.indexOf(select)){
return "The computer's choice is correct!"
} else{
return `The computer's choice is wrong. The correct answer is: ${quiz.answer}`
}