Hi
I really can’t see where I am going wrong . Can you please help me?
TIA
Your code so far
const questions = [
{
category: "Movies",
question: "Who directed alien?",
choices: ["Alfred Hitchcock","Quentin Tarentino","Ridley Scott"],
answer: "Ridley Scott"
},
{
category: "Art",
question: "Who is famous for their anonymous street art?",
choices: ["Banksy","Bach","Elizabeth Taylor"],
answer: "Banksy"
},
{
category: "Music",
question: "Who sang no scrub?",
choices: ["TLC","Destiny's child","Bewitched"],
answer: "TLC"
},
{
category: "Books",
question: "In which book is the character Touraine?",
choices: ["Wheel of Time","The Unbroken","Fire and Ice"],
answer: "The Unbroken"
},
{
category: "Games",
question: "In which game can you go straight to jail, do not pass go?",
choices: ["Mouse Trap", "The game of Life","Monopoly"],
answer: "Monopoly"
}
];
function getRandomQuestion (arr) {
let question = questions[Math.floor(Math.random()*5)+0].question
return question
};
const quiz = getRandomQuestion(questions);
const getRandomComputerChoice = (arr) =>{
let choice = questions.choices[Math.floor(Math.random()*3)+0]
return choice
};
const option = getRandomComputerChoice(choices);
const getResult = (quiz,option) =>{
const correct = questions.answer;
if (option===correct){
return `The computer's choice is correct!`;
} else {
return `The computer's choice is wrong. The correct answer is: ${correct}`;
}
}
console.log(getResult(quiz,option));
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/143.0.0.0 Safari/537.36 Edg/143.0.0.0
Can you give an overview of what you think this challenge is asking you to do?
Something like:
**questions is an array of [what type of structure?]
**getRandomQuestion takes [what parameter?] and should [do what?] and return [what?]
** when I call getRandomQuestion, I pass [what argument?] to it
**getRandomComputerChoice takes [what parameter?] and should [do what?] and return [what?]
** when I call getRandomComputerChoice, I pass [what argument from where?] to it
etc…
Create an array containing 5 questions [properties: objects]
getRandomQuestion takes the questions array as the parameter, the function should choose a random question from the questions array. It should return the question.
Calling the getRandomQuestion with the questions array as the argument.
getRandomComputerChoice takes the choices array as the parameter, the function should index the question from the question function to access the choices array under that question. It should then choose a random answer from that choices array and return the chosen answer.
Calling the getRandomComputerChoice with the getRandomQuestion question as the argument.
getResults takes the return of the getRandomComputerChoice function and checks it against the answer. If it is correct it will return the correct string, if it is wrong it will return the incorrect string with the correct answer.
I am sure it is the getRandomComputerChoice that I am getting wrong but I don’t know how to access the choices under the right question.
I can access the choices array when I log in the console (questions[].choices[]).
Good effort. Let’s look at the big picture a different way:
I’ll manually choose a random question object from the questions array at index 3 (this is similar to what getRandomQuestion should do, but the index is dynamic):
{
category: "Books",
question: "In which book is the character Touraine?",
choices: ["Wheel of Time","The Unbroken","Fire and Ice"],
answer: "The Unbroken"
}
console.log() what you are returning from getRandomQuestion. Is that a question object?
What are the properties in the question object above?
How would you get the choices array from that question object?
console.log() what you are returning from getRandomQuestion. Is that a question object?
I have now changed the code so that I am returning an object.
const getRandomQuestion = (questions) =>{
return [questions[Math.floor(Math.random()*5)].question]};
let q = getRandomQuestion(questions)
console.log(typeof q);
console.log(q);
----------------------------------------------
What are the properties in the question object above?
category, question, choices & answer.
----------------------------------------------
How would you get the choices array from that question object?
const getRandomComputerChoice = () => {
return questions.choices[Math.floor(Math.random()*3)]};
console.log(getRandomComputerChoice(questions));
you are returning an array that contains one string, you are not returning an object. typeof returns object for arrays too[1], that’s why there is Array.isArray[2]
where is the parameter for this?
an array does not have a choices property
and here you are not passing in an array of choices