Tell us what’s happening:
I can’t pass test 9. “You should have a function named getRandomComputerChoice that takes the array of the available choices as a parameter, and returns a random answer to the selected question.” It doesn’t like the parameters for the array, when I try questionObject.choices it gives me a syntax error. Other options don’t show an error, but don’t pass the test. Code works though.
Your code so far
const questions = [
{
category: "ABCs",
question: "What is the 1st number of the English alphabet?",
choices: ["B", "A", "C"],
answer: "A",
},
{
category: "ABCs",
question: "What letter comes after J?",
choices: ["H", "I", "K"],
answer: "K",
},
{
category: "123s",
question: "What is 1 + 2?",
choices: ["3", "5", "8"],
answer: "3",
},
{
category: "123s",
question: "What is 2 + 2?",
choices: ["2", "4", "10"],
answer: "4",
},
{
category: "123s",
question: "What is 5 + 2?",
choices: ["1", "6", "7"],
answer: "7",
}
]
function getRandomQuestion(questions) {
let randomNum= Math.floor(Math.random()*questions.length);
return questions[randomNum];
}
let questionObject = getRandomQuestion(questions);
let choices = questionObject.choices;
function getRandomComputerChoice(choices) {
let randomNum1= Math.floor(Math.random()*3);
return questionObject.choices[randomNum1];
}
let computerChoice = getRandomComputerChoice(questionObject.choices);
function getResults(questionObject, computerChoice) {
if (computerChoice == questionObject.answer) {
return "The computer's choice is correct!"
}
else {
return `The computer's choice is wrong. The correct answer is: ${questionObject.answer}`
}
}
let result = getResults(questionObject, computerChoice);
console.log(result)
console.log(questionObject.question + "\n" + computerChoice + "\n" + result);
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/140.0.0.0 Safari/537.36
Challenge Information:
Build a Quiz Game - Build a Quiz Game