Build a Quiz Game - Build a Quiz Game

Tell us what’s happening:

TypeError: Cannot read properties of undefined (reading ‘length’)

cannot see error stated above

Your code so far

let questions=[

{
  category:"Music",
  question:"Favourite song?",
  choices:["classic","jazz","old"],
  answer:"classic"
},
{
category:"subject",
  question:"Favourite subject?",
  choices:["maths","english","science"],
  answer:"maths"
},
{ 
category:"hobbies",
  question:"Favourite hobby?",
  choices:["travelling","dancing","shopping"],
  answer:"shopping"

},
{ 
category:"Favourite city",
  question:"Which is favourite city?",
  choices:["b'gham","handsworth","coventry"],
  answer:"coventry"

},
{ 
category:"Religion",
  question:"Favourite religion?",
  choices:["sikh","hindu","muslim"],
  answer:"sikh"

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

console.log(randomQ);
return randomQ;
}

getRandomQuestion();


function getRandomComputerChoice (choices)
{
  let randomNum = Math.floor(Math.random() * questions.length);
   
let randomchoice=questions[randomNum].choices;
console.log(randomchoice);
return randomchoice;
}
getRandomComputerChoice();




Your browser information:

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

Challenge Information:

Build a Quiz Game - Build a Quiz Game

what do you think will be the value of questions inside the function if you don’t pass anything when you call the function?

Ok I have updated as getRandomQuestion(questions);

and it worked however : getRandomComputerChoice(choices); do not work

do you have a variable choices to use? if not you need to create it or pass something else

Its in reference to this point: 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.

Choices are in array so will it not worrk like this?

What is the “array of available choices” and where is it?

You need to use the choices parameter in your function. You never use it.

You need to write the function assuming that the “array of available choices” has been passed as an argument to your function, and access it by the choices parameter.

Please find my updated code : two issues

1- it does not show different question when I call function more than once.

2-I do not see result is working properly . I am not sure what to put in when we call function get results as argument.

let questions=[

{
  category:"Music",
  question:"Favourite song?",
  choices:["classic","jazz","old"],
  answer:"classic"
},
{
category:"subject",
  question:"Favourite subject?",
  choices:["maths","english","science"],
  answer:"maths"
},
{ 
category:"hobbies",
  question:"Favourite hobby?",
  choices:["travelling","dancing","shopping"],
  answer:"shopping"

},
{ 
category:"Favourite city",
  question:"Which is favourite city?",
  choices:["b'gham","handsworth","coventry"],
  answer:"coventry"

},
{ 
category:"Religion",
  question:"Favourite religion?",
  choices:["sikh","hindu","muslim"],
  answer:"sikh"

},
];
//get random question number and question
let randomNum = Math.floor(Math.random() * questions.length);

//FUNCTION ONE QUESTION
function getRandomQuestion (questions)
{
let randomQ=questions[randomNum].question;
console.log(`Random question: ${randomQ}.`)
return randomQ;
}

//FUNCTION TWO CHOICE
let randomCh=questions[randomNum].choices;

function getRandomComputerChoice (randomCh)
{
  
//let randomNum = Math.floor(Math.random() * questions.length);
console.log(`Random choice: ${randomCh}.`)
return randomCh;
}


//FUNCTION THREE RESULT

function getResults (randomQ,randomCh)
{
let correctAnswer=questions[randomNum].answer;
  
  if(randomCh===correctAnswer)
  {
  console.log(`The computer's choice is correct!`);
  }
  else
  {
  console.log(`The computer's choice is wrong. The correct answer is:${correctAnswer}`);
  }
}


getRandomQuestion(questions);
getRandomComputerChoice (randomCh);

getResults("math");




that happens because you do not calculate a new random number when the function is called, but you use this

a question object, and an answer to check if it is the right one, so what you return from the previous two questions

also note that your getRandomQuestion is not returning a question object, please update

Have updated first two functions and it start giving new questions and new available choices not but should the choices should be corresponding to question , as it shows diff questions and different choices from other question.

I am so confused if you could tell me what is end product so I can work accordingly as to me we should have question showing available choices to pick and in result function we should give one choice which should be matched against correct answer. here my two updated function below and will work on get result one once it work correctly. Second function showing choiceds without me putting any argument when I call function.

//FUNCTION ONE QUESTION
function getRandomQuestion (questions)
{
let randomNum = Math.floor(Math.random() * questions.length);
let randomQ=questions[randomNum].question;
console.log(`Random question: ${randomQ}.`)
return randomQ;
}

getRandomQuestion(questions);

//FUNCTION TWO CHOICE


function getRandomComputerChoice (choice)
{
  
let randomNum = Math.floor(Math.random() * questions.length);
let randomCh=questions[randomNum].choices;
console.log(`Random choice: ${randomCh}.`)
return randomCh;
}
getRandomComputerChoice ();

if you save the output to this to a variable

and use that variable to get the array of choices to pass to

what do you think?

not working

//FUNCTION ONE QUESTION
function getRandomQuestion (questions)
{
let randomNum = Math.floor(Math.random() * questions.length);
let randomQ=questions[randomNum].question;
console.log(`Random question: ${randomQ}.`)
return randomQ;
}

let arr=getRandomQuestion(questions);

//FUNCTION TWO CHOICE


function getRandomComputerChoice (arr)
{
  
let randomNum = Math.floor(Math.random() * questions.length);
//let randomCh=questions[randomNum].choices;
let randomCh=arr["choices"];

console.log(`Random choice: ${randomCh}.`)
return randomCh;
}
getRandomComputerChoice ();
  1. 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.

What does this user story say you should return from getRandomQuestion?

But what are you returning from that function?

this return a random question……………is it not right?

It needs to return a random question from the questions in the variable passed as an argument.

No. You are returning the question in the question object. You should be returning the entire question object. Then you can use that question object to get the choices to pass to getRandomComputerChoice. Make sense?

can it be more simplify sorry but its so confusing ….getting entire object means gettting all as in ..choices, answers, catagory etc. not only one question from that one object.

Yes! You want to return the entire object.

Just like it says in the instructions.

The questions array is an array of question objects, right?

Pretend that you write all of the stuff in each question object on a piece of paper, mix up all those papers in a pile, and pick one. Now you have a piece of paper with the category, question, choice, and answer for one randomly selected question object. Next you write down each choice from that specific question object on a piece of paper, mix them up in a pile, and pick one. Now you have a randomly selected choice from that question object, which, in this case, represents the computer choice. Because you are passing the entire question object to getResults, you can access the answer and compare that to the computer choice.

I think I got it right this time but I stilll do not get what to pass as argument in calling second function.

//FUNCTION ONE QUESTION
function getRandomQuestion (questions)
{
let randomNum = Math.floor(Math.random() * questions.length);
//let randomQ=questions[randomNum].question;
//let randomQ=questions[randomNum];
//console.log(`Random question: ${randomQ}.`)
return questions[randomNum];
}

let ranQuestion=(getRandomQuestion(questions));

console.log(ranQuestion);

//FUNCTION TWO CHOICE


function getRandomComputerChoice (choice)
{
  
let randomNum = Math.floor(Math.random() * questions.length);
//let randomCh=questions[randomNum].choices;
let randomCh=ranQuestion.choices[randomNum];

console.log(`Random choice: ${randomCh}.`)
return randomCh;
}
getRandomComputerChoice ();

this is output for this code:

{ category: 'subject',
  question: 'Favourite subject?',
  choices: [ 'maths', 'english', 'science' ],
  answer: 'maths' }
Random choice: maths.

Good job on the getRandomQuestion changes! Please read the edit to my last post to help you understand how to use that result to get the choices to pass to getRandomComputerChoice.