Tell us what’s happening:
I cannot get User Stories 8 and 9 to pass with the getRandomQuestion and getRandomComputerChoice functions.
My code is kind of a mess, but I’m really just trying to understand why I am having a hard time accessing the keys and properties in the objects, ESPECIALLY when I have to access choices. I keep pulling the whole array of choices rather than it randomly choosing one.
Your code so far
const questions = [{
category: "science",
question: "Is science cool?",
choices: ["yes", "no", "maybe"],
answer: "yes"
}, {
category: "geography",
question: "Is geography cool?",
choices: ["yes", "no", "maybe"],
answer: "yes"
}, {
category: "math",
question: "Is math cool?",
choices: ["yes", "no", "maybe"],
answer: "yes"
}, {
category: "english",
question: "Is english cool?",
choices: ["yes", "no", "maybe"],
answer: "yes"
}, {
category: "art",
question: "Is art cool?",
choices: ["yes", "no", "maybe"],
answer: "yes"
}];
function getRandomQuestion(questions) {
// Check if the array is empty
if (questions.length === 0) {
return null;
}
// Generate a random index within the array's length
const randomIndex = Math.floor(Math.random() * questions.length);
// Return the question property of the randomly selected object
return questions[randomIndex].question;
}
function getRandomComputerChoice(questions, choices) {
const questions = questions[choices];
const randomIndex = Math.floor(Math.random() * questions.length);
return questions[randomIndex];
}
console.log(getRandomQuestion(questions));
console.log(getRandomComputerChoice(questions));
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36
Challenge Information:
Build a Quiz Game - Build a Quiz Game