Tell us what’s happening:
I have copied this code to try and help me understand it a bit more, but I am still confused, can someone please explain what this code does, thanks.
function getResults(questionObject, computerChoice) {
if(computerChoice === questionObject.answer) {
return “The computer’s choice is correct!”
} else {
return The computer's choice is wrong. The correct answer is: ${questionObject.answer}
}
};
Your code so far
const questions = [
{
category: "category1",
question: "question1?",
choices: ["choices11", "choices12", "answer1"],
answer: "answer1",
},
{
category: "category2",
question: "question2?",
choices: ["choices21", "choices22", "answer2"],
answer: "answer2",
},
{
category: "category3",
question: "question3?",
choices: ["choices31", "choices32", "answer3"],
answer: "answer3",
},
{
category: "category4",
question: "question4?",
choices: ["choices41", "choices42", "answer4"],
answer: "answer4",
},
{
category: "category5",
question: "question5?",
choices: ["choices51", "choices52", "answer5"],
answer: "answer5",
}
];
function getRandomQuestion (questions) {
let randomNum = Math.floor(Math.random() * questions.length);
return questions[randomNum];
}
function getRandomComputerChoice (choices) {
let randomChoice = Math.floor(Math.random() * choices.length);
return choices[randomChoice];
}
function getResults(questionObject, computerChoice) {
if(computerChoice === questionObject.answer) {
return "The computer's choice is correct!"
} else {
return `The computer's choice is wrong. The correct answer is: ${questionObject.answer}`
}
};
let output1 = getRandomQuestion());
let output2 = getRandomComputerChoice(choices);
let output3 = getResults(questionObject, computerChoice);
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/137.0.0.0 Safari/537.36
Challenge Information:
Build a Quiz Game - Build a Quiz Game