Build a Quiz Game - Build a Quiz Game

Tell us what’s happening:

I cannot pass the ‘11. Your getResult function should take the question object as the first parameter and the computer’s choice as the second parameter.’ and ‘12. If the computer choice matches the answer. The getResults should return “The computer’s choice is correct!”’. Please help.

Your code so far

const questions = [
  {
    category: "Culinary Arts",
    question: "Which of these ingredients is essential for making a traditional Filipino 'caramel' or 'yema' icing?",
    choices: ["Whipping Cream", "Condensed Milk", "Cream Cheese"],
    answer: "Condensed Milk"
  },
  {
    category: "Computing",
    question: "Which of these is popular Linux desktop environment known for its high level of customization and modern features?",
    choices: ["KDE Plasma", "DirectX", "NTFS"],
    answer: "KDE Plasma"
  },
  {
    category: "Web Development",
    question: "In CSS, which layout module is designed for creating complex two-dimensional layouts using rows and columns?",
    choices: ["Flexbox", "Float","Grid"],
    answer: "Grid"
  },
  {
    category: "Space and Astronomy",
    question: "Which is the largest planet in our solar system?",
    choices: ["Saturn", "Jupiter", "Neptune"],
    answer: "Jupiter"
  },
  {
    category: "Pop Culture",
    question: "In the film The Matrix, what color pill does Neo take to see  the truth?",
    choices: ["Blue", "Green", "Red"],
    answer: "Red"
  }
]

const getRandomQuestion = (questions) => {
  const newQuestion = Math.floor(Math.random() * questions.length)
  return questions[newQuestion]
}
const randomQuestion = getRandomQuestion(questions)

const choices = randomQuestion.choices


const getRandomComputerChoice = (choices) => {
  const randomChoice = Math.floor(Math.random() * choices.length)
  return choices[randomChoice]
}

const computerChoice = getRandomComputerChoice(choices)

const correctAnswer = randomQuestion.answer

const getResults = (randomQuestion, computerChoice) => {

  if (randomQuestion !== computerChoice) {
    return `The computer's choice is wrong. The correct answer is: ${randomQuestion.answer}`
 
  } else {
    return `The computer's choice is correct!`
  }
  
}
console.log(randomQuestion)
console.log(computerChoice)
console.log(getResults(randomQuestion, computerChoice))

Your browser information:

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

Challenge Information:

Build a Quiz Game - Build a Quiz Game

1 Like

Hi @palenciajp22,

What type of data structure is randomQuestion? (Look at your logs)

Can you expect randomQuestion to ever equal computerChoice?

Happy coding!

1 Like

Pardon. What do you mean data structure? is it not ‘object’?

I expect randomQuestion to not equal computerchoice. When I do change it to “===” . All of this “Failed:11. Your getResults function should take the question object as the first parameter and the computer’s choice as the second parameter.

  • Failed:12. If the computer choice matches the answer, getResults should return The computer's choice is correct!

  • Passed:13. If the computer choice doesn’t match the answer, getResults should return The computer's choice is wrong. The correct answer is: <correct-answer>, where <correct-answer> is the value of the correct answer to the chosen question.

  • Passed:14. Your getResults function should use exact equality comparison, not substring matching.” is wrong. But when I do “!==” only 11 and12 were wrong.

Yes, randomQuestion is an object. Your comparison operator is correct, but will a question object ever be equal to a question choice? What should you compare instead?

You’re very close, the issue is just in your comparison logic.

In your getResults function, you are comparing the entire randomQuestion object with computerChoice, which will always be different. Instead, you should compare the correct answer inside the question object with the computer’s choice.

Try updating your function like this:

You’re very close, the issue is just in your comparison logic.

In your getResults function, you are comparing the entire randomQuestion object with computerChoice, which will always be different. Instead, you should compare the correct answer inside the question object with the computer’s choice.

Try updating your function like this:

The key point is:
Compare computerChoice with question.answer, not the whole object

Once you fix that, both tests 11 and 12 should pass .
I hope it helps