I recently passed this topic, but I had to delete these two variable definitions in order to pass user tests 11, 12, and 13.
-
- Your
getResultsfunction should take the question object as the first parameter and the computer’s choice as the second parameter.
- Your
-
Passed:12. If the computer choice matches the answer,
getResultsshould returnThe computer's choice is correct! -
Passed:13. If the computer choice doesn’t match the answer,
getResultsshould returnThe 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.
I am not sure I am understanding why this does not work, especially for being able to test/log results in one call console.log(getResults()). I am not sure I see how the data would pass through the functions otherwise to ensure getRandomQuestion() isn’t called twice, other than explicitly setting the outputs outside of the functions when calling them. Sometimes I am not sure how the testing works and wish it would be more explicit.
const getResults = (question, computerChoice) => {
question = getRandomQuestion(questions);
// console.log(question);
computerChoice = getRandomComputerChoice(question.choices);
// console.log(computerChoice);
// console.log(question.answer);
Additionally, originally, I also had this code for getting the computer’s choice, which was incorrect. I see the user story asks for the list of available choices, but I was thinking of it as passing the data of the random question call to the computer choice function. Is this the correct way to be thinking about functions or am I not abstracting enough/thinking about creating programs the right way?
const getRandomComputerChoice = (questions) => {
let computerChoice = questions.choices[Math.floor(Math.random() * (questions.choices.length))];
return computerChoice;
}
My full code is blurred and included below as well. Thanks!
const questions = [
{
category: "one",
question: "two?",
choices: ["three", "six", "four"],
answer: "four"
},
{
category: "cat",
question: "dog?",
choices: ["house", "dog", "pig"],
answer: "pig"
},
{
category: "hat",
question: "coat?",
choices: ["hat", "coat", "skirt"],
answer: "skirt"
},
{
category: "water",
question: "soda?",
choices: ["water", "soda", "beer"],
answer: "beer"
},
{
category: "plant",
question: "stick?",
choices: ["bag", "plant", "tree"],
answer: "tree"
}
];
const getRandomQuestion = (questions) => {
let randomQuestion = questions[Math.floor(Math.random() * questions.length)];
return randomQuestion;
};
const getRandomComputerChoice = (choices) => {
let computerChoice = choices[Math.floor(Math.random() * choices.length)];
};
const getResults = (question, computerChoice) => {
question = getRandomQuestion(questions);
console.log(question);
computerChoice = getRandomComputerChoice(question.choices);
console.log(computerChoice);
console.log(question.answer);
if (computerChoice === question.answer) {
return "The computer's choice is correct!";
} else {
return "The computer's choice is wrong. The correct answer is: " + question.answer;
}
}