Need Help with understanding the Build a Quiz Game - JavaScript

const questions = [
  {
    category: "Math",
    question: "What does 9 times 7 equal?",
    choices: ["64", "52", "70"],
    answer: "64"
  },
  {
    category: "Science",
    question: "What is the formula for Oxygen?",
    choices: ["H20", "Fe203", "02"],
    answer: "H20"
  },
  {
    category: "TV",
    question: "How many seaons does the hit show 'Supernatural' have?",
    choices: ["12", "15", "13"],
    answer: "15"
  },
  {
    category: "Geography",
    question: "What mountain range spans through the state of Tennessee?",
    choices: ["Appalachain Mountains", "Indes Mountains", "Smoky Mountains"],
    answer: "Smoky Mountains",
  },
  {
    category: "Movies",
    question: "In which Harry Potter movie does Harry compete in the Tri-wizard Tournament?",
    choices: ["Order of the Phoenix", "Goblet of Fire", "Half-BLood Prince"],
    answer: "Goblet of Fire",
  },
];

function getRandomQuestion(questions) {
  const randomIndex = Math.floor(Math.random() * questions.length);
  const randomObject = questions[randomIndex];
  return randomObject;
}

function getRandomComputerChoice(choices) {
  const randomIndex = Math.floor(Math.random() * 3);
  const randomChoice = choices[randomIndex];
  return randomChoice;
}

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

I am not completely sure how this code passed the “Build a Quiz Game” lab. Can anyone help me understand some portions of it? For starters, I don’t understand how the parameter “choices” works when it isn’t referenced to what I had believed it needed to be.

Could you explain how you understand it? It’s easier to help with understanding when you know what the other person assumes about the code. What do you believe choices should be referenced to?

My initial thought was that I was supposed to grab the exact random value that would be generated when using the getRandomQuestion() function. I had assumed using a function within a function would work so the value from the getRandomQuestion would already be within the scope and the exact same value could be used to reference the choices array within that random object. I tried that and it didn’t work. So I had reviewed some of the other forums and decided I would use the filler of choices and move on to complete the last task with the getResults() function and then come back to try and understand that portion. However, it passed the lab once I had finished the remaining tasks and I am not completely sure how. I see the value in having the getResults() function’s two parameters, questionObject and computerChoice , though I am not sure how the function recognizes the match between the [[getRandomQuestion() as questionObject]] and [[getRandomComputerChoice() as computerChoice]] , specifically how it recognizes the exact same randomly chosen object. Another thought that I had was that maybe when providing the parameter values while calling the function, I may not be using the correct way of collecting the choices array in (computerChoice).

While I was typing this, I had a idea and tried it.

console.log(getResults(getRandomQuestion(questions), getRandomComputerChoice(getRandomQuestion(questions).choices))).

This seems to work, but is there not a simpler way to reference the choices array?

To some degree it doesn’t really. Since JavaScript is dynamically typed language, there’s no guarantee whatever is passed to function is the exact object as expected. As long as it can do everything that function needs it to do, there shouldn’t be any errors when executing.

It further depends how these functions are later used together. This is example how they could be used:

const question = getRandomQuestion(questions);
const computerChoice = getRandomComputerChoice(question.choices);
const result = getResults(question, computerChoice);
console.log("Question:", question.question);
console.log("\nPossible answers:", question.choices.join(", "));
console.log("\nComputer choice:", computerChoice);
console.log(result);

Yeah that makes sense now that I have looked it over some more. I think I looked into it a bit too deep and overlooked the functionality of the getRandomComputerChoice() functionality, which is to be called with the choices array from the provided object which can be done like in your example here. I sometimes forget that I can use variables to represent functions. Thanks!