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.
Your call to getRandomComputerChoice() is not correct. Wouldn’t you need to get the question object first so you can get that specific question’s choices to pass to getRandomComputerChoice()? What variables could you create then to use in your call to getResults()?
Well, 'm continuing to work on this project. For now, what I’ve managed to do, and this is amazing, is understand what I’m doing.
But I don’t understand :
step 7: The value of answer should be included in the choices array.
My code is the next:
const questions = [
{
category : "Food",
question : "What fruit do you prefer?",
choices : ["tomatoes", "bananas","apples"],
answer : "bananas"
},
{
category : "Food",
question : "Do you like tomatoes?",
choices : ["Yes", "No", "It's irrelevant."],
answer : "No"
},
{
category : "Clothes",
question : "Do you like dresses?",
choices : ["Yes", "No", "It's irrelevant."],
answer : "It's irrelevant"
},
{
category : "Travel",
question : "Do you like travel?",
choices : ["Yes", "No", "It's irrelevant."],
answer : "No"
},
{
category : "Games",
question : "Do you like travel?",
choices : ["Yes", "No", "It's irrelevant."],
answer : "Yes"
}
];
// Access to the properties of an object
console.log(questions[2].choices); // object => Yes,No,It's irrelevant.
console.log(questions[2].choices[0]); // string => Yes
console.log(questions[2].answer + "\n\n"); // string => It's irrelevant.
//The function randomly chooses an element from the array.
function getRandomQuestion(questions){
const index = questions.length;
const randomIndex = Math.floor(Math.random()*index);
return questions[randomIndex];
}
console.log(getRandomQuestion(questions)); //[object Object]
/* {
category: 'Food',
question: 'Do you like tomatoes?',
choices: [ 'Yes', 'No', 'It\'s irrelevant.' ],
answer: 'No'
}
*/
// Now, getRandomQuestion(questions).choices | output => array | ["Yes", "No", "It's irrelevant."]
/* The next function takes the array, choices, of the available choices as a parameter, and returns a random answer to the selected question.*/
function getRandomComputerChoice(questionChoices){
const index = questionChoices.length;
const randomIndex = Math.floor(Math.random()*index);
return questionChoices[randomIndex];
}
// the question object:
const randomChooseArrayItem = getRandomQuestion(questions);
// then, the computer's choice:
const randomComputerChoice = getRandomComputerChoice(randomChooseArrayItem.choices);
console.log("\n\n" + getRandomComputerChoice(randomChooseArrayItem.choices) + "\n\n"); //string | tomatoes
/* The next function takes the question object, as the first parameter and the computer's choice as the second parameter. */
function getResults(randomChooseArrayItem, randomComputerChoice){
if(randomChooseArrayItem.answer == randomComputerChoice){
return "The computer's choice is correct!";
} else {
return "The computer's choice is wrong. The correct answer is: " + randomChooseArrayItem.answer;
}
}
console.log(getResults(randomChooseArrayItem, randomComputerChoice));
It just means that when you create your questions array of question objects, that the value of the answer property in each question object should be included as one of the array elements in the choices property, which you have done.
/ running tests
7. The value of answer should be included in the choices array.
// tests completed
// console output
[ 'Yes', 'No', 'It\'s irrelevant.' ]
Yes
It's irrelevant
{ category: 'Food',
question: 'What fruit do you prefer?',
choices: [ 'tomatoes', 'bananas', 'apples' ],
answer: 'bananas' }
apples
The computer's choice is correct!