Build a Quiz Game - Build a Quiz Game

Tell us what’s happening:

What am I doing wrong that my function isn’t working right? My first 2 function aren’t passing and I am looking at a hint of where I am missing/going wrong, spend like 2 hours on it put I am confused.

Your code so far

const questions = [
{
    category: 'Tech',
    question: 'What is most popular phone now a days?',
    choices: ['blackberry','iPhone','nokia'],
    answer: 'iPhone'
  },
    {
    category: 'Food',
    question: 'What is most popular food in Pakistan?',
    choices: ['Plain rice','Biryani','Korma'],
    answer: 'Biryani'
  },
    {
    category: 'Takeaway',
    question: 'What is most popular takeaway?',
    choices: ['Philly','Burgerism','Grubbies'],
    answer: 'Philly'
  },
    {
    category: 'Airlines',
    question: 'What is most popular airlines?',
    choices: ['Emirates','Etihad','Qatar'],
    answer: 'Emirates'
  },
    {
    category: 'Travel',
    question: 'What is most popular SVU?',
    choices: ['Toyota','Kia','JLR'],
    answer: 'Kia'
  },
]
function getRandomQuestion(arr){
  return arr[Math.floor(Math.random() * arr.length)].question
}
function getRandomComputerChoice(answerArr){
  return answerArr[Math.floor(Math.random() * answerArr.length).answer]
}
function getResults(arr, answer){
  if(arr.question !== answer){
        return `The computer's choice is wrong. The correct answer is ${answer}`;
  }else{
        return "The computer's choice is correct!";
  }
}

Your browser information:

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

Challenge Information:

Build a Quiz Game - Build a Quiz Game

what are you asked to return from getRandomQuestion? please check the user stories again

for the second one, what is the function receiving? please consult the user stories on this too

1 Like

For the first user story, it says

You should have a function named getRandomQuestion that takes an array of questions as a parameter and returns a random question object from the array.

The confusing part is last bit where it says “returns a random question object from the array.” So not a question but the whole object as question itself is a key and its value is a string.

yes, the whole object

fix that and you will pass a test

The first one passed but for second one

function getRandomComputerChoice(arr){
  return arr[Math.floor(Math.random() * answerArr.length)].answer;
}

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.

What am I missing, when running the code on my machine, I do have a random answer from choices, then what am I missing?

can you find an example in your code of an array with the available choices?

1 Like

you mean like this

function getRandomComputerChoice(answerArr){

return answerArr[Math.floor(Math.random() * answerArr.length)].choices[Math.floor(Math.random())];

}

But doing that doesn’t fix the error, but it gives me a value from array of choices, but the user stories says ‘answer’ but here we are outputting choices

no, I am asking you to point out an example of the choices array, not to change your function

example of the choice array? So you mean like

    {

    category: 'Travel',

question: 'What is most popular SVU?',

choices: ['Toyota','Kia','JLR'],

answer: 'Kia'

  },

choices array in it?

so this is the choices array, or an example of it, right?

so when you write

does it work here?

sorry I am confused. I got this code now

function getRandomComputerChoice(answerArr){

  console.log(answerArr[Math.floor(Math.random() * answerArr.length)].choices[Math.floor(Math.random())]);

}

and it does give me random answers from the choices.

how are you calling it to test?

getRandomComputerChoice(questions) like this.

that is not an array of choices

but in the function body, I am accessing the array of choices when I do
console.log(answerArr[Math.floor(Math.random() * answerArr.length)].choices[Math.floor(Math.random())]);

no, you can’t change how the function works, the function need to work receiving the array of choices of a specific question, not the array of questions

Not really getting it because function parameter can be only be like

function getRandomComputerChoice (arr){ }

and in function body I will do console.log(answerArr[0].choices[1])

Edit: I hardcoded values for the purpose of being quick to draft something.

What am I missing?

and arr need to be an array of choices, not the questions array

because the user story say that the function gets an array of choices and give back one of them