Build a Quiz Game - Build a Quiz Game

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

The instructions didn’t asked to use a parameter for the function in the challenge step.
Edit: you can directly return a random object form the questions array within the getRandomQuestion function.

1 Like

Hello, hasanzaib1389 is correct. just calling the function works with or without the parentheses. A parameter is another name for a placeholder of a real value, while you can use an array as a parameter it is not recommend as it can change the original without you knowing it.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.