Build a Quiz Game - Build a Quiz Game

Tell us what’s happening:

hi i know that i haven’t finished yet but am having real problems with the second function, i am confused how to call for the result of the first function and then tell it i want to select from the “choices” available, any hints on what i need to research to come up with the answer greatly recieved

Your code so far

const questions = [
  {
    catergory: "Photography",
    question: "Who took the first successful photograph?",
    choices: ["J.M Daguerre", "William Henry Fox Talbot", "Nicephore Niepce"],
    answer: "Niecephore Niepce"
  },
  {
    catergory: "Photography",
    question: "What year did Kodak introduce Kodachrome color film?",
choices: ["1941", "1933", "1936"],
answer: "1936"
  },
  {
    catergory: "Nature",
    question: "What is a young eel called",
choices: ["fry", "sprat", "elver"],
answer: "elver"
  },
  {
    catergory: "Food and Drink",
    question: "What fish does caviar come from?",
    choices: ["Strugeon", "Salmon", "Shark"],
    answer: "Sturgeon"
  },
  {
    catergory: "Nature",
    question: "Which bird of prey feeds on small birds?",
    choices: ["Osprey", "Golden Eagle", "Sparrow Hawk"],
    answer: "Sparrow Hawk"
  },
  {
    catergory: "Food And Drink",
    question: "What fish is used to produce Gravadlax?",
    choices: ["Sardine", "Eel", "Salmon"],
    answer: "Salmon"
  }
];
 
 let questionChoice ;
function getRandomQuestion() {
  let random = Math.floor(Math.random() * questions.length);
  return questions[random].question;
};
function getRandomComputerChoice(choices) {
let random2 = Math.floor(Math.random() * 3);
let randomAnswer = choices[random2];
return randomAnswer;
};
console.log(getRandomQuestion());
console.log(getRandomComputerChoice());

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

you are forgetting something here

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.

So then when you call it you pass in the array of questions

if you save the output of the function call (with proper argument), you have a question object

you can then get the possible choices from that object, and call getRandomComputerChoice

notice how you create it with a parameter

so when you call it you also need to pass in a parameter:

 let questionChoice;

function getRandomQuestion(questions) {

  let random = Math.floor(Math.random() \* questions.length);

  let questionChoice = questions\[random\].question;

  return questionChoice;

};

function getRandomComputerChoice(questionChoice, choices) {

let random2 = Math.floor(Math.random() \* 3);

let randomAnswer = questionChoice.choices\[random2\];

return randomAnswer;

};

I have changed the code but am still getting a typeError message not really sure what this means again any pointers greatfully recieved

If you have an error please share the exact text of the error message.

Error messages are extremely useful and often contain the precise information that you need to resolve a problem. What do you think the error message tells you about your code?

I’ve edited your post to improve the readability of the code. When you enter a code block into a forum post, please precede it with three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add the backticks.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').

  1. 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.

The instructions only specify 1 parameter for this function

I suggest that you review each User Story and make sure it’s implemented accurately

If I am reading the error correctly at the moment my code is treating the choices array as a property of the questionChoice variable instead of the array connected to it, but that is as far as i can get. The message is typeError: cannot read properties of undefined ( reading ‘choice’)

please post all your code, properly formatted please, otherwise with the extra characters added in it’s really difficult to debug your code

When you enter a code block into a forum post, please precede it with three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add the backticks.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').

let randomAnswer;
function getRandomComputerChoice(choices) {
let random = Math.floor(Math.random() * choices.length);
let randomAnswer = choices[random];
return randomAnswer;
}
function getResults(randomQuestion, randomAnswer) {
  if (randomAnswer === randomQuestion.answer) {
    return "the computer's choice is correct!";
  } else {
    return "The computer's choice is wrong. The correct answer is: ${randomQuestion.answer}";
  }
}

I have rewritten the code and test 9 for the computerchoice function passes bu t i Know that i am still not connecting the result from the first question choice function with the choices for that questions. really confused

consider using variables in the global space to get a random question and then a random computer choice for that question, and passing those to getResult

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