Build a Quiz Game - Build a Quiz Game

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

Hi @halfihalfian182 and welcome to the forum.

getResults() takes two arguments, which you need to pass to the function by using getRandomQuestion() and getRandomComputerChoice(). (HINT: You can store the random question in a variable and use the variable to get the question’s choices)

Is that the correct parameter? Where are you using question in your function?

Are you seeing an error in the console? Is that the correct way to reference choices?

Shouldn’t your random choice come directly from the choices argument passed to the function?

with the parameter (question), the log is giving me the random question i want. parameter (questions) log me undefined. But I know there must be mistake with the parameter. I’ll redo all the parameter.

Could you give me a hint for the next 2 parameter, please.

First function is (questions), right?

Second function is (choices)? i guess.

Third (question, getRandomComputerChoice) ?

I wouldn’t use the name of a function as the second parameter. Maybe just computerChoice?

You know, I’ve heard that giving variables meaningful names is one of the hardest things we learn to do as developers? :laughing:

I used (question,choice) as the third parameter. Yet I still didnt find the answer I want.

Then I try to change how I call the console.log with the argument/s that match the parameter. Finally it all clicked.

I need to learn and understand more about parameter and argument so I wont be confused by any instructions given. THANKS!

I tweak my argument/s around these three things:

let randomQ = getRandomQuestion(questions);

let computerChoice = getRandomComputerChoice(randomQ.choices);

let result = getResults(randomQ, computerChoice);

and some adjustment in the third function.

WOW it’s frustrating but FUN!

1 Like

Nicely done!

Maybe it will help you to think of parameters as placeholders in a function definition. So, if I define a function like this:

function getProduct(id) {}

id doesn’t have a value yet, so I have to pass its value in when I call the function, like this:

getProduct(12)

Now when getProduct() runs, the value of id is 12.

Thanks for your explanation. It’s actually straight forward, but I sometimes caught up with instructions and going circle making myself more confused :laughing: