Build a Quiz Game - Build a Quiz Game

Tell us what’s happening:

I’ve tried everything I can think of, switched the variables around 100 times and I just can’t see where I’m going wrong. Please give me some guidance on this. Thanks

Your code so far

const questions = [
  {
    category: "desserts",
    question: "Favorite dessert?",
    choices: ['Ice Cream', 'Cake', 'Brownies'],
    answer: 'Brownies'
  },
  {
    category: "cars",
    question: "Favorite car?",
    choices: ['Tesla', 'Mercedes', 'Infiniti'],
    answer: 'Mercedes'
  },
  {
    category: "socials",
    question: "Favorite social media?",
    choices: ['Facebook', 'Instagram', 'X'],
    answer: 'Facebook'
  },
  {
    category: "veggies",
    question: "Favorite vegetable?",
    choices: ['Asparagus', 'Broccolli', 'Potatoes'],
    answer: 'Asparagus'
  },
  {
    category: "codeLangs",
    question: "Favorite Coding Language?",
    choices: ['Python', 'JavaScript', 'C++'],
    answer: 'Python'
  }
]
const getRandomQuestion = (questions) =>{
  let randomQuestion = Math.floor(Math.random() * 5);
  return questions[randomQuestion];

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

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

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/141.0.0.0 Safari/537.36

Challenge Information:

Build a Quiz Game - Build a Quiz Game

let’s look at getResults, if the first parameter is one of the objects in the array, and the second parameter a string, how do you check is the answer in the object?

1 Like

Thanks for the feedback. I’ve decided to reset the lesson and start over.

That’s a shame. Your code looked good except for not understanding how to access the answer property in your array of question objects.

Tell us what’s happening:

I can’t figure out why just changing the operator in step 13 makes steps 11 and 12 pass, but not 13. If I use the < operator, 11 and 12 pass. But not if I use the !== operator. I’m making a mess here. Please give me advice.

Your code so far

const questions = [
  {
    category: "desserts",
    question: "Favorite dessert?",
    choices: ['Ice Cream', 'Cake', 'Brownies'],
    answer: 'Brownies'
  },
  {
    category: "cars",
    question: "Favorite car?",
    choices: ['Tesla', 'Mercedes', 'Infiniti'],
    answer: 'Mercedes'
  },
  {
    category: "socials",
    question: "Favorite social media?",
    choices: ['Facebook', 'Instagram', 'X'],
    answer: 'Facebook'
  },
  {
    category: "veggies",
    question: "Favorite vegetable?",
    choices: ['Asparagus', 'Broccolli', 'Potatoes'],
    answer: 'Asparagus'
  },
  {
    category: "codeLangs",
    question: "Favorite Coding Language?",
    choices: ['Python', 'JavaScript', 'C++'],
    answer: 'Python'
  }
]
function getRandomQuestion(questions) {
  let randomQuestion = Math.floor(Math.random() * questions.length);
  return questions[randomQuestion];
}

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

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

}
console.log(questions);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/141.0.0.0 Safari/537.36

Challenge Information:

Build a Quiz Game - Build a Quiz Game

Have you tried to play around a bit with the functions to see what is returned? For example you can add at the bottom of the code:

const question = getRandomQuestion(questions);
const computerChoice = getRandomComputerChoice(question.choices);
console.log(getResults(question, computerChoice))

I’ve been trying something like that by defining some global variables. I’ll keep trying with your idea in mind. Thank you.

Why did you add this if condition? I mean, if the answer does not satisfy your if condition, wouldn’t it then be the wrong answer?

Also, where have you declared an answer variable in this code? Is there something else you could use here that you have already used?

I was just experimenting to see what effect it would have. It confused me even more.

I have merged your two topics, do not create multiple topics for the same challenge

1 Like