Tell us what’s happening:
My code fails in step 8 for some reason I don’t understand why.
“8. You should have a function named getRandomQuestion
that returns a random question object from the questions
array.”
Your code so far
const questions = [
{
category: "History",
question: "In what year did World War II begin?",
choices: ["1939", "1941", "1935"],
answer: "1939",
},
{
category: "Science",
question: "Which planet is known as the Red Planet?",
choices: ["Mars", "Jupiter", "Saturn"],
answer: "Mars",
},
{
category: "Sports",
question: "How many players are on a soccer team on the field?",
choices: ["11", "9", "10"],
answer: "11",
},
{
category: "Art",
question: "Who painted the Mona Lisa?",
choices: ["Leonardo da Vinci", "Pablo Picasso", "Vincent van Gogh"],
answer: "Leonardo da Vinci",
},
{
category: "History",
question: "In what year did the French Revolution start?",
choices: ["1788", "1789", "1810"],
answer: "1789",
},
];
const getRandomNumber = (min, array) =>
Math.floor(Math.random() * (Math.floor(array.length) - Math.ceil(min)) + Math.ceil(min));
const getRandomQuestion = (questions) => questions[getRandomNumber(0,questions)];
const randomQuestion = getRandomQuestion(questions);
const getRandomComputerChoice = (arrayChoices) => arrayChoices[getRandomNumber(0, arrayChoices)];
const randomAnswer = getRandomComputerChoice(randomQuestion.choices);
const getResults = (question, randomA) => {
if (question.answer.includes(randomA)) {
return "The computer's choice is correct!";
} else {
return `The computer's choice is wrong. The correct answer is: ${question.answer}`;
}
};
console.log(randomQuestion);
console.log(randomAnswer);
console.log(getResults(randomQuestion, randomAnswer));
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/132.0.0.0 Safari/537.36
Challenge Information:
Build a Quiz Game - Build a Quiz Game