Be sure and read more than just the first line of the failing message. The ability to read and comprehend error messages is a skill you’ll need to acquire as a developer. Ask questions on what you don’t understand.
// running tests
8. You should have a function named getRandomQuestion that takes an array of questions as a parameter and returns a random question object from the array.
Does your getRandomQuestion function return a question object?
Here is the code. The console logs are at the bottom. When I call the functions the console logs are all correct.
{
category: "Sports",
question: "Whos is the current starting qb for the Cincinnati Bengals?",
choices: ["Joe Burrow", "Andy Dalton", "Josh Allen"],
answer: "Joe Burrow"
},
{
category: "Geography",
question: "What is the largest city in the world?",
choices: ["Delhi", "Shanghai", "Tokyo"],
answer: "Tokyo"
},
{
category: "Science",
question: "What component in a circuit is used to store current?",
choices: ["Resistor", "Capacitor", "Transistor"],
answer: "Capacitor"
},
{
category: "General Knowledge",
question: "What is a group of tigers called?",
choices: ["Streak", "Pride", "Murder"],
answer: "Streak"
},
{
category: "Literature",
question: "Which author wrote the popular LitRPG series Dungeon Crawler Carl?",
choices: ["Patrick Rothfuss", "Brandon Sanderson", "Matt Dinnaman"],
answer: "Matt Dinnaman"
}
];
function getRandomQuestion(questions) {
let randomQuestion = Math.floor(Math.random() * questions.length);
return questions[randomQuestion];
}
function getRandomComputerChoice(choices) {
let randomChoice = Math.floor(Math.random() * selectedQuestion.choices.length);
return choices[randomChoice];
}
function getResults() {
if (computerChoice === selectedQuestion.answer) {
return "The computer's choice is correct!";
} else if (computerChoice !== selectedQuestion.answer) {
return `The computer's choice is wrong. The correct answer is: ${selectedQuestion.answer}`;
}
}
const selectedQuestion = getRandomQuestion(questions);
const computerChoice = getRandomComputerChoice(selectedQuestion.choices);
const answer = selectedQuestion.answer;
console.log(selectedQuestion.question);
console.log(`Computer's choice: ${computerChoice}`);
console.log(getResults());
let questions = [
{
category: "Sports",
question: "Whos is the current starting qb for the Cincinnati Bengals?",
choices: ["Joe Burrow", "Andy Dalton", "Josh Allen"],
answer: "Joe Burrow"
},
{
category: "Geography",
question: "What is the largest city in the world?",
choices: ["Delhi", "Shanghai", "Tokyo"],
answer: "Tokyo"
},
{
category: "Science",
question: "What component in a circuit is used to store current?",
choices: ["Resistor", "Capacitor", "Transistor"],
answer: "Capacitor"
},
{
category: "General Knowledge",
question: "What is a group of tigers called?",
choices: ["Streak", "Pride", "Murder"],
answer: "Streak"
},
{
category: "Literature",
question: "Which author wrote the popular LitRPG series Dungeon Crawler Carl?",
choices: ["Patrick Rothfuss", "Brandon Sanderson", "Matt Dinnaman"],
answer: "Matt Dinnaman"
}
];
function getRandomQuestion(questions) {
let randomQuestion = Math.floor(Math.random() * questions.length);
return questions[randomQuestion];
}
function getRandomComputerChoice(choices) {
let randomChoice = Math.floor(Math.random() * selectedQuestion.choices.length);
return choices[randomChoice];
}
function getResults() {
if (computerChoice === selectedQuestion.answer) {
return "The computer's choice is correct!";
} else if (computerChoice !== selectedQuestion.answer) {
return `The computer's choice is wrong. The correct answer is: ${selectedQuestion.answer}`;
}
}
const selectedQuestion = getRandomQuestion(questions);
const computerChoice = getRandomComputerChoice(selectedQuestion.choices);
const answer = selectedQuestion.answer;
console.log(selectedQuestion.question);
console.log(`Computer's choice: ${computerChoice}`);
console.log(getResults());