Build a Quiz Game - Build a Quiz Game

Tell us what’s happening:

Hi there,

I am stuck on returning a result from step 9. I don’t quite know how to use the correct parameters for the function in order to call the function with an object of a random question.

When I console log the variables I have created for each function, I get the correct response but can’t quite make them work in the final function.

Some pointers in the right direction would be much appreciated.

Your code so far

const questions= [
  {category: "colours" ,
  question: "Colour of the kiwi fruit is?", 
  choices: ["red", "green", "blue"], 
  answer: "green"},
  
  {category: "continent" ,
   question: "What continent is Nigeria in?", 
   choices: ["America", "Africa", "Europe"], 
   answer: "Africa"},

  {category: "Foods" ,
   question: "What is a Tomato?", 
   choices: ["Vegetable", "Protein", "Fruit"], 
   answer: "Fruit"},

  {category: "Cats" ,
   question: "What is the world's biggest cat?", 
   choices: ["Lion", "Tiger", "Lynx"], 
   answer: "Tiger"},

   {category: "population" ,
   question: "How many billion people are there in the world?", 
   choices: ["7", "8", "9"], 
   answer: "8"},
]

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

const listChoices = getRandomQuestion(questions).choices


function getRandomComputerChoice(listChoices){
  let numberIndex= (Math.floor(Math.random()*listChoices.length))
  let randomAnswer= listChoices[numberIndex]
  return randomAnswer
}

const randomQuery= getRandomQuestion(questions)
const computerResponse= getRandomComputerChoice(randomQuery.choices)
const correctResponse= randomQuery.answer

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

getResults()



Your browser information:

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

Challenge Information:

Build a Quiz Game - Build a Quiz Game

do you expect this to ever be false?

That was a typo from trying to fix the code. It was to be

if (computerResponse === correctResponse)

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

You never use the randomQuery parameter in your function.

Try testing out your program to see if it works.


getResults()

Try logging this function call to the console (and providing the correct arguments)

and where does correctResponse come from?

Yes I haven’t as I can’t quite work out how to. I have also tried logging the function with various arguments but there is something glaring obvious that I can’t seem to grasp.

I’ve tried this but that seems redundant as just a way to get the randomQuery in the function

if(randomQuery || computerResponse === correctResponse)

what would randomQuery be?

and also where does correctResponse come from?

correctResponse is randomQuery.answer

from the answer key in the object

there isn’t a line inside the function about that tho, so is the answer always the same even when we change the question?

Many thanks for the pointers.

If this is true then randomQuery will never be equal to correctResponse

I would suggest more descriptive names for your variables so it’s more clear what they contain. Also try not to have any global variables, as little as absolutely necessary.

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