Build a Quiz Game - Build a Quiz Game

Tell us what’s happening:

My code doesnt work properly, but passes all the test. In the console even if the answer of the computer is similar with the correct answer, it says “Choice is wrong. The correct answer is: undefined”. The best would be to check it yourself to understand what I mean. Thanks for any help!

Your code so far

const questions = [
  {
    category: "PCo",
    question: "fav color?",
    choices: ['Red', 'Blue', 'Yellow'],
    answer: 'Red'
  },
  {
    category: "Pca",
    question: "fav car?",
    choices: ['Toyota', 'Lambo', 'BMW'],
    answer: 'BMW'
  },
  {
    category: "PB",
    question: "fav brand?",
    choices: ['LV', 'KmRii', 'LGB'],
    answer: 'KmRii'
  },
  {
    category: "PF",
    question: "fav fruit?",
    choices: ['Apple', 'Grape', 'Watermelon'],
    answer: 'Watermelon'
  },
  {
    category: "PH",
    question: "fav hobby?",
    choices: ['Coding', 'Shopping', 'Hustling'],
    answer: 'Shopping'
  }
];

function getRandomQuestion(questions){
  let randomNum = Math.floor(Math.random()*questions.length)
  return questions[randomNum]
};

function getRandomComputerChoice(choices){
  let randomNum = Math.floor(Math.random()*choices.length)
  return choices[randomNum]
};

function getResults(question ,choice){
  if(question.answer == choice){
    return "The computer's choice is correct!"
  }
  else if(question.answer !== choice){
    return `The computer's choice is wrong. The correct answer is: ${question.answer}`;
  }
};


console.log(getRandomQuestion(questions));
console.log(getRandomComputerChoice([ 'Red', 'Blue', 'Yellow' ]));
console.log(getResults('fav color?', 'Red'));

Your browser information:

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

Challenge Information:

Build a Quiz Game - Build a Quiz Game

1 Like

Good candidate for a bug report!

Thank you for helping make FCC better. Bugs can be reported as GitHub Issues. Whenever reporting a bug, please check first that there isn’t already an issue for it and provide as much detail as possible.

You can explain just as you did here and include your code.

The only thing wrong with this code is how you’re passing arguments to a couple of the functions in your console.log() statements.

this is not a bug with the challenge, look at your call to getResults, look at the first argument, does it have an answer property?

The bug is that this code passes all the tests, right?

If it’s returning undefined it shouldn’t be accepted as passing all the tests? It might meet all the requirements technically but then the tests or requirements might need to be updated.

there are no requirements about calling the function, only writing the function correctly, and the function is written correctly

undefined comes from the function being called with a non-appropriate argument

1 Like

Of course, I see thanks

No, but my getResults function has (question, choice), where should i put the answer when calling it?

you need to call the function with an object as first argument that has an answer property, like the question objects you have written previously

to be honest i cannot get it at all…
my get results function has (question, choice), I am writing (question, choice) as it asks me. I cannot get what you are talking about..

you also write question.answer, so question can’t be a string when you call the function, also the instructions tell you that it should take a question object as first argument

Im sorry but i really cant get it. Is it in getResults function “if” property? if yes, i cant get what should i change to make it work.

Can you say what you can you do to get a question object?

the function itself is fine

your issue is where you are calling the function

can you point out which is the line that os calling the getResults function?

console.log(getResults('fav color?', 'Red'));

this is the one calling function. Am i correct?

that’s correct, and do you see that your first argument is a string?

my first argument is a question as I see it, and I am putting down the question, as it asks me??

It does not ask for a question, it asks for a question object

You should have a function named getResults that takes the question object as the first parameter

a question object is, for example, one of the objects inside your questions array

okay i think i got what you mean, is it the right one? I tried to randomise it? is it what is asked for?

let randomNum = Math.floor(Math.random() * questions.length)
console.log(getResults(questions[randomNum], 'Red'));

Why aren’t you using the functions you created to get the values you need to pass into getResults? First you need to get a random question object…and then? Remember, getResults should be looking at a single question object.