Build a Quiz Game - Build a Quiz Game

Tell us what’s happening:

Not sure what I’m doing wrong. There is obviously something wrong with my last function, but i’m not sure what i need to do to fix it.

Your code so far

const questions = [
  {
    category: "Doctor Who", 
    question: "What is the name of the highly versatile handheld tool frequently used by the Doctor?", 
    choices: ["sonic hairdryer", "sonic screwdriver", "sonic dildo"], 
    answer: "sonic screwdriver"
  },
  {
    category: "Doctor Who", 
    question: "What is the name of the police box which serves as the Doctor’s time machine?", 
    choices: ["TARDIS", "DeLorean", "portable toilet"], 
    answer: "TARDIS"
  },
  {
    category: "Doctor Who", 
    question: "What did the 5th Doctor wear on his lapel?", 
    choices: ["cucumber", "celery", "shallots"], 
    answer: "celery"
  },
  {
    category: "Doctor Who", 
    question: "What is the name of the dog-like robot in Doctor Who?", 
    choices: ["K-9", "Max", "Klaus"], 
    answer: "K-9"
  },
  {
    category: "Doctor Who", 
    question: "What is the name of the Doctor’s home planet?", 
    choices: ["Earth", "Raxacoricofallapatorius", "Gallifrey"], 
    answer: "Gallifrey"
  }
];

let selectedQuestion = [];
let possibleChoices = [];
let selectedAnswer = "";
let correctAnswer = ""


function getRandomQuestion(questions) {
  
  const randomIndex = Math.floor(Math.random() * questions.length);
  const randomQuestion = questions[randomIndex];
  selectedQuestion = randomQuestion;
  possibleChoices = randomQuestion.choices;
  correctAnswer = randomQuestion.answer;
  return randomQuestion;
}
 



function getRandomComputerChoice(possibleChoices) {
  
  const randomIndex = Math.floor(Math.random() * possibleChoices.length);
  const randomAnswer = possibleChoices[randomIndex];
  selectedAnswer = randomAnswer;
  return randomAnswer;
}



function getResults(arr, choices) {
  
  if (selectedAnswer === correctAnswer) {
    return "The computer's choice is correct!"
  } else {return "The computer's choice is wrong. The correct answer is: " + correctAnswer}
}

console.log(getResults(questions, selectedAnswer))

Your browser information:

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

Challenge Information:

Build a Quiz Game - Build a Quiz Game
https://www.freecodecamp.org/learn/full-stack-developer/lab-quiz-game/lab-quiz-game

Here are some troubleshooting steps you can follow. Focus on one test at a time:

  1. Are there any errors or messages in the console?
  2. What is the requirement of the first failing test?
  3. Check the related User Story and ensure it’s followed precisely.
  4. What line of code implements this?
  5. What is the result of the code and does it match the requirement? (Write the value of a variable to the console at that point in the code if needed.)

If this does not help you solve the problem, please reply with answers to these questions.

There are no errors - the console just lists the 3 or 4 incomplete tests. I suppose i can just go through them here one at a time.

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.

I believe i have done this correctly. When i log the result to the console, it returns a random answer to the selected question, as requested.

function getRandomComputerChoice(choices) {
  
  const randomIndex = Math.floor(Math.random() * possibleChoices.length);
  const randomAnswer = possibleChoices[randomIndex];
  selectedAnswer = randomAnswer;
  return randomAnswer;
}
  1. Your getResults function should take the question object as the first parameter and the computer’s choice as the second parameter.
  2. If the computer choice matches the answer, getResults should return The computer’s choice is correct!
  3. If the computer choice doesn’t match the answer, getResults should return The computer’s choice is wrong. The correct answer is: , where is the value of the correct answer to the chosen question.

These 3 all relate to this function. I definitely have no idea why #12 & #13 are failing.

function getResults(one, two) {
  
  if (selectedAnswer === correctAnswer) {
    return "The computer's choice is correct!"
  } else {return "The computer's choice is wrong. The correct answer is: " + correctAnswer}
}
{ category: 'Doctor Who',
  question: 'What is the name of the police box which serves as the Doctor’s time machine?',
  choices: [ 'TARDIS', 'DeLorean', 'portable toilet' ],
  answer: 'TARDIS' }
computer answer: TARDIS
The computer's choice is correct!

My console.log’s appear to be returning the correct information.

I can only guess what the issue is - maybe it’s something to do with the parameters in my functions? but i’m really not sure.

what is possibleChoices?

please read again user story 11

i declared some global variables to store all the information. That was on the only way i could think of that I could use data from one function in another function.

let selectedQuestion = [];
let possibleChoices = [];
let selectedAnswer = "";
let correctAnswer = "";

and i’ve read test #11 a hundred times - i’m really not sure what else i can do

Don’t look at all the tests like this. Focus on one test at a time.

Your code passes test 9 when I test it. The first test it fails is test 11. Is that different for you?

i was making a few changes - so that is why #9 wasn’t passing for me. But i have changed it back and now its passing again.

Ok, so from test 11 only, please answer those questions.

Here are some troubleshooting steps you can follow. Focus on one test at a time:

  1. Are there any errors or messages in the console?
  2. What is the requirement of the first failing test?
  3. Check the related User Story and ensure it’s followed precisely.
  4. What line of code implements this?
  5. What is the result of the code and does it match the requirement? (Write the value of a variable to the console at that point in the code if needed.)

If this does not help you solve the problem, please reply with answers to these questions.

11. Your getResults function should take the question object as the first parameter and the computer’s choice as the second parameter.

You should have a function named getResults that takes the question object as the first parameter and the computer’s choice as the second parameter. The function should return The computer's choice is correct! if the answer is correct. Otherwise, it returns The computer's choice is wrong. The correct answer is: <correct-answer>, where <correct-answer> is the value of the correct answer to the chosen question.

function getResults(arr, choices) {
  
  if (selectedAnswer === correctAnswer) {
    return "The computer's choice is correct!"
  } else {return "The computer's choice is wrong. The correct answer is: " + correctAnswer}
}
{ category: 'Doctor Who',
  question: 'What is the name of the highly versatile handheld tool frequently used by the Doctor?',
  choices: [ 'sonic hairdryer', 'sonic screwdriver', 'sonic dildo' ],
  answer: 'sonic screwdriver' }
The computer's choice is wrong. The correct answer is: sonic screwdriver

I honestly don’t know what else to do

getResults(arr, choices)

Does arrstand for array? An array isn’t an object.

The second parameter choices is that going to be “the computer’s choice”? Which would be 1 choice? Why is it plural?

You should use those parameters in your function, you never refer to them. The instructions do not say to create and use global variables so don’t.

You aren’t following the instructions. You aren’t using the question object and computer’s choice as parameters.

ok i’ve removed all the Global variables

function getRandomQuestion(arr) {
  return arr[Math.floor(Math.random() * arr.length)];
}
 
console.log(getRandomQuestion(questions));


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

But how can i tell the 2nd function which question the 1st function randomly selected? So it knows which answers to choose from?

for that you need to save the result of getRandomQuestion in a variable, and call getRandomComputerChoice using that variable

i got it to work, thank for you for your help