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}!`;
}
}