Tell us what’s happening:
My code doesnt work properly, but passes all the test. In the console even if the answer of the computer is similar with the correct answer, it says “Choice is wrong. The correct answer is: undefined”. The best would be to check it yourself to understand what I mean. Thanks for any help!
Your code so far
const questions = [
{
category: "PCo",
question: "fav color?",
choices: ['Red', 'Blue', 'Yellow'],
answer: 'Red'
},
{
category: "Pca",
question: "fav car?",
choices: ['Toyota', 'Lambo', 'BMW'],
answer: 'BMW'
},
{
category: "PB",
question: "fav brand?",
choices: ['LV', 'KmRii', 'LGB'],
answer: 'KmRii'
},
{
category: "PF",
question: "fav fruit?",
choices: ['Apple', 'Grape', 'Watermelon'],
answer: 'Watermelon'
},
{
category: "PH",
question: "fav hobby?",
choices: ['Coding', 'Shopping', 'Hustling'],
answer: 'Shopping'
}
];
function getRandomQuestion(questions){
let randomNum = Math.floor(Math.random()*questions.length)
return questions[randomNum]
};
function getRandomComputerChoice(choices){
let randomNum = Math.floor(Math.random()*choices.length)
return choices[randomNum]
};
function getResults(question ,choice){
if(question.answer == choice){
return "The computer's choice is correct!"
}
else if(question.answer !== choice){
return `The computer's choice is wrong. The correct answer is: ${question.answer}`;
}
};
console.log(getRandomQuestion(questions));
console.log(getRandomComputerChoice([ 'Red', 'Blue', 'Yellow' ]));
console.log(getResults('fav color?', 'Red'));
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36
Challenge Information:
Build a Quiz Game - Build a Quiz Game