Build a Quiz Game - Build a Quiz Game

Tell us what’s happening:

I can’t pass the test 11, 12, 13, and 14. While my code is producing what appears to be the correct output, the test is not accepting it. In addition, my code for the getRandomComputerChoice() is similar with getRandomQuestion(), and I have a feeling that it may have something to do with my problem. May I kindly ask for your advise please? TIA!

Your code so far

const questions = [
  {
    category: "Science",
    question: "What is the primary function of hemoglobin in the human body?",
    choices: [
      "A) To fight infections",
      "B) To transport oxygen in the blood",
      "C) To aid in digestion"
    ],
    answer: "B) To transport oxygen in the blood"
  },
  {
    category: "History",
    question: "Which ancient wonder of the world was located in present-day Iraq and was built around 600 BCE?",
    choices: [
      "A) The Great Pyramid of Giza",
      "B) The Hanging Gardens of Babylon",
      "C) The Colossus of Rhodes"
    ],
    answer: "B) The Hanging Gardens of Babylon"
  },
  {
    category: "Geography",
    question: "Which African country is the only one that borders both the Atlantic Ocean and the Mediterranean Sea?",
    choices: [
      "A) Nigeria",
      "B) Egypt",
      "C) Morocco"
    ],
    answer: "C) Morocco"
  },
  {
    category: "Mathematics",
    question: "If x = 5, what is the value of 3x^2 - 2x + 4?",
    choices: [
      "A) 59",
      "B) 69",
      "C) 79"
    ],
    answer: "B) 69"
  },
  {
    category: "Literature",
    question: "In George Orwell’s “1984,” what is the name of the all-powerful ruling party?",
    choices: [
      "A) The Ministry of Truth",
      "B) Big Brother",
      "C) Ingsoc"
    ],
    answer: "C) Ingsoc"
  }
];

function getRandomQuestion(arr) {
  const random = Math.floor(Math.random() * (arr.length - 0) + 0);
  return arr[random];
}

function getRandomComputerChoice(arr) {
  const random = Math.floor(Math.random() * (arr.length - 0) + 0);
  return arr[random];
}

function getResults(question, answer) {
  const randomQuestion = getRandomQuestion(question);
  answer = getRandomComputerChoice(randomQuestion.choices);

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

console.log(getResults(questions));

Your browser information:

User Agent is: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/149.0.0.0 Safari/537.36

Challenge Information:

Build a Quiz Game - Build a Quiz Game

GitHub Link: freeCodeCamp/curriculum/challenges/english/blocks/lab-quiz-game/66f17db06803d11a1bd19a20.md at main · freeCodeCamp/freeCodeCamp · GitHub

  1. Your getResults function should take the question object as the first parameter and the computer’s choice as the second parameter.

If I check this console log:

function getResults(question, answer) {
  console.log(question, "****", answer)

It seems to output a lot more than just 1 question object.

console.log(getResults(questions));

Does this console log at the end of your code make sense?

getResults takes 2 arguments. The first argument should be a (1 single) question object, not an array of question objects, right?

Thank you for pointing out the output of my getResult() function, such a great hint! I am able now to pass the tests.