Tell us what’s happening:
I am struggling with the getResults() function and can’t figure out what is wrong. I have checked the forums and checked the logic using console.log for each variable. There’s a single piece i’m missing and can’t find it.
Your code so far
let questions = [
{
category: "Colors",
question: "What is the best color?",
choices: ["blue", "green", "purple"],
answer: "purple"
},
{
category: "Animals",
question: "What is the best pet?",
choices: ["bird", "cat", "dog"],
answer: "dog"
},
{
category: "City",
question: "Which is the best city?",
choices: ["San Francisco", "Austin", "Miami"],
answer: "Austin"
},
{
category: "Country",
question: "What is the largest country?",
choices: ["United States", "China", "Russia"],
answer: "Russia"
},
{
category: "EVs",
question: "What is the best EV?",
choices: ["Tesla", "Ford", "Rivian"],
answer: "Tesla"
}
];
function getRandomQuestion(q){
let randomIndex = Math.floor(Math.random() * q.length)
return q[randomIndex];
};
function getRandomComputerChoice(choices){
let randomIndex = Math.floor(Math.random() * choices.length)
return choices[randomIndex];
};
let ques = getRandomQuestion(questions);
let c = getRandomComputerChoice(ques.choices);
function getResults(question, computerChoice){
if (question.answer === computerChoice){
return `The computers choice is correct!`;
} else {
return `The computer's choice is wrong. The correct answer is: ${question.answer};`
};
};
console.log(getResults(ques, c));
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/141.0.0.0 Safari/537.36
Challenge Information:
Build a Quiz Game - Build a Quiz Game