Build a Quiz Game - Build a Quiz Game

Tell us what’s happening:

I don’t know what is wrong with step no. 13.
is it a typo or the return data is not what expected?

Your code so far

const questions = [];

const question1 = {
  category:"History",
  question: "What was the year of Mexican Independece?",
  choices: ["1801", "1700", "1810"],
  answer: "1810"
}

const question2 = {
  category:"History",
  question: "Who was the first french Emperor?",
  choices: ["Napoleon", "Jaqces de Soir", "Ney"],
  answer: "Napoleon"
}

const question3 = {
  category: "Science",
  question: "What is the best medicine?",
  choices: ["Ki-Cab", "Omeprazol", "Yakult"],
  answer: "Ki-Cab"
}

const question4 = {
  category: "Science",
  question: "Who is the best lab?",
  choices: ["Faizer", "Bayer", "Carnot"],
  answer: "Carnot"
}

const question5 = {
  category: "Science",
  question: "Who is the best rep?",
  choices: ["Josse", "Fabia", "Luis"],
  answer: "Josse"
}

questions.push(question1, question2, question3, question4, question5);

let random = Math.floor(Math.random() * 5) //For the Questions
let random1 = Math.floor(Math.random() * 3) // For the choices
const { choices: randomChoice } = questions[random]; // Choices destructing

function getRandomQuestion (questions) {
  return questions[random];
}

//console.log(getRandomQuestion(questions));

function getRandomComputerChoice (randomChoice) {
  return randomChoice[random1];
}
//console.log(getRandomComputerChoice(randomChoice));
 
 let ants1 = getRandomQuestion(questions);
 let ants2 = getRandomComputerChoice(randomChoice);
 let { answer } = questions[random]

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


Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36 OPR/102.0.0.0

Challenge Information:

Build a Quiz Game - Build a Quiz Game

Welcome to the forum @yyjhpqmdbw

What is another way get the answer?

Happy coding

you need to get the random number inside the functions, not outside, so that they generate a different value each time they are called

But if I am not mistaken, the question and choice are random, when I activated the console.log for the three I notice that the questions are not in accordance with the choice and the result, is this the problem? because I only have the 13th step wrong and it’s just about the message that is needed to return.

Maybe. How can you get the "correct answer" for each random choice?

Happy coding

here the answer variable comes from outside the function, you should not refer to global variables from inside the function

this is also valid for calculating random numbers, calculate the random number inside the function

Okay I think I got it, thank you, I will try it