Build a Quiz Game - Build a Quiz Game

Tell us what’s happening:

The quiz works normally on the console but challenge 12 is not passing

Your code so far

let questions = [
{category: 'História',question:'Quem descobriu o Brasil?',choices:['Pedro Alvares Cabral','Pero vaz de caminha','Cristovao Colombo'],answer:'Pedro Alvares Cabral'},
{category: 'Geografia',question:'Qual o nome da capital da Argentina?',choices:['Patagonia','Buenos Aires','Cidade del Leste'],answer:'Buenos Aires'},{category: 'Ciência',question:'Qual é o maior orgão do corpo humano?',choices:['Pele','Fígado','Intestino grosso'],answer:'Pele'},
{category: 'Artes',question:'Quem é o pintor do quadro "Monalisa"?',choices:['Leonardo da Vinci','Picasso','Michelangelo'],answer:'Leonardo da Vinci'},{category: 'Esportes',question:'Maior medalhista olimpico de todos os tempos?',choices:['Michael Phelps(Natação)','Larisa Latynina(ginástica artistica)','Nikolay Andrianov(ginástica artistica)'],answer:'Michael Phelps(Natação)'}

];
// 

let indexQuestion = Math.floor(Math.random() * (questions.length - 0) + 0);
let indexChoice = Math.floor(Math.random() * (questions[indexQuestion].choices.length - 0) + 0);

function getRandomQuestion(questions){
return questions[indexQuestion]
}
function getRandomComputerChoice(choice){
return choice[indexChoice];
 /*`A: ${choice[indexQuestion].choices[0]}\nB: ${choice[indexQuestion].choices[1]}\nC: ${choice[indexQuestion].choices[2]}`*/
}
let computerChoice = getRandomComputerChoice(questions[indexQuestion].choices)
function getResults(){
if( computerChoice == questions[indexQuestion].answer){
  return "The computer's choice is correct!"
} else {
  return `The computer's choice is wrong.The correct answer is: ${questions[indexQuestion].answer}`
}

}
// console.log(questions[indexQuestion].question)
// console.log(questions[indexQuestion].choices)
// console.log(getResults())
console.log(getResults())
console.log(`Choice computer: ${computerChoice}`)
console.log(`${questions[indexQuestion].answer}`)

console.log(typeof computerChoice == questions[indexQuestion].answer)

Your browser information:

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

Challenge Information:

Build a Quiz Game - Build a Quiz Game

why are these in the global scope?

2 Likes

To access other functions and variables without having to repeat the command. What would be the most ideal way in this case?

you need to do it once in getRandomQuestion and once in getRandomComputerChoice, the difference between doing it in the global scope or in the function is: if you do it in the global scope each time you call the function you get the same thing, instead you need to get different things

1 Like