Not sure what I’m doing wrong. There is obviously something wrong with my last function, but i’m not sure what i need to do to fix it.
Your code so far
const questions = [
{
category: "Doctor Who",
question: "What is the name of the highly versatile handheld tool frequently used by the Doctor?",
choices: ["sonic hairdryer", "sonic screwdriver", "sonic dildo"],
answer: "sonic screwdriver"
},
{
category: "Doctor Who",
question: "What is the name of the police box which serves as the Doctor’s time machine?",
choices: ["TARDIS", "DeLorean", "portable toilet"],
answer: "TARDIS"
},
{
category: "Doctor Who",
question: "What did the 5th Doctor wear on his lapel?",
choices: ["cucumber", "celery", "shallots"],
answer: "celery"
},
{
category: "Doctor Who",
question: "What is the name of the dog-like robot in Doctor Who?",
choices: ["K-9", "Max", "Klaus"],
answer: "K-9"
},
{
category: "Doctor Who",
question: "What is the name of the Doctor’s home planet?",
choices: ["Earth", "Raxacoricofallapatorius", "Gallifrey"],
answer: "Gallifrey"
}
];
let selectedQuestion = [];
let possibleChoices = [];
let selectedAnswer = "";
let correctAnswer = ""
function getRandomQuestion(questions) {
const randomIndex = Math.floor(Math.random() * questions.length);
const randomQuestion = questions[randomIndex];
selectedQuestion = randomQuestion;
possibleChoices = randomQuestion.choices;
correctAnswer = randomQuestion.answer;
return randomQuestion;
}
function getRandomComputerChoice(possibleChoices) {
const randomIndex = Math.floor(Math.random() * possibleChoices.length);
const randomAnswer = possibleChoices[randomIndex];
selectedAnswer = randomAnswer;
return randomAnswer;
}
function getResults(arr, choices) {
if (selectedAnswer === correctAnswer) {
return "The computer's choice is correct!"
} else {return "The computer's choice is wrong. The correct answer is: " + correctAnswer}
}
console.log(getResults(questions, selectedAnswer))
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/139.0.0.0 Safari/537.36 Edg/139.0.0.0
There are no errors - the console just lists the 3 or 4 incomplete tests. I suppose i can just go through them here one at a time.
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.
I believe i have done this correctly. When i log the result to the console, it returns a random answer to the selected question, as requested.
Your getResults function should take the question object as the first parameter and the computer’s choice as the second parameter.
If the computer choice matches the answer, getResults should return The computer’s choice is correct!
If the computer choice doesn’t match the answer, getResults should return The computer’s choice is wrong. The correct answer is: , where is the value of the correct answer to the chosen question.
These 3 all relate to this function. I definitely have no idea why #12 & #13 are failing.
function getResults(one, two) {
if (selectedAnswer === correctAnswer) {
return "The computer's choice is correct!"
} else {return "The computer's choice is wrong. The correct answer is: " + correctAnswer}
}
{ category: 'Doctor Who',
question: 'What is the name of the police box which serves as the Doctor’s time machine?',
choices: [ 'TARDIS', 'DeLorean', 'portable toilet' ],
answer: 'TARDIS' }
computer answer: TARDIS
The computer's choice is correct!
My console.log’s appear to be returning the correct information.
I can only guess what the issue is - maybe it’s something to do with the parameters in my functions? but i’m really not sure.
i declared some global variables to store all the information. That was on the only way i could think of that I could use data from one function in another function.
let selectedQuestion = [];
let possibleChoices = [];
let selectedAnswer = "";
let correctAnswer = "";
and i’ve read test #11 a hundred times - i’m really not sure what else i can do
11. Your getResults function should take the question object as the first parameter and the computer’s choice as the second parameter.
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. The function should return The computer's choice is correct! if the answer is correct. Otherwise, it returns The computer's choice is wrong. The correct answer is: <correct-answer>, where <correct-answer> is the value of the correct answer to the chosen question.
function getResults(arr, choices) {
if (selectedAnswer === correctAnswer) {
return "The computer's choice is correct!"
} else {return "The computer's choice is wrong. The correct answer is: " + correctAnswer}
}
{ category: 'Doctor Who',
question: 'What is the name of the highly versatile handheld tool frequently used by the Doctor?',
choices: [ 'sonic hairdryer', 'sonic screwdriver', 'sonic dildo' ],
answer: 'sonic screwdriver' }
The computer's choice is wrong. The correct answer is: sonic screwdriver