Build a Quiz Game - Build a Quiz Game

Tell us what’s happening:

I have problems with the functions. I read the familiar questions and I tried different codes but it doesn’t work.
The console.logs of function 1 und 2 show the correckt results but I didn’t pass tasks 8 to 14.

Can you guys give me tips or hints, please?

Your code so far

const questions = [];

const questions1 = {
category: "Question 1:",
question: "When starts WW2?",
choices: ["1928", "1918", "1939"],
answer: "1939",
};

const questions2 = {
category: "Question 2:",
question: "Who was not a german president?",
choices: ["Angela Merkel", "Friedrich Merz", "Sido Aggro"],
answer: "Sido Aggro",
};

const questions3 = {
category: "Question 3:",
question: "Which is not a primary color?",
choices: ["red", "green", "yellow"],
answer: "green",
};

const questions4 = {
category: "Question 4:",
question: "Which language is a programming language?",
choices: ["HTML", "CSS", "JavaScript"], 
answer: "JavaScript",
};

const questions5 = {
category: "Question 5:",
question: "What is the heighest mountain in europe?",
choices: ["Mont Blanc", "Zugspitze", "Matternhorn"],
answer: "Mont Blanc",
};

questions.push(questions1, questions2, questions3, questions4, questions5);

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

console.log(getRandomQuestion())
console.log(getRandomQuestion())
console.log(getRandomQuestion())


function getRandomComputerChoice(choices) {
let getchoices = Math.floor(Math.random() * questions.length);
 return questions[getchoices].answer;
};

console.log(getRandomComputerChoice())
console.log(getRandomComputerChoice())
console.log(getRandomComputerChoice())
console.log(getRandomComputerChoice())

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

console.log(getResults(1, 2))
console.log(getResults(2, 4))
console.log(getResults(1, 1))

I also tried this function what was in other forum sections:

but the console only shows “undefined”

Your browser information:

User Agent is: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/142.0.0.0 Safari/537.36

Challenge Information:

Build a Quiz Game - Build a Quiz Game

Is 1 a random question? Is 2 a random choice from the random question?

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

This should be a random question and random choice?

The random choice needs to come from the random question object.

Sorry I don’t get it. I know what you mean, but I don’t know how to fix the syntax.
My first idea was just this:
console.log(getResults(getRandomQuestion(), getRandomQuestion(getRandomComputerChoice());

But it doesn’t works.

Before we continue with the console.log, are the two functions correct?

Take a look at this function. Is the length of the global questions array the same as the length of a choices array within a question object?

BTW, you can always create a variable to hold the value of your random question. You’re the programmer! :wink:

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

const randomQuestion = getRandomQuestion(question); // I'm not sure what I can choose for the parameter.

console.log(getRandomQuestion())
console.log(getRandomQuestion())
console.log(getRandomQuestion())

function getRandomComputerChoice(choices) {
let getchoices = Math.floor(Math.random() * choices.length); // I corrected this. But console shows an errore that `length` is not defined. 
 return choices[getchoices];
};

That’s my first idea. My brain smokes :sweat_smile: I’ll try again tomorrow.
Thanks for your help.

Shouldn’t the parameter be the array you are getting the random question object from?

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

const randomQuestion = getRandomQuestion(questions);

console.log(randomQuestion);

function getRandomComputerChoice(randomQuestion) {
let getchoices = Math.floor(Math.random() * randomQuestion.length);
 return randomQuestion[getchoices];
};

const computerChoice = getRandomComputerChoice(randomQuestion.choices);
console.log(computerChoice);

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

Finally my updated code. Function 1 and 2 should be correct because they passes the tests.

But I don’t know why. If I log randomQuestion is my output:

{ category: ‘Question 3:’,
question: ‘Which is not a primary color?’,
choices: [ ‘red’, ‘green’, ‘yellow’ ],
answer: ‘green’ }

I thought the log should only show one random question and not the full block?
My last function still not working.

I passed it. Finally I forgot a result :smiley: