Build a Quiz Game - Build a Quiz Game

 const questions = [
  { category: "Music", 
  question: "Who was the Beatles first drummer?", 
  choices: ["Ringo Starr", "Pete Best", "Robert Starkey"], 
  answer: "Pete Best" },
  { category: "Music", 
  question: "What was Motley Crue's original name?", 
  choices: ["Dark Blue", "New Years", "Xmas"], 
  answer: "Xmas" },
  { category: "Movies", 
  question: "In what city does Hogwart's reside?", 
  choices: ["London", "Switzerland", "Romania"], 
  answer: "London" },
  { category: "Movies", 
  question: "What sport, is the movie 'Draft Day' about?", 
  choices: ["Basketball", "Baseball", "Football"], 
  answer: "Football" },
  { category: "Music", question: "Who wrote and performed the soundtrack for the movie 'Flash Gordon'?", choices: ["Kiss", "Led Zeppelin", "Queen"], 
  answer: "Queen" },];

let randomQuestion;
let randomAnswer;
let computerAnswer;

function getRandomQuestion(array) {
  randomQuestion = Math.floor(Math.random()*array.length);
  console.log(randomQuestion);
  return array[randomQuestion];
  }
console.log(getRandomQuestion(questions).question);
 
function getRandomComputerChoice(answerArr) {
  randomAnswer = Math.floor(Math.random()*answerArr.length);
  computerAnswer = answerArr[randomAnswer];
  return answerArr[randomAnswer];
}
function getResults(quest, compAnswer) {
  if(quest.answer === compAnswer) {
    return "The computer's choice is correct!";
  } else {
    return `The computer's choice is wrong. The correct answer is: ${questions[randomQuestion].answer}`;
  }
}

console.log(getRandomComputerChoice(questions[randomQuestion].choices));
console.log(getResults(questions[randomQuestion], computerAnswer));

I’ve edited this for you.

For the future its it’s 3 backticks on its own line, your code, and then 3 more backticks
```
code
code
```

You need to have one topic per challenge. Otherwise people will be helping you in two different threads and it would get confusing and redundant.

Looking through your code I notice:

You declare some global variables here

let randomQuestion;
let randomAnswer;
let computerAnswer;

Do you need them? randomQuestion is used only inside getRandomQuestion so it should not be global. The same goes for all of these. Global variables might not get reset properly and might not pass the tests.

Also, you just don’t need them and they are not asked for in the instructions.

function getRandomQuestion(array) {
  randomQuestion = Math.floor(Math.random()*array.length);
  console.log(randomQuestion);
  return array[randomQuestion];
  }

randomQuestion is also not a question, it’s just a random number so it’s named confusingly. As mentioned:

The last problem is here:

    return `The computer's choice is wrong. The correct answer is: ${questions[randomQuestion].answer}`;

Are you trying to choose a new random question? Shouldn’t you use the function parameter you’ve already used earlier to determine if the computer’s answer is correct?

Generally, you will want your functions only to use the defined parameters and not access anything outside of the function. There are exceptions but generally you want to think this way about functions.

I hope this helps, please ask if you have any questions about this.

1st: I declared those variable globally because once I have a randomly selected question, I need to be able to tell the getRandomComputerChoice which array of choices to use. If I declare the variable inside of the getRandomQuestion function I wont be able to tell the getRandomComputerChoice which array to use. Correct?? Same for the randomAnswer. My getResults needs to be able to compare the questions correct question object answer with the random computer choice, How can it do that without access to those variables??

2nd: NO I am not trying to select a new question. That is simply displaying the correct answer. I tried it this way and using standard string concatination, both ways work but this way is cleaner. Im not recalling the function, the randomQuestion variable is assigned during the function call I am just using it to designate which answer in the questions array object is the correct one.

the randomQuestion variable does not exist inside getResults, you should use the parameters you have available

You only need to worry about what’s in the instructions. The instructions only specify the input and output of the functions, don’t worry about anything else.

In any case, when you’re testing you might want to store the results of your functions. You would want randomQuestion to store a question object, and getRandomQuestion returns a random question object so it would make sense to store the result of getRandomQuestion in the randomQuestion variable, correct?

You’re passing the question object to the function in the quest parameter:

function getResults(quest, compAnswer) {

And you’ve used the quest parameter to access the answer already:

if(quest.answer === compAnswer) {

So why not use it again?

There’s only one const you need to create in the global space where you call getResults() and that’s the value of the random question object, which you’ll need to pass in the value of the second parameter.