Build a Quiz Game - Build a Quiz Game

Tell us what’s happening:

I keep failing tests 11, 12, 13, and 14.

I seem to be getting the appropriate responses for 12 and 13, so I’m assuming that there’s something wrong with the parameters I’m passing.

I’m also unsure of the problem with 14; I’ve tried both === and != in my comparisons.

(My questions are repeated, using numbers to help distinguish which one is being used and to ensure that the answer from one couldn’t be used in place for a different question.)

I may be misunderstanding the concept of the ‘object’ entirely, because I think I am passing the entire question object to getResults… unless I’m supposed to pass the entire questions array to that function?

Your code so far

function getRandomInt(max) {
    return Math.floor(Math.random() * max);
    } 

const questions = [
  {
  category: "traffic",
  question: "Do you stop at red lights? 1",
  choices: ["yes1", "no1", "maybe1"],
  answer: "maybe1"
  },
  {
  category: "traffic",
  question: "Do you stop at red lights? 2",
  choices: ["yes2", "no2", "maybe2"],
  answer: "maybe2"
  },
  {
  category: "traffic",
  question: "Do you stop at red lights? 3",
  choices: ["yes3", "no3", "maybe3"],
  answer: "maybe3"
  },
  {
  category: "traffic",
  question: "Do you stop at red lights? 4",
  choices: ["yes4", "no4", "maybe4"],
  answer: "maybe4"
  },
  {
  category: "traffic",
  question: "Do you stop at red lights? 5",
  choices: ["yes5", "no5", "maybe5"],
  answer: "maybe5"
  }  
];

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

let selectedQuestion = getRandomQuestion(questions)
let availableChoices = selectedQuestion.choices

console.log(selectedQuestion)
console.log(selectedQuestion.question)
console.log(availableChoices)

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

let computerChoice = getRandomComputerChoice(availableChoices)

let correctAnswer = selectedQuestion.answer

console.log(correctAnswer)

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

getResults(selectedQuestion, computerChoice); 


Your browser information:

User Agent is: Mozilla/5.0 (X11; Linux x86_64; rv:152.0) Gecko/20100101 Firefox/152.0

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

Welcome to the forum @jmehmel,

When you return console.log(), your function prints to the console, but returns undefined. So, if you want to see what your function returns, wrap your function call in console.log().

Also, you are using the global variable, correctAnswer, inside getResults rather than getting the answer from the question object passed to the function as a parameter, like you did for the strict equality comparison (=== or !==).

Happy coding

Aha. Those two bits helped me solve this.

My lesson, alongside the ones this lab was meant to offer, is that I need to be careful with my variable names and to track local-to-the-function versus global.

I think I kept expecting the problem to be elsewhere! Reminder to self: always check your variable names!

(And make sure you’re returning a thing and not just printing a thing… not sure if that’s giving away the solution!)