Build a Quiz Game - Build a Quiz Game

Tell us what’s happening:

My element 9, 11, 13 and 14 are not working properly. I am struggling getting only the choices array as a parameter for my getRandomComputerChoice function since I haven’t segregated it from it’s original object array, and the exercise do not ask me to. I’m stuck =(

Your code so far

const questions = [
  {
    category: "Basics",
    question: "What does CPU stand for?",
    choices: [
      "A) Central Processing Unit",
      "B) Computer Processing Unit",
      "C) Control Processing User",
    ],
    answer: "A) Central Processing Unit",
  },
  {
    category: "Programming Logic",
    question: "What is the output of 2 + 2?",
    choices: ["A) 3", "B) 4", "C) 5"],
    answer: "B) 4",
  },
  {
    category: "Web",
    question: "Which of these is a web browser?",
    choices: ["A) Chrome", "B) Linux", "C) Python"],
    answer: "A) Chrome",
  },
  {
    category: "Data",
    question: "Which one is a database?",
    choices: ["A) React", "B) Git", "C) MySQL"],
    answer: "C) MySQL",
  },
  {
    category: "Git",
    question: "What does git commit do?",
    choices: [
      "A) Downloads code from the internet",
      "B) Saves a snapshot of changes to the repository",
      "C) Deletes the current branch",
    ],
    answer: "B) Saves a snapshot of changes to the repository",
  },
];

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

function getRandomComputerChoice(choicesArr) {
  const getChoices = getRandomQuestion(questions);
  const randomIndex = Math.floor(Math.random() * getChoices.choices.length);
  return getChoices.choices[randomIndex];
}

function getResults(question, cpuChoice) {
  if (question.choices[cpuChoice] === question.choices[cpuChoice]) {
    return "The computer's choice is correct!";
  } else {
    return `The computer's choice is wrong. The correct answer is: ${questions[cpuChoice].answer}!`;
  }
}

here it looks you have a choicesArr but I don’t see you using it

you should review what getResul is being passed, also here you are comparing a thing with itself so it’s never going to be false

I’m still struggling to get (and reuse) the selected question from the function “getRandomQuestion” since it is returning in another function…

review what it takes as parameters

You should have a function named getResults that takes the question object as the first parameter and the computer’s choice as the second parameter.

then consider if you need to use other functions inside it

1 Like

gotcha. thank you so much