Tell us what’s happening:
Help me to understand how I can calling randomQuestion into compChoice. Because my code on compChoice wont always match with the randomQuestion.
The same problem happen on getResult code too because I think I address RandomQuestion incorrectly. So it log undefined.
And I confused on setting the paramaters. Thanks!
Your code so far
const questions = [
{
category: "name",
question: "What's your name?",
choices: ["Davy", "Jones", "Rocks"],
answer: "Davy",
},
{
category: "age",
question: "What's your age?",
choices: ["22","33","44"],
answer: "44",
},
{
category: "color",
question: "What's your favorit color?",
choices: ["blue", "black", "red"],
answer: "blue",
},
{
category: "drink",
question: "What's your favorit drink?",
choices: ["juice", "coke", "water"],
answer: "water",
},
{
category: "music",
question: "What's your favorit music genre?",
choices: ["pop", "metal", "country"],
answer: "metal",
}
];
//console.log(questions[2].choices[2]);
function getRandomQuestion(question) {
let questionIndex = Math.floor(Math.random() * questions.length);
let randomQuestion = questions[questionIndex];
return randomQuestion;
};
console.log(getRandomQuestion());
function getRandomComputerChoice(choices) {
let choiceIndex = Math.floor(Math.random() * [choices].length);
let randomChoice = getRandomQuestion().choices[choiceIndex];
return randomChoice
};
console.log(getRandomComputerChoice());
function getResults(question,choices) {
let correctAnswer = getRandomQuestion().answer;
if (getRandomComputerChoice() === getRandomQuestion().answer) {
return "The computer's choice is correct!";
} else "The computer's choice is wrong. The correct answer is: " + correctAnswer;
}
console.log(getResults());
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/141.0.0.0 Safari/537.36
Challenge Information:
Build a Quiz Game - Build a Quiz Game