Build a Quiz Game - Help please!

Tell us what’s happening:

I can’t pass test 9. “You should have a function named getRandomComputerChoice that takes the array of the available choices as a parameter, and returns a random answer to the selected question.” It doesn’t like the parameters for the array, when I try questionObject.choices it gives me a syntax error. Other options don’t show an error, but don’t pass the test. Code works though.

Your code so far

const questions = [
  {
    category: "ABCs",
    question: "What is the 1st number of the English alphabet?",
    choices: ["B", "A", "C"],
    answer: "A",
  },
  {
    category: "ABCs",
    question: "What letter comes after J?",
    choices: ["H", "I", "K"],
    answer: "K",
  },
  {
    category: "123s",
    question: "What is 1 + 2?",
    choices: ["3", "5", "8"],
    answer: "3",
  },
  {
    category: "123s",
    question: "What is 2 + 2?",
    choices: ["2", "4", "10"],
    answer: "4",
  },
  {
    category: "123s",
    question: "What is 5 + 2?",
    choices: ["1", "6", "7"],
    answer: "7",
  }
]



function getRandomQuestion(questions) {
  let randomNum= Math.floor(Math.random()*questions.length);
  return questions[randomNum];
}

let questionObject = getRandomQuestion(questions);
let choices = questionObject.choices;


function getRandomComputerChoice(choices) {
  let randomNum1= Math.floor(Math.random()*3);
  return questionObject.choices[randomNum1];
}

let computerChoice = getRandomComputerChoice(questionObject.choices);

function getResults(questionObject, computerChoice) {
  if (computerChoice == questionObject.answer) {
    return "The computer's choice is correct!"
  }
  else {
    return `The computer's choice is wrong. The correct answer is: ${questionObject.answer}`
  }
}

let result = getResults(questionObject, computerChoice);

console.log(result)

console.log(questionObject.question + "\n" + computerChoice + "\n" + result);


Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/140.0.0.0 Safari/537.36

Challenge Information:

Build a Quiz Game - Build a Quiz Game

where are you using the choices parameter inside the function?

this is not using the parameter, this is accessing a property literally named choices on questionObject`, and this is not what you are asked to do

I have tried it with just “choices” without the variable on questionObject, but it was not letting me pass. So added the variable was just another try to get passed the test


function getRandomQuestion(questions) {
  let randomNum= Math.floor(Math.random()*questions.length);
  return questions[randomNum];
}

let questionObject = getRandomQuestion(questions);


function getRandomComputerChoice(choices) {
  let randomNum1= Math.floor(Math.random()*3);
  return questionObject.choices[randomNum1];
}

let computerChoice = getRandomComputerChoice(questionObject.choices);

This was the original, but it still doesn’t pass. Code works though

it does not work, no, because it does not follow the requirements

there is a requirement that the function takes an array as argument, but you then never use it

1 Like

Got it now. Between your response and further research, I was able to understand the problem. Thanks