Build a Quiz Game - Build a Quiz Game

Tell us what’s happening:

I don’t undertand user storie of getRandomComputerChoice. I know that I need to get the array choices of the selected object of the array questions and get just one random, but I don’t understand how to connect them. Maybe I am seeing with a perspective that I shouldn’t.

Your code so far

const questions = [
  {
    category: "Food",
    question: "Fav food?",
    choices: ["Pasta","Pizza","Burger"],
    answer: "Pasta",
  }, 
  {
    category: "Car",
    question: "Fav car?",
    choices: ["BMW","SEAT","OPEL"],
    answer: "SEAT",
  }, 
  {
    category: "Footbal Team",
    question: "Fav Football Team?",
    choices: ["Real Madrid","Barcelona","Las Palmas"],
    answer: "Las Palmas",
  }, 
  {
    category: "Brand Clothes",
    question: "Fav Brand Clothes?",
    choices: ["Nike","Adidas","Supreme"],
    answer: "Nike",
  }, 
  {
    category: "Music Genre",
    question: "Fav Music Genre?",
    choices: ["Reggaeton","Trap","Rap"],
    answer: "Reggaeton",
  }, 
];

function getRandomQuestion(arrQuestions) {
  return arrQuestions[Math.floor(Math.random() * arrQuestions.length)];
}


function getRandomComputerChoice(arrChoices) {
  const getChoices = Math.floor(Math.random() * arrChoices.length)

  console.log(getChoices)
  return arrChoices[getChoices].choices
}

function getResults() {

}


console.log(getRandomQuestion(questions))
console.log(getRandomComputerChoice(questions))





Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/145.0.0.0 Safari/537.36

Challenge Information:

Build a Quiz Game - Build a Quiz Game

Example of the argument, which getRandomComputerChoice function would get is following:

["Pasta","Pizza","Burger"]
  • as per your shared code snippet, you are currently returning an array of answers, but you are supposed to return “one” randomly selected answer.

you can ask yourself how you can get a “randomly generated” number that us valid for those “choices” and try returning that

happy coding :slight_smile:

So, it doesn’t have to be from the same object

function getRandomComputerChoice(arrChoices) {
  const getChoices = arrChoices[Math.floor(Math.random() * arrChoices.length)].choices

  return getChoices[Math.floor(Math.random() * getChoices.length)]
}

I uploaded the function like this, but it won’t pass the test 9

you should double check what is inside the parameter arrChoices

isn’t it an array of strings like

so there isn’t a choices property to access?

I got it, I misunderstood that calling questions are just the objects so I needed something to store the question object result from getRandomQuestion. Thank you so much

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