Build a Quiz Game - Build a Quiz Game

Tell us what’s happening:

failing steps 8, 9, 10, 11 but the console output seems to work correctly. I have to be missing something

Your code so far

let questions = [];

const question1 = {
  category: "Sports",
  question: "Whos is the current starting qb for the Cincinnati Bengals?",
  choices: ["Joe Burrow", "Andy Dalton", "Josh Allen"],
  answer: "Joe Burrow"
}

const question2 = {
  category: "Geography",
  question: "What is the largest city in the world?",
  choices: ["Delhi", "Shanghai", "Tokyo"],
  answer: "Tokyo"
}

const question3 = {
  category: "Science",
  question: "What component in a circuit is used to store current?",
  choices: ["Resistor", "Capacitor", "Transistor"],
  answer: "Capacitor"
}

const question4 = {
  category: "General Knowledge",
  question: "What is a group of tigers called?",
  choices: ["Streak", "Pride", "Murder"],
  answer: "Streak"
}

const question5 = {
  category: "Literature",
  question: "Which author wrote the popular LitRPG series Dungeon Crawler Carl?",
  choices: ["Patrick Rothfuss", "Brandon Sanderson", "Matt Dinnaman"],
  answer: "Matt Dinnaman"
}

questions.push(question1, question2, question3, question4, question5);

let randomQ = Math.floor(Math.random() * questions.length);

function getRandomQuestion(questions) {
  let randomQuestion = randomQ;
  return questions[randomQuestion].question; 
}

let randomC = Math.floor(Math.random() * questions[randomQ].choices.length);

function getRandomComputerChoice(choices) {
  choices = questions[randomQ].choices
  let randomChoice = randomC
  return choices[randomChoice];
}

let computerChoice = getRandomComputerChoice(questions[randomQ].choices[randomC]);

let userAnswer = questions[randomQ].answer;

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


console.log(getRandomQuestion(questions));
console.log(getRandomComputerChoice(questions[randomQ].choices));
console.log(getResults());
console.log(computerChoice === userAnswer)
console.log(computerChoice)
console.log(userAnswer)











Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:136.0) Gecko/20100101 Firefox/136.0

Challenge Information:

Build a Quiz Game - Build a Quiz Game

Be sure and read more than just the first line of the failing message. The ability to read and comprehend error messages is a skill you’ll need to acquire as a developer. Ask questions on what you don’t understand.

// running tests
8. You should have a function named getRandomQuestion that takes an array of questions as a parameter and returns a random question object from the array.

Does your getRandomQuestion function return a question object?

No it’s returning a string.

you will need to update that

Ok after changing things I’ve gotten it to pass all the steps except 11 and 12. The console shows the correct output.

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

const selectedQuestion = getRandomQuestion(questions);
const computerChoice = getRandomComputerChoice(selectedQuestion.choices);
const userAnswer = selectedQuestion.answer;
console.log(selectedQuestion.question);
console.log(`Computer's choice: ${computerChoice}`)
console.log(getResults());

It doesn’t work, it works on return To Start.

I’m not sure what you mean.

Focus on Test 11, one thing at a time.

What is the feedback / hints in the console? The feedback for test 8 contained all you needed to know to fix it.

// running tests
11. If the computer choice matches the answer,
getResults should return The computer’s choice is correct!

This is all it says

Have you tested that? Are you able to show that it works?

I have and it returns the correct statement

How did you test it?

If you had to prove to me it was working, what could you show?

What feedback do those console.log statements give you?

Also, if I copy paste your latest code it still fails test 8. Can you post the full and most recent update?

Here is the code. The console logs are at the bottom. When I call the functions the console logs are all correct.

  {
  category: "Sports",
  question: "Whos is the current starting qb for the Cincinnati Bengals?",
  choices: ["Joe Burrow", "Andy Dalton", "Josh Allen"],
  answer: "Joe Burrow"
},

{
  category: "Geography",
  question: "What is the largest city in the world?",
  choices: ["Delhi", "Shanghai", "Tokyo"],
  answer: "Tokyo"
},

{
  category: "Science",
  question: "What component in a circuit is used to store current?",
  choices: ["Resistor", "Capacitor", "Transistor"],
  answer: "Capacitor"
},

{
  category: "General Knowledge",
  question: "What is a group of tigers called?",
  choices: ["Streak", "Pride", "Murder"],
  answer: "Streak"
},

{
  category: "Literature",
  question: "Which author wrote the popular LitRPG series Dungeon Crawler Carl?",
  choices: ["Patrick Rothfuss", "Brandon Sanderson", "Matt Dinnaman"],
  answer: "Matt Dinnaman"
}
];

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

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

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

const selectedQuestion = getRandomQuestion(questions);
const computerChoice = getRandomComputerChoice(selectedQuestion.choices);
const answer = selectedQuestion.answer;
console.log(selectedQuestion.question);
console.log(`Computer's choice: ${computerChoice}`);
console.log(getResults());
let questions = [
  {
  category: "Sports",
  question: "Whos is the current starting qb for the Cincinnati Bengals?",
  choices: ["Joe Burrow", "Andy Dalton", "Josh Allen"],
  answer: "Joe Burrow"
},

{
  category: "Geography",
  question: "What is the largest city in the world?",
  choices: ["Delhi", "Shanghai", "Tokyo"],
  answer: "Tokyo"
},

{
  category: "Science",
  question: "What component in a circuit is used to store current?",
  choices: ["Resistor", "Capacitor", "Transistor"],
  answer: "Capacitor"
},

{
  category: "General Knowledge",
  question: "What is a group of tigers called?",
  choices: ["Streak", "Pride", "Murder"],
  answer: "Streak"
},

{
  category: "Literature",
  question: "Which author wrote the popular LitRPG series Dungeon Crawler Carl?",
  choices: ["Patrick Rothfuss", "Brandon Sanderson", "Matt Dinnaman"],
  answer: "Matt Dinnaman"
}
];

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

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

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

const selectedQuestion = getRandomQuestion(questions);
const computerChoice = getRandomComputerChoice(selectedQuestion.choices);
const answer = selectedQuestion.answer;
console.log(selectedQuestion.question);
console.log(`Computer's choice: ${computerChoice}`);
console.log(getResults());

To “prove it” You would need to show the result of the console log.

With your previous code I was getting this result:

undefined
Computer's choice: Andy Dalton
The computer's choice is wrong. The correct answer is: undefined

I think the problem is here:

You should have a function named getResults that takes the selected question object and the computer choice as its parameters

Yep that was the problem. Thanks a lot for your help

1 Like