Build a Quiz Game - Build a Quiz Game

Tell us what’s happening:

Hi, I am trying to run the code for the getResults() function to see what i get from it.

I have treid to write some code out to see if i get the correct return from the function, but am unsure of what to write.

let randomChoice = getRandomComputerChoice()
getResults(question[1], randomChoice)

Your code so far

const questions = [
 {
    category: "category1",
    question: "question1?",
    choices: ["choices11", "choices12", "answer1"],
    answer: "answer1",
  },

 {
    category: "category2",
    question: "question2?",
    choices: ["choices21", "choices22", "answer2"],
    answer: "answer2",
  },
 {
    category: "category3",
    question: "question3?",
    choices: ["choices31", "choices32", "answer3"],
    answer: "answer3",
  },
 {
    category: "category4",
    question: "question4?",
    choices: ["choices41", "choices42", "answer4"],
    answer: "answer4",
  },
 {
    category: "category5",
    question: "question5?",
    choices: ["choices51", "choices52", "answer5"],
    answer: "answer5",
  }

];

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

function getRandomComputerChoice (choices) {
  let randomChoice = Math.floor(Math.random() * choices.length);
  return choices[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}`
  }
};

let randomChoice = getRandomComputerChoice()
getResults(question[1], randomChoice)

Your browser information:

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

Challenge Information:

Build a Quiz Game - Build a Quiz Game

An error is popping up with: TypeError: Cannot read properties of undefined (reading ‘length’).

I’m not sure what this means though.

Are you calling getRandomComputerChoice() correctly here? Don’t you need something? And is question[1] the way to get a random question?

Is this correct?

let randomQuestion = getRandomQuestion (questions)

let randomChoice = getRandomComputerChoice(choices)

getResults(randomQuestion, randomChoice)

Close. Where should your choices parameter by coming from?

The object or the function? I’m not actually sure, haha.

You first got a randomQuestion object by calling getRandomeQuestion(). Now, in your call to getRandomComputerChoice() you pass in a parameter, choices, right? Where should you be getting those choices from?

from the randomQuestion object?

Seems logical. Try it.

I’m not sure how i should write that out in my code. Thus this is all i have come up with haha

let randomQuestion = getRandomQuestion (questions);

let randomChoice = randomQuestion.getRandomComputerChoice(choices);

getResults(randomQuestion, randomChoice)

randomQuestion is not a class with a getRandomComputerChoice() method, but you’ve written it like it is. How would you get the choices property from the randomQuestion object? Can you write that?

let randomQuestion = getRandomQuestion (questions);

let randomChoice = getRandomComputerChoice(randomQuestion.choices);

getResults(randomQuestion, randomChoice);

Thank you so much. i really appreciate it, been working on this for the entire day haha.
<3

1 Like