const questions = [
{
category: "Science",
question: "What is the chemical symbol for water?",
choices: ["H2O", "O2", "CO2"],
answer: "H2O"
},
{
category: "Geography",
question: "Which is the largest continent?",
choices: ["Asia", "Africa", "Europe"],
answer: "Asia"
},
{
category: "History",
question: "Who was the first President of the United States?",
choices: ["George Washington", "Abraham Lincoln", "Thomas Jefferson"],
answer: "George Washington"
},
{
category: "Technology",
question: "What does 'HTTP' stand for?",
choices: ["HyperText Transfer Protocol", "Human Transfer Technology", "HyperText Transfer Procedure"],
answer: "HyperText Transfer Protocol"
},
{
category: "Literature",
question: "Who wrote 'Romeo and Juliet'?",
choices: ["William Shakespeare", "Charles Dickens", "Jane Austen"],
answer: "William Shakespeare"
}
];
let randomIndex = Math.floor(Math.random()*questions.length);
let randomChoice = Math.floor(Math.random()*questions[randomIndex].choices.length);
function getRandomQuestion(array){
if(array.length === 0){
return null;
}
return questions[randomIndex].question;
}
function getRandomComputerChoice(array){
return array[randomIndex].choices[randomChoice];
}
const answer = questions[randomIndex].answer;
const computerChoice = getRandomComputerChoice(questions);
function getResults(answer,computerChoice){
if(answer == computerChoice){
return `The computer's choice is correct!`;
}else{
return `The computer's choice is wrong. The correct answer is: ${answer}`;
}
}
console.log(getRandomQuestion(questions));
console.log(getRandomComputerChoice(questions));
console.log(getResults(answer,computerChoice));
I see a small comprehension mistake, make sure to read again what is given to getResults
as argument